59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"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 {
|
|
idx bleve.Index
|
|
}
|
|
|
|
func NewIndexer(conf *Config) (Indexer, error) {
|
|
var (
|
|
idx bleve.Index
|
|
err error
|
|
)
|
|
|
|
fn := filepath.Join(conf.Data, "spyda.bleve")
|
|
|
|
if FileExists(fn) {
|
|
idx, err = bleve.Open(fn)
|
|
} else {
|
|
mapping := bleve.NewIndexMapping()
|
|
idx, err = bleve.New(fn, mapping)
|
|
}
|
|
if err != nil {
|
|
log.WithError(err).Error("error creating indexer")
|
|
return nil, err
|
|
}
|
|
|
|
return &indexer{idx: idx}, nil
|
|
}
|
|
|
|
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
|
|
}
|