about summary refs log tree commit diff stats
path: root/internal/index/search.go
diff options
context:
space:
mode:
authorAlan Pearce2025-01-02 15:14:34 +0100
committerAlan Pearce2025-01-02 15:14:34 +0100
commit797c6e27e18f6c47ceb4266d747a05064365c32d (patch)
tree2c67bb5e15f5bc1c3679ff8107b72b6e71132a53 /internal/index/search.go
parent58643f38bdd62dc538a03865b1f81eb77d957779 (diff)
downloadsearchix-797c6e27e18f6c47ceb4266d747a05064365c32d.tar.lz
searchix-797c6e27e18f6c47ceb4266d747a05064365c32d.tar.zst
searchix-797c6e27e18f6c47ceb4266d747a05064365c32d.zip
fix: boost non-stemmed matches over stemmed matches
https://todo.sr.ht/~alanpearce/searchix/7
Diffstat (limited to 'internal/index/search.go')
-rw-r--r--internal/index/search.go31
1 files changed, 17 insertions, 14 deletions
diff --git a/internal/index/search.go b/internal/index/search.go
index 4fe1736..d576d5a 100644
--- a/internal/index/search.go
+++ b/internal/index/search.go
@@ -10,7 +10,6 @@ import (
 	"go.alanpearce.eu/x/log"
 
 	"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"
@@ -142,7 +141,15 @@ func (index *ReadIndex) Search(
 	// match the user's query in any field ...
 	userQuery := bleve.NewMatchQuery(keyword)
 	userQuery.Analyzer = "option_name"
-	query.AddMust(userQuery)
+
+	userQueryLoc := bleve.NewMatchQuery(keyword)
+	userQueryLoc.Analyzer = "loc"
+	userQueryLoc.SetBoost(2)
+
+	query.AddMust(bleve.NewDisjunctionQuery(
+		userQuery,
+		userQueryLoc,
+	))
 
 	if source != nil {
 		query.AddMust(
@@ -151,27 +158,23 @@ func (index *ReadIndex) Search(
 	} else {
 		q := bleve.NewTermQuery("nixpkgs")
 		q.SetField("Source")
-		q.SetBoost(-100)
+		q.SetBoost(-150)
 
 		query.AddShould(q)
 	}
 
-	// ...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"),
-	)
+	attrQuery := bleve.NewMatchQuery(keyword)
+	attrQuery.SetField("Attribute")
+	attrQuery.Analyzer = "keyword_single"
+	query.AddShould(attrQuery)
+
 	nameLiteralQuery := bleve.NewMatchQuery(keyword)
 	nameLiteralQuery.SetField("Name")
-	nameLiteralQuery.Analyzer = standard.Name
+	nameLiteralQuery.Analyzer = "keyword_single"
 	query.AddShould(nameLiteralQuery)
 
 	search := bleve.NewSearchRequest(query)
+	search.Explain = config.DevMode
 	search.Size = ResultsPerPage
 
 	if from != 0 {