The stats component
The stats component can be used to return simple statistics for indexed numeric fields in the document set returned by a Solr query. Let us get the statistics for prices of all books in our index. We will also facet on price
and availability (inStock
) and see the output.
Tip
It is advisable to use a templating engine instead of writing HTML code inside PHP.
Create the query to fetch all books and set the number of rows to 0
as we are not interested in the results but only the statistics, which will be fetched as a separate component as given in the following query:
$query->setQuery('cat:book'); $query->setRows(0);
Get the stats component and create statistics for field price
and create facets on price
and inStock
fields.
$statsq = $query->getStats(); $statsq->createField('price')->addFacet('price')->addFacet('inStock');
Execute the query and fetch the stats component from the result-set as given in the following query:
$resultset = $client->select...