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.
|
|
|
|
|
2014-01-29 07:09:12 -05:00
|
|
|
For example, lets say we have a index of products, and each product holds the list of resellers - each having its own
|
|
|
|
price for the product. The mapping could look like:
|
2013-11-24 06:13:08 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
...
|
|
|
|
|
|
|
|
"product" : {
|
|
|
|
"properties" : {
|
|
|
|
"resellers" : { <1>
|
|
|
|
"type" : "nested"
|
|
|
|
"properties" : {
|
|
|
|
"name" : { "type" : "string" },
|
|
|
|
"price" : { "type" : "double" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
<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]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"query" : {
|
|
|
|
"match" : { "name" : "led tv" }
|
|
|
|
}
|
|
|
|
"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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
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]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"aggregations": {
|
|
|
|
"resellers": {
|
2014-01-29 07:09:12 -05:00
|
|
|
"min_price": {
|
|
|
|
"value" : 350
|
|
|
|
}
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|