diff options
author | Alan Pearce | 2024-05-13 21:50:14 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-13 22:25:46 +0200 |
commit | 6b40e0c0fa90f11be14a93f1d6275779fd645cac (patch) | |
tree | 0df5bea87242450829ca441d235c077a0e09a149 /internal/search/search.go | |
parent | 2722f24af87f437ed9fcb8cc743ad1784141fd3a (diff) | |
download | searchix-6b40e0c0fa90f11be14a93f1d6275779fd645cac.tar.lz searchix-6b40e0c0fa90f11be14a93f1d6275779fd645cac.tar.zst searchix-6b40e0c0fa90f11be14a93f1d6275779fd645cac.zip |
refactor: combine import and web server into one binary
Diffstat (limited to 'internal/search/search.go')
-rw-r--r-- | internal/search/search.go | 123 |
1 files changed, 0 insertions, 123 deletions
diff --git a/internal/search/search.go b/internal/search/search.go deleted file mode 100644 index c930f15..0000000 --- a/internal/search/search.go +++ /dev/null @@ -1,123 +0,0 @@ -package search - -import ( - "bytes" - "context" - "encoding/gob" - "path" - "searchix/internal/options" - - "github.com/blevesearch/bleve/v2" - "github.com/blevesearch/bleve/v2/search" - "github.com/pkg/errors" -) - -const ResultsPerPage = 20 - -type DocumentMatch struct { - search.DocumentMatch - Data options.NixOption -} - -type Result struct { - *bleve.SearchResult - Hits []DocumentMatch -} - -type ReadIndex struct { - index bleve.Index - meta *IndexMeta -} - -func Open(dataRoot string) (*ReadIndex, error) { - indexPath := path.Join(dataRoot, indexBaseName) - metaPath := path.Join(dataRoot, metaBaseName) - - idx, err := bleve.Open(indexPath) - if err != nil { - return nil, errors.WithMessagef(err, "unable to open index at path %s", indexPath) - } - - meta, err := openMeta(metaPath) - if err != nil { - return nil, errors.WithMessagef(err, "unable to open metadata at path %s", metaPath) - } - - return &ReadIndex{ - idx, - meta, - }, nil -} - -func (index *ReadIndex) GetSource(ctx context.Context, name string) (*bleve.SearchResult, error) { - query := bleve.NewTermQuery(name) - query.SetField("Source") - search := bleve.NewSearchRequest(query) - - result, err := index.index.SearchInContext(ctx, search) - - select { - case <-ctx.Done(): - return nil, ctx.Err() - default: - if err != nil { - return nil, errors.WithMessagef( - err, - "failed to execute search to find source %s in index", - name, - ) - } - } - - return result, nil -} - -func (index *ReadIndex) Search( - ctx context.Context, - source string, - keyword string, - from uint64, -) (*Result, error) { - sourceQuery := bleve.NewTermQuery(source) - userQuery := bleve.NewMatchQuery(keyword) - userQuery.Analyzer = "option_name" - - query := bleve.NewConjunctionQuery(sourceQuery, userQuery) - - search := bleve.NewSearchRequest(query) - search.Size = ResultsPerPage - search.Fields = []string{"_data"} - - if from != 0 { - search.From = int(from) - } - - bleveResult, err := index.index.SearchInContext(ctx, search) - select { - case <-ctx.Done(): - return nil, ctx.Err() - default: - if err != nil { - return nil, errors.WithMessage(err, "failed to execute search query") - } - - results := make([]DocumentMatch, min(ResultsPerPage, bleveResult.Total)) - var buf bytes.Buffer - for i, result := range bleveResult.Hits { - _, err = buf.WriteString(result.Fields["_data"].(string)) - if err != nil { - return nil, errors.WithMessage(err, "error fetching result data") - } - err = gob.NewDecoder(&buf).Decode(&results[i].Data) - if err != nil { - return nil, errors.WithMessagef(err, "error decoding gob data: %s", buf.String()) - } - buf.Reset() - } - - return &Result{ - SearchResult: bleveResult, - Hits: results, - }, nil - } -} |