Peter Stuifzand
62bad5ec91
All checks were successful
continuous-integration/drone/push Build is passing
42 lines
901 B
Go
42 lines
901 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/blevesearch/bleve"
|
|
"github.com/blevesearch/bleve/mapping"
|
|
)
|
|
|
|
// TODO: http handler
|
|
// TODO: index all pages on start
|
|
// TODO: reindex all command
|
|
// TODO: search(query) command
|
|
|
|
type searchHandler struct {
|
|
documents PagesRepository
|
|
indexMapping mapping.IndexMapping
|
|
searchIndex bleve.Index
|
|
}
|
|
|
|
func NewSearchHandler(searchIndex bleve.Index) (http.Handler, error) {
|
|
return &searchHandler{
|
|
searchIndex: searchIndex,
|
|
}, nil
|
|
}
|
|
|
|
func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
q := bleve.NewQueryStringQuery(r.URL.Query().Get("q"))
|
|
sr := bleve.NewSearchRequest(q)
|
|
results, err := s.searchIndex.Search(sr)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
}
|
|
enc := json.NewEncoder(w)
|
|
enc.SetIndent("", " ")
|
|
err = enc.Encode(&results)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
}
|
|
}
|