JiteshPubreja
JiteshPubreja

Reputation: 28

Solr Json Facet API using Solarium

I'm Working on a PHP project where I'm using Solarium as my Primary Library for Interfacing with Apache Solr.

I'm using Json Facet API of Solr as follows:-

json.facet={"unique_docs":"unique(doc_id)","hll_docs":"hll(doc_id)"}

How can I use Similar Query with Solarium.

I've Found an Resolved Issue on the solarium Github Page that is of my concern.

JSON Facet API #378

But, the page doesn't explain the way to use it. There is nothing pertaining to this in the Solarium docs either.

Upvotes: 1

Views: 777

Answers (2)

Masi
Masi

Reputation: 9

To add a Solr-JSON-facet for to a Solarium query API you need to create a Solarium facet set object. To use stat facet functions (aka aggregate functions) Solarium provides methods to create and setup the query.

So this should do what you want:

$query = new \Solarium\QueryType\Select\Query\Query();
$query->getFacetSet()
    ->createJsonFacetAggregation('unique_docs') // the JSON key
    ->setFunction('unique(doc_id)'); // the aggregate function (JSON value)
    ->createJsonFacetAggregation('hll_docs')
    ->setFunction('hll(doc_id)');

Upvotes: 1

Vasco Lopes
Vasco Lopes

Reputation: 76

I got it working with customizerequest:

$facet_json = '{my_key:{type:terms,field:my_solr_field_name,domain:{blockChildren:"my_parent_filter:1"}}}';

$customizer = $this->client->getPlugin('customizerequest');
$customizer->createCustomization('json.facet')
    ->setType('param')
    ->setName('json.facet')
    ->setValue($facet_json);

There was a release that should support the API with something like $facetset->createJsonFacetTerms($options) but I never managed to get it working like it should and couldn't find any doc.

To get the json facet after the request is executed I have something like this:

$facet_result = $this->result_set->getFacetSet()->getFacet('my_key');

foreach($facet_result as $facet) {
    $value = $facet->getValue();
    $count = $facet->getCount();
}

Upvotes: 1

Related Questions