Grouping results by queries
Instead of grouping by a field, we can also group by query. Let us create groups for different price ranges. Instead of using the addField()
function on the grouping component, we have to use the addQuery()
function and specify our query as a parameter in the function as shown in the following:
$grp->addQuery('price:[0 TO 5]'); $grp->addQuery('price:[5.01 TO 10]'); $grp->addQuery('price:[10.01 TO *]');
Here we have created 3 groups for price ranges $0 to $5, $5.01 to $10 and more than $10.
We can also set sorting within a group using the setSort()
function as shown in the following query:
$grp->setSort('price desc');
Code to display the groups is similar to that discussed earlier. Output from our code is shown in the following screenshot:
From Solr logs we can see that instead of group.field
parameter we have got multiple
group.query
parameters.
1404903 [http-bio-8080-exec-4] INFO org.apache.solr.core.SolrCore – [collection1] webapp=/solr path...