Add additional fields to search request
This commit is contained in:
@@ -142,6 +142,26 @@ func (s *Server) CacheHandler() httprouter.Handle {
|
||||
func (s *Server) SearchHandler() httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
ctx := NewContext(s.config, s.db, r)
|
||||
|
||||
q := strings.TrimSpace(r.FormValue("q"))
|
||||
if q == "" {
|
||||
ctx.Error = true
|
||||
ctx.Message = "Error empty search"
|
||||
s.render("error", w, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
results, err := s.indexer.Search(q)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("error performing search")
|
||||
ctx.Error = true
|
||||
ctx.Message = "Error performing search, please try again later"
|
||||
s.render("error", w, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug(results)
|
||||
|
||||
s.render("search", w, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user