diff options
author | Alan Pearce | 2024-05-08 13:32:19 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-08 13:34:21 +0200 |
commit | 24b81a63620667cd9c9f50952bbe881c08f16048 (patch) | |
tree | 9904a827a8fe5af8a5ac0ca2114e7178f10582f9 /internal/search/search.go | |
parent | 83f6751139ed2dbf2f7c2f235e03d91d632e38bb (diff) | |
download | searchix-24b81a63620667cd9c9f50952bbe881c08f16048.tar.lz searchix-24b81a63620667cd9c9f50952bbe881c08f16048.tar.zst searchix-24b81a63620667cd9c9f50952bbe881c08f16048.zip |
feat: improve search by using a custom tokeniser for option names
Diffstat (limited to 'internal/search/search.go')
-rw-r--r-- | internal/search/search.go | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/internal/search/search.go b/internal/search/search.go index b7640fc..9021d7b 100644 --- a/internal/search/search.go +++ b/internal/search/search.go @@ -11,6 +11,9 @@ import ( "github.com/bcicen/jstream" "github.com/blevesearch/bleve/v2" + "github.com/blevesearch/bleve/v2/analysis/analyzer/custom" + "github.com/blevesearch/bleve/v2/analysis/token/camelcase" + "github.com/blevesearch/bleve/v2/analysis/tokenizer/letter" "github.com/mitchellh/mapstructure" "github.com/pkg/errors" ) @@ -28,21 +31,44 @@ type Index[T options.NixOption] struct { } func New[T options.NixOption](kind string) (*Index[T], error) { + var err error bleve.SetLog(log.Default()) + + indexMapping := bleve.NewIndexMapping() + textFieldMapping := bleve.NewTextFieldMapping() + descriptionFieldMapping := bleve.NewTextFieldMapping() + descriptionFieldMapping.Analyzer = "web" + + err = indexMapping.AddCustomAnalyzer("option_name", map[string]interface{}{ + "type": custom.Name, + "tokenizer": letter.Name, + "token_filters": []string{ + camelcase.Name, + }, + }) + if err != nil { + return nil, errors.WithMessage(err, "could not add custom analyser") + } nameMapping := bleve.NewTextFieldMapping() - // something special for tokenisation? - // nameMapping. + nameMapping.Analyzer = "option_name" + nameMapping.IncludeTermVectors = true + nixValueMapping := bleve.NewDocumentStaticMapping() + nixValueMapping.AddFieldMappingsAt("Text", textFieldMapping) + nixValueMapping.AddFieldMappingsAt("Markdown", textFieldMapping) + optionMapping := bleve.NewDocumentStaticMapping() optionMapping.AddFieldMappingsAt("Option", nameMapping) + optionMapping.AddFieldMappingsAt("Loc", bleve.NewKeywordFieldMapping()) optionMapping.AddFieldMappingsAt("RelatedPackages", textFieldMapping) optionMapping.AddFieldMappingsAt("Description", textFieldMapping) - indexMapping := bleve.NewIndexMapping() + optionMapping.AddSubDocumentMapping("Default", nixValueMapping) + optionMapping.AddSubDocumentMapping("Example", nixValueMapping) + indexMapping.AddDocumentMapping("option", optionMapping) - var err error index, err := bleve.NewMemOnly(indexMapping) // index, err = bleve.New(path.Join(cfg.DataPath, const indexFilename = "index.bleve"), indexMapping) @@ -96,6 +122,7 @@ func New[T options.NixOption](kind string) (*Index[T], error) { func (index *Index[T]) Search(ctx context.Context, keyword string, from uint64) (*Result[T], error) { query := bleve.NewMatchQuery(keyword) + query.Analyzer = "option_name" search := bleve.NewSearchRequest(query) search.Size = ResultsPerPage |