all repos — searchix @ 4c06f763f471f51a655b8437f9529149512e5507

Search engine for NixOS, nix-darwin, home-manager and NUR users

refactor: enable access to bleve DocumentMatch structs

Alan Pearce
commit

4c06f763f471f51a655b8437f9529149512e5507

parent

8537192b854cb4abde7c7ca2d58c201df060504a

1 file changed, 11 insertions(+), 5 deletions(-)

changed files
M internal/search/search.gointernal/search/search.go
@@ -15,6 +15,7 @@ "github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
"github.com/blevesearch/bleve/v2/analysis/token/camelcase" "github.com/blevesearch/bleve/v2/analysis/tokenizer/letter" "github.com/blevesearch/bleve/v2/document" + "github.com/blevesearch/bleve/v2/search" index "github.com/blevesearch/bleve_index_api" "github.com/mitchellh/mapstructure" "github.com/pkg/errors"
@@ -22,9 +23,14 @@ )
const ResultsPerPage = 20 +type DocumentMatch[T options.NixOption] struct { + search.DocumentMatch + Data T +} + type Result[T options.NixOption] struct { *bleve.SearchResult - Results []T + Hits []DocumentMatch[T] } type Index[T options.NixOption] struct {
@@ -158,14 +164,14 @@ if err != nil {
return nil, errors.WithMessage(err, "failed to execute search query") } - results := make([]T, min(ResultsPerPage, bleveResult.Total)) + results := make([]DocumentMatch[T], 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]) + err = gob.NewDecoder(&buf).Decode(&results[i].Data) if err != nil { return nil, errors.WithMessagef(err, "error decoding gob data: %s", buf.String()) }
@@ -173,8 +179,8 @@ buf.Reset()
} return &Result[T]{ - bleveResult, - results, + SearchResult: bleveResult, + Hits: results, }, nil } }