2

There's a perfectly good answer for how to get a unique count in solr from their website, http://yonik.com/solr-count-distinct/, and from this SO answer: How to select distinct field values using Solr?

My issue is that I do not understand how to translate this cURL syntax into PHP, which is what I'm coding in.

The official example seems easy enough to follow from a what's going on in this code perspective:

$ curl http://localhost:8983/solr/techproducts/query -d '
q=*:*&
json.facet={
  x : "unique(manu_exact)"    // manu_exact is the manufacturer indexed as a single string
}'

However, I'm only familiar with two ways of submitting a solr query to my server using PHP. The first, using a direct URL:

$url = "localhost:8983/solr/asdf/select?q=*:*";
$Q = curl_init();      
curl_setopt($Q, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($Q, CURLOPT_URL, $url);
$rawData = curl_exec($Q);
$data =  json_decode($rawData,true);

or through posting the values:

$url     = "localhost:8983/solr/asdf/select";
$solr_q  = "q=date_range:[2016+TO+*]&fq=title:manager&wt=json&indent=true";

$ch      = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $solr_q);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$rawData = curl_exec($ch);
$json = json_decode($rawData,true);

I'm running solr 5.4.1, so I know I have the ability to do json facets, but I have no idea how to make the request outside of the official example using command line curl. How do I utilize this json.facet in the same manner that I'm currently utilizing solr in PHP?

Community
  • 1
  • 1
Brian Powell
  • 3,336
  • 4
  • 34
  • 60

1 Answers1

3

As always - right after you make a post on SO you find the answer. If someone could still shed some light on maybe a better way for me to be doing this I would be very grateful - but this technically works as a direct URL:

http://localhost:8983/solr/asdf/select?q=*:*&json.facet.x="unique(field)"&wt=json&indent=true&rows=0

or using the syntax I used in the OP:

...
$url    = "http://localhost:8983/solr/asdf/select";
  $post   = "q=*:*&json.facet.x=\"unique(field)\"&rows=0&wt=json&indent=true";
...

The things I was missing is that:

  1. I had to name the json facet, (&json.facet.x= instead of &json.facet= and the { } characters need to be replaced with quotation marks " " in order for the query to work as a URL or a POST request.

I really hope this helps someone as it has been frustrating me to no end for about a month now.....

Brian Powell
  • 3,336
  • 4
  • 34
  • 60