From 95de2cc96b47daceed492f2e9c7d68d045008d38 Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io> Date: Wed, 8 Jul 2015 13:49:31 +0300 Subject: [PATCH] Add nested aggregation example. --- docs/Aggregation/index.md | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/docs/Aggregation/index.md b/docs/Aggregation/index.md index 571f9c1..ffc3dfe 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) -- GitLab