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
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2019-01-18 03:34:11 -05:00
|
|
|
PUT /index
|
2013-11-24 06:13:08 -05:00
|
|
|
{
|
2019-01-18 03:34:11 -05:00
|
|
|
"mappings": {
|
2013-11-24 06:13:08 -05:00
|
|
|
"properties" : {
|
|
|
|
"resellers" : { <1>
|
2014-11-27 05:03:19 -05:00
|
|
|
"type" : "nested",
|
2013-11-24 06:13:08 -05:00
|
|
|
"properties" : {
|
2016-03-18 12:01:27 -04:00
|
|
|
"name" : { "type" : "text" },
|
2013-11-24 06:13:08 -05:00
|
|
|
"price" : { "type" : "double" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-08-30 06:11:10 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
2013-11-24 06:13:08 -05:00
|
|
|
<1> The `resellers` is an array that holds nested documents under the `product` object.
|
|
|
|
|
|
|
|
The following aggregations will return the minimum price products can be purchased in:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2017-08-30 06:11:10 -04:00
|
|
|
GET /_search
|
2013-11-24 06:13:08 -05:00
|
|
|
{
|
|
|
|
"query" : {
|
|
|
|
"match" : { "name" : "led tv" }
|
2014-06-10 06:40:42 -04:00
|
|
|
},
|
2013-11-24 06:13:08 -05:00
|
|
|
"aggs" : {
|
|
|
|
"resellers" : {
|
|
|
|
"nested" : {
|
|
|
|
"path" : "resellers"
|
|
|
|
},
|
|
|
|
"aggs" : {
|
2014-02-13 14:33:35 -05:00
|
|
|
"min_price" : { "min" : { "field" : "resellers.price" } }
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-08-30 06:11:10 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
|
2019-01-18 03:34:11 -05:00
|
|
|
// TEST[s/^/PUT index\/_doc\/0\?refresh\n{"name":"led", "resellers": [{"name": "foo", "price": 350.00}, {"name": "bar", "price": 500.00}]}\n/]
|
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:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2017-08-30 06:11:10 -04:00
|
|
|
...
|
|
|
|
"aggregations": {
|
|
|
|
"resellers": {
|
|
|
|
"doc_count": 0,
|
|
|
|
"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/]
|