diff --git a/docs/Aggregation/index.md b/docs/Aggregation/index.md index 571f9c16f9ca6dcc24ee75aefb74f9aef3e9caa8..ffc3dfe7fa632e29118bd4d8b86e20ffe3de72da 100644 --- a/docs/Aggregation/index.md +++ b/docs/Aggregation/index.md @@ -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)