diff options
author | Alan Pearce | 2024-07-12 20:52:52 +0200 |
---|---|---|
committer | Alan Pearce | 2024-07-12 21:20:15 +0200 |
commit | 6781684647ad7f131054a93e1d13105d865c0e83 (patch) | |
tree | 307a688125f18128f582f7754af9a5451f0cb109 /internal/index/search.go | |
parent | 442345898802e93b0b884fd0f1c3492e08c34049 (diff) | |
download | searchix-6781684647ad7f131054a93e1d13105d865c0e83.tar.lz searchix-6781684647ad7f131054a93e1d13105d865c0e83.tar.zst searchix-6781684647ad7f131054a93e1d13105d865c0e83.zip |
feat: enable combined search
Diffstat (limited to 'internal/index/search.go')
-rw-r--r-- | internal/index/search.go | 69 |
1 files changed, 35 insertions, 34 deletions
diff --git a/internal/index/search.go b/internal/index/search.go index 03a8e60..c5b8c6b 100644 --- a/internal/index/search.go +++ b/internal/index/search.go @@ -19,7 +19,7 @@ import ( const ResultsPerPage = 20 type DocumentMatch struct { - search.DocumentMatch + *search.DocumentMatch Data nix.Importable } @@ -53,9 +53,18 @@ func (index *ReadIndex) GetEnabledSources() ([]string, error) { return enabledSources, nil } -func (index *ReadIndex) GetSource(ctx context.Context, name string) (*bleve.SearchResult, error) { - query := bleve.NewTermQuery(name) - query.SetField("Source") +func (index *ReadIndex) GetSource( + ctx context.Context, + source *config.Source, +) (*bleve.SearchResult, error) { + var query query.Query + if source == nil { + query = bleve.NewMatchAllQuery() + } else { + tq := bleve.NewTermQuery(source.Name) + tq.SetField("Source") + query = tq + } search := bleve.NewSearchRequest(query) result, err := index.index.SearchInContext(ctx, search) @@ -68,7 +77,7 @@ func (index *ReadIndex) GetSource(ctx context.Context, name string) (*bleve.Sear return nil, errors.WithMessagef( err, "failed to execute search to find source %s in index", - name, + source, ) } } @@ -89,7 +98,7 @@ func (index *ReadIndex) search( ctx context.Context, request *bleve.SearchRequest, ) (*Result, error) { - request.Fields = []string{"_data"} + request.Fields = []string{"_data", "Source"} bleveResult, err := index.index.SearchInContext(ctx, request) select { @@ -103,6 +112,7 @@ func (index *ReadIndex) search( results := make([]DocumentMatch, min(ResultsPerPage, bleveResult.Total)) var buf bytes.Buffer for i, result := range bleveResult.Hits { + results[i].DocumentMatch = bleveResult.Hits[i] _, err = buf.WriteString(result.Fields["_data"].(string)) if err != nil { return nil, errors.WithMessage(err, "error fetching result data") @@ -133,37 +143,28 @@ func (index *ReadIndex) Search( userQuery := bleve.NewMatchQuery(keyword) userQuery.Analyzer = "option_name" - query.AddMust( - setField(bleve.NewTermQuery(source.Key), "Source"), - userQuery, - ) - - switch source.Importer { - case config.Packages: - // ...and boost it if it matches any of these - query.AddShould( - setField(bleve.NewMatchQuery(keyword), "MainProgram"), - setField(bleve.NewMatchQuery(keyword), "Name"), - setField(bleve.NewMatchQuery(keyword), "Attribute"), - ) - case config.Options: - query.AddShould( - setField(bleve.NewMatchQuery(keyword), "Loc"), - setField(bleve.NewMatchQuery(keyword), "Name"), - ) - nameLiteralQuery := bleve.NewMatchQuery(keyword) - nameLiteralQuery.SetField("Name") - nameLiteralQuery.Analyzer = standard.Name - query.AddShould( - nameLiteralQuery, - ) - default: - return nil, errors.Errorf( - "unsupported source type for search: %s", - source.Importer.String(), + if source != nil { + query.AddMust( + setField(bleve.NewTermQuery(source.Key), "Source"), + userQuery, ) } + // ...and boost it if it matches any of these + query.AddShould( + setField(bleve.NewMatchQuery(keyword), "MainProgram"), + setField(bleve.NewMatchQuery(keyword), "Name"), + setField(bleve.NewMatchQuery(keyword), "Attribute"), + ) + query.AddShould( + setField(bleve.NewMatchQuery(keyword), "Loc"), + setField(bleve.NewMatchQuery(keyword), "Name"), + ) + nameLiteralQuery := bleve.NewMatchQuery(keyword) + nameLiteralQuery.SetField("Name") + nameLiteralQuery.Analyzer = standard.Name + query.AddShould(nameLiteralQuery) + search := bleve.NewSearchRequest(query) search.Size = ResultsPerPage |