cakephp / elastic-search

Elastic search datasource for CakePHP
Other
88 stars 53 forks source link

Aggregation sample #247

Closed irongomme closed 3 years ago

irongomme commented 3 years ago

Is it possible to have a sample use for aggregation ?

irongomme commented 3 years ago

I've found how to use aggregation. So if it can be usefull, here is my way :

// Declare aggregations
$filtersAggs = new \Elastica\Aggregation\Terms('filters');
$filtersAggs->setField('filters.filter_value_id');
$filtersAggs->setSize(100);

$printersAggs = new \Elastica\Aggregation\Terms('printers');
$printersAggs->setField('printers.keyword');
$printersAggs->setSize(50);

// Add it to query
$results = $this->EsProducts->find()
    ->where($conditions)
    ->limit($limit)
    ->page($page)
    ->aggregate([$filtersAggs, $printersAggs])
    ->all();

// Read results 
$totalCount = $results->getTotalHits();
$filterAggregation = $results->getAggregation('filters');
$printerAggregation = `$results->getAggregation('printers');