go-solr
go-solr copied to clipboard
Support for facet
Hi,
Thanks for the great solr connector!
I'm working on a project that needs to implement features with solr facet module by sending HTTP request. Will there be any support for facet in select API ?
Thanks!
You can always use http
module for that. Your request would look like http://localhost:8983/solr/yourcore/select?q=*.*&fq.type="YOURTYPE&wt=json&indent=true&facet=true&facet.field=YOURFACETFIELD
, which in Go you can always write like this:
u, _ := url.Parse("http://localhost:8983/solr/YOURCORE/select")
search := url.Values{
"q": []string{"*:*"},
"wt": []string{"json"},
"indent": []string{"true"},
"facet": []string{"true"},
"facet.field": []string{"field_1", "field_2"}, // where fields are actual fields
"fq": []string{"type:\"YOURFILTERQUERY\""},
}
u.RawQuery = search.Encode()
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return errors.WithMessage(err, "service error: creating solr request failed")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.WithMessage(err, "service error: sending request failed")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.WithMessage(err, "service error: status code not ok")
}
Not the cleanest way to do so, but works.
#60 I opened a PR adding Solr JSON Faceting Support. The addition is rather simple as much of the plumbing was already in place, but it should support the entire JSON faceting API.