diff options
author | Alan Pearce | 2024-05-29 11:56:51 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-29 11:56:51 +0200 |
commit | 73185e4633d4f56ce8acdd3cc19f78faaa78bd2b (patch) | |
tree | a50878b5592a8ed165db9faff07c103620a682fc /internal/index/search.go | |
parent | 838e178e503d9576a40fce8fc986c4e1972e4c0f (diff) | |
download | searchix-73185e4633d4f56ce8acdd3cc19f78faaa78bd2b.tar.lz searchix-73185e4633d4f56ce8acdd3cc19f78faaa78bd2b.tar.zst searchix-73185e4633d4f56ce8acdd3cc19f78faaa78bd2b.zip |
feat: improve search relevance by boosting keyword matches
Diffstat (limited to 'internal/index/search.go')
-rw-r--r-- | internal/index/search.go | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/internal/index/search.go b/internal/index/search.go index 0b20063..47f8ac1 100644 --- a/internal/index/search.go +++ b/internal/index/search.go @@ -8,7 +8,9 @@ import ( "searchix/internal/nix" "github.com/blevesearch/bleve/v2" + "github.com/blevesearch/bleve/v2/analysis/analyzer/standard" "github.com/blevesearch/bleve/v2/search" + "github.com/blevesearch/bleve/v2/search/query" "github.com/pkg/errors" ) @@ -71,18 +73,57 @@ func (index *ReadIndex) GetSource(ctx context.Context, name string) (*bleve.Sear return result, nil } +func setField( + q query.FieldableQuery, + field string, +) query.FieldableQuery { + q.SetField(field) + + return q +} + func (index *ReadIndex) Search( ctx context.Context, source *config.Source, keyword string, from uint64, ) (*Result, error) { - sourceQuery := bleve.NewTermQuery(source.Key) - sourceQuery.SetField("Source") + query := bleve.NewBooleanQuery() + + // match the user's query in any field ... userQuery := bleve.NewMatchQuery(keyword) userQuery.Analyzer = "option_name" - query := bleve.NewConjunctionQuery(sourceQuery, userQuery) + 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(), + ) + } search := bleve.NewSearchRequest(query) search.Size = ResultsPerPage |