about summary refs log tree commit diff stats
path: root/internal/index
diff options
context:
space:
mode:
authorAlan Pearce2024-05-29 11:56:51 +0200
committerAlan Pearce2024-05-29 11:56:51 +0200
commit73185e4633d4f56ce8acdd3cc19f78faaa78bd2b (patch)
treea50878b5592a8ed165db9faff07c103620a682fc /internal/index
parent838e178e503d9576a40fce8fc986c4e1972e4c0f (diff)
downloadsearchix-73185e4633d4f56ce8acdd3cc19f78faaa78bd2b.tar.lz
searchix-73185e4633d4f56ce8acdd3cc19f78faaa78bd2b.tar.zst
searchix-73185e4633d4f56ce8acdd3cc19f78faaa78bd2b.zip
feat: improve search relevance by boosting keyword matches
Diffstat (limited to 'internal/index')
-rw-r--r--internal/index/indexer.go3
-rw-r--r--internal/index/search.go47
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