go-solr icon indicating copy to clipboard operation
go-solr copied to clipboard

Support for facet

Open ymtech2004 opened this issue 6 years ago • 2 comments

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!

ymtech2004 avatar Sep 26 '18 01:09 ymtech2004

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.

mutantkeyboard avatar Nov 29 '18 08:11 mutantkeyboard

#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.

M-Anwar avatar Apr 23 '19 18:04 M-Anwar