2013-11-24 06:13:08 -05:00
|
|
|
[[search-aggregations-bucket-nested-aggregation]]
|
2014-05-12 19:35:58 -04:00
|
|
|
=== Nested Aggregation
|
2013-11-24 06:13:08 -05:00
|
|
|
|
|
|
|
A special single bucket aggregation that enables aggregating nested documents.
|
|
|
|
|
2017-09-04 00:39:39 -04:00
|
|
|
For example, lets say we have an index of products, and each product holds the list of resellers - each having its own
|
2014-01-29 07:09:12 -05:00
|
|
|
price for the product. The mapping could look like:
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2019-09-05 10:11:25 -04:00
|
|
|
[source,console]
|
2013-11-24 06:13:08 -05:00
|
|
|
--------------------------------------------------
|
2019-09-11 12:06:42 -04:00
|
|
|
PUT /products
|
2013-11-24 06:13:08 -05:00
|
|
|
{
|
2020-07-20 15:59:00 -04:00
|
|
|
"mappings": {
|
|
|
|
"properties": {
|
|
|
|
"resellers": { <1>
|
|
|
|
"type": "nested",
|
|
|
|
"properties": {
|
|
|
|
"reseller": { "type": "text" },
|
|
|
|
"price": { "type": "double" }
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
2020-07-20 15:59:00 -04:00
|
|
|
}
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
2020-07-20 15:59:00 -04:00
|
|
|
}
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2019-09-11 12:06:42 -04:00
|
|
|
<1> `resellers` is an array that holds nested documents.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2019-09-11 12:06:42 -04:00
|
|
|
The following request adds a product with two resellers:
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2019-09-05 10:11:25 -04:00
|
|
|
[source,console]
|
2013-11-24 06:13:08 -05:00
|
|
|
--------------------------------------------------
|
2019-09-11 12:06:42 -04:00
|
|
|
PUT /products/_doc/0
|
|
|
|
{
|
|
|
|
"name": "LED TV", <1>
|
|
|
|
"resellers": [
|
|
|
|
{
|
|
|
|
"reseller": "companyA",
|
|
|
|
"price": 350
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"reseller": "companyB",
|
|
|
|
"price": 500
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TEST[s/PUT \/products\/_doc\/0/PUT \/products\/_doc\/0\?refresh/]
|
|
|
|
// TEST[continued]
|
|
|
|
<1> We are using a dynamic mapping for the `name` attribute.
|
|
|
|
|
|
|
|
|
|
|
|
The following request returns the minimum price a product can be purchased for:
|
|
|
|
|
|
|
|
[source,console]
|
|
|
|
--------------------------------------------------
|
|
|
|
GET /products/_search
|
2013-11-24 06:13:08 -05:00
|
|
|
{
|
2020-07-20 15:59:00 -04:00
|
|
|
"query": {
|
|
|
|
"match": { "name": "led tv" }
|
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"resellers": {
|
|
|
|
"nested": {
|
|
|
|
"path": "resellers"
|
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"min_price": { "min": { "field": "resellers.price" } }
|
|
|
|
}
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
2020-07-20 15:59:00 -04:00
|
|
|
}
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2019-09-11 12:06:42 -04:00
|
|
|
// TEST[s/GET \/products\/_search/GET \/products\/_search\?filter_path=aggregations/]
|
|
|
|
// TEST[continued]
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2014-01-29 07:09:12 -05:00
|
|
|
As you can see above, the nested aggregation requires the `path` of the nested documents within the top level documents.
|
|
|
|
Then one can define any type of aggregation over these nested documents.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
|
|
|
Response:
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2013-11-24 06:13:08 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2017-08-30 06:11:10 -04:00
|
|
|
...
|
|
|
|
"aggregations": {
|
|
|
|
"resellers": {
|
2019-09-11 12:06:42 -04:00
|
|
|
"doc_count": 2,
|
2017-08-30 06:11:10 -04:00
|
|
|
"min_price": {
|
|
|
|
"value": 350
|
|
|
|
}
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
2017-08-30 06:11:10 -04:00
|
|
|
}
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-08-30 06:11:10 -04:00
|
|
|
// TESTRESPONSE[s/\.\.\.//]
|
|
|
|
// TESTRESPONSE[s/: [0-9]+/: $body.$_path/]
|