diff options
-rw-r--r-- | internal/index/indexer.go | 3 | ||||
-rw-r--r-- | internal/index/search.go | 47 |
2 files changed, 46 insertions, 4 deletions
diff --git a/internal/index/indexer.go b/internal/index/indexer.go index 742cf5e..a59889a 100644 --- a/internal/index/indexer.go +++ b/internal/index/indexer.go @@ -101,7 +101,7 @@ func createIndexMapping() (mapping.IndexMapping, error) { optionMapping := bleve.NewDocumentStaticMapping() - optionMapping.AddFieldMappingsAt("Name", keywordFieldMapping) + optionMapping.AddFieldMappingsAt("Name", nameMapping) optionMapping.AddFieldMappingsAt("Source", keywordFieldMapping) optionMapping.AddFieldMappingsAt("Loc", locFieldMapping) optionMapping.AddFieldMappingsAt("RelatedPackages", textFieldMapping) @@ -112,6 +112,7 @@ func createIndexMapping() (mapping.IndexMapping, error) { packageMapping := bleve.NewDocumentStaticMapping() + packageMapping.AddFieldMappingsAt("Name", nameMapping) packageMapping.AddFieldMappingsAt("Attribute", keywordFieldMapping) packageMapping.AddFieldMappingsAt("Source", keywordFieldMapping) packageMapping.AddFieldMappingsAt("Description", textFieldMapping) 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 |