Skip to content
Snippets Groups Projects
Commit 95de2cc9 authored by Aivaras Gotovskis's avatar Aivaras Gotovskis
Browse files

Add nested aggregation example.

parent 33dcb907
No related branches found
No related tags found
Loading
......@@ -16,6 +16,67 @@ $queryArray = $search->toArray();
There are 2 types of aggregation: bucketing and metric. The only difference in using them is that metric bucketing
aggregations supports nesting while metric aggregations will ignore any set nested aggregations.
## Nesting aggregations
Bucketing aggregation can have anu number nested aggregations and nesting can go to unlimited depth.
Example nested aggregation.
```JSON
{
"aggregations": {
"agg_color": {
"terms": {
"field": "color"
},
"aggregations": {
"agg_avg_price": {
"avg": {
"field": "price"
}
},
"agg_brand": {
"terms": {
"field": "brand"
},
"aggregations": {
"agg_avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
},
"agg_avg_price": {
"avg": {
"field": "price"
}
}
}
}
```
```php
$avgPriceAggregation = new AvgAggregation('avg_price');
$avgPriceAggregation->setField('price');
$brandTermAggregation = new TermsAggregation('brand');
$brandTermAggregation->setField('brand');
$brandTermAggregation->addAggregation($avgPriceAggregation);
$colorTermsAggregation = new TermsAggregation('color');
$colorTermsAggregation->setField('color');
$colorTermsAggregation->addAggregation($avgPriceAggregation);
$colorTermsAggregation->addAggregation($brandTermAggregation);
$search = new Search();
$search->addAggregation($colorTermsAggregation);
$search->addAggregation($avgPriceAggregation);
$queryArray = $search->toArray();
```
## Metric Aggregations
- [Avg](Avg.md)
- [Cardinality](Cardinality.md)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment