You've already forked multi-select-facet
mirror of
https://github.com/stevenferrer/multi-select-facet.git
synced 2025-11-23 21:54:45 +02:00
added auto-suggest
This commit is contained in:
60
cmd/api/suggest_handler.go
Normal file
60
cmd/api/suggest_handler.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/stevenferrer/solr-go"
|
||||
"github.com/stevenferrer/solr-go/suggester"
|
||||
)
|
||||
|
||||
type suggestHandler struct {
|
||||
collection string
|
||||
solrClient solr.Client
|
||||
}
|
||||
|
||||
func (h *suggestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query().Get("q")
|
||||
|
||||
if len(q) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
dict := "mySuggester"
|
||||
suggestResp, err := h.solrClient.Suggester().
|
||||
Suggest(r.Context(), suggester.Request{
|
||||
Collection: h.collection,
|
||||
Params: suggester.Params{
|
||||
Query: q,
|
||||
Dictionaries: []string{dict},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
||||
suggest := *suggestResp.Suggest
|
||||
termBody := suggest[dict][q]
|
||||
|
||||
suggestions := []struct {
|
||||
Term string `json:"term"`
|
||||
}{}
|
||||
for _, suggest := range termBody.Suggestions {
|
||||
suggestions = append(suggestions, struct {
|
||||
Term string `json:"term"`
|
||||
}{Term: suggest.Term})
|
||||
}
|
||||
|
||||
resp := map[string]interface{}{
|
||||
"numFound": termBody.NumFound,
|
||||
"suggestions": suggestions,
|
||||
}
|
||||
|
||||
w.Header().Add("content-type", "application/json")
|
||||
|
||||
err = json.NewEncoder(w).Encode(resp)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user