Add additional fields to search request

This commit is contained in:
James Mills
2021-02-02 00:18:45 +10:00
parent 8a1161cf77
commit 4383b3125d
4 changed files with 62 additions and 1 deletions

View File

@@ -1,14 +1,16 @@
package internal
import (
"fmt"
"path/filepath"
"github.com/apex/log"
"github.com/blevesearch/bleve/v2"
log "github.com/sirupsen/logrus"
)
type Indexer interface {
Index(entry *Entry) error
Search(q string) (*bleve.SearchResult, error)
}
type indexer struct {
@@ -40,3 +42,17 @@ func NewIndexer(conf *Config) (Indexer, error) {
func (i *indexer) Index(entry *Entry) error {
return i.idx.Index(entry.Hash(), entry)
}
func (i *indexer) Search(q string) (*bleve.SearchResult, error) {
query := bleve.NewMatchQuery(q)
searchRequest := bleve.NewSearchRequest(query)
searchRequest.Fields = []string{"URL", "Title", "Author", "Summary", "Length"}
searchResults, err := i.idx.Search(searchRequest)
if err != nil {
log.WithError(err).Error("error searching index")
return nil, fmt.Errorf("error searching index: %w", err)
}
log.Debug(searchResults)
return searchResults, nil
}