about summary refs log tree commit diff stats
path: root/internal/index/search.go
diff options
context:
space:
mode:
authorAlan Pearce2024-06-08 20:31:47 +0200
committerAlan Pearce2024-06-08 20:42:48 +0200
commitd40c0e188a7fe1b36887f59c4a9958faa81b3d44 (patch)
treeb6c168adcc3336dd0f6de4b1cbb7bf671f212f53 /internal/index/search.go
parentef6c98da84c2327e0a0003fb3b1b64a5d1e2d550 (diff)
downloadsearchix-d40c0e188a7fe1b36887f59c4a9958faa81b3d44.tar.lz
searchix-d40c0e188a7fe1b36887f59c4a9958faa81b3d44.tar.zst
searchix-d40c0e188a7fe1b36887f59c4a9958faa81b3d44.zip
feat: add detail pages for packages/options
Diffstat (limited to 'internal/index/search.go')
-rw-r--r--internal/index/search.go77
1 files changed, 51 insertions, 26 deletions
diff --git a/internal/index/search.go b/internal/index/search.go
index 47f8ac1..3198afb 100644
--- a/internal/index/search.go
+++ b/internal/index/search.go
@@ -82,6 +82,42 @@ func setField(
 	return q
 }
 
+func (index *ReadIndex) search(
+	ctx context.Context,
+	request *bleve.SearchRequest,
+) (*Result, error) {
+	request.Fields = []string{"_data"}
+
+	bleveResult, err := index.index.SearchInContext(ctx, request)
+	select {
+	case <-ctx.Done():
+		return nil, ctx.Err()
+	default:
+		if err != nil {
+			return nil, errors.WithMessage(err, "failed to execute search query")
+		}
+
+		results := make([]DocumentMatch, min(ResultsPerPage, bleveResult.Total))
+		var buf bytes.Buffer
+		for i, result := range bleveResult.Hits {
+			_, err = buf.WriteString(result.Fields["_data"].(string))
+			if err != nil {
+				return nil, errors.WithMessage(err, "error fetching result data")
+			}
+			err = gob.NewDecoder(&buf).Decode(&results[i].Data)
+			if err != nil {
+				return nil, errors.WithMessagef(err, "error decoding gob data: %s", buf.String())
+			}
+			buf.Reset()
+		}
+
+		return &Result{
+			SearchResult: bleveResult,
+			Hits:         results,
+		}, nil
+	}
+}
+
 func (index *ReadIndex) Search(
 	ctx context.Context,
 	source *config.Source,
@@ -127,38 +163,27 @@ func (index *ReadIndex) Search(
 
 	search := bleve.NewSearchRequest(query)
 	search.Size = ResultsPerPage
-	search.Fields = []string{"_data"}
 
 	if from != 0 {
 		search.From = int(from)
 	}
 
-	bleveResult, err := index.index.SearchInContext(ctx, search)
-	select {
-	case <-ctx.Done():
-		return nil, ctx.Err()
-	default:
-		if err != nil {
-			return nil, errors.WithMessage(err, "failed to execute search query")
-		}
+	return index.search(ctx, search)
+}
 
-		results := make([]DocumentMatch, min(ResultsPerPage, bleveResult.Total))
-		var buf bytes.Buffer
-		for i, result := range bleveResult.Hits {
-			_, err = buf.WriteString(result.Fields["_data"].(string))
-			if err != nil {
-				return nil, errors.WithMessage(err, "error fetching result data")
-			}
-			err = gob.NewDecoder(&buf).Decode(&results[i].Data)
-			if err != nil {
-				return nil, errors.WithMessagef(err, "error decoding gob data: %s", buf.String())
-			}
-			buf.Reset()
-		}
+func (index *ReadIndex) GetDocument(
+	ctx context.Context,
+	source *config.Source,
+	id string,
+) (*nix.Importable, error) {
+	key := nix.MakeKey(source, id)
+	query := bleve.NewDocIDQuery([]string{key})
+	search := bleve.NewSearchRequest(query)
 
-		return &Result{
-			SearchResult: bleveResult,
-			Hits:         results,
-		}, nil
+	result, err := index.search(ctx, search)
+	if err != nil {
+		return nil, err
 	}
+
+	return &result.Hits[0].Data, err
 }