2013-11-24 06:13:08 -05:00
|
|
|
[[search-aggregations-bucket-global-aggregation]]
|
2014-05-12 19:35:58 -04:00
|
|
|
=== Global Aggregation
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2017-01-20 14:26:10 -05:00
|
|
|
Defines a single bucket of all the documents within the search execution
|
|
|
|
context. This context is defined by the indices and the document types you're
|
|
|
|
searching on, but is *not* influenced by the search query itself.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2017-01-20 14:26:10 -05:00
|
|
|
NOTE: Global aggregators can only be placed as top level aggregators because
|
|
|
|
it doesn't make sense to embed a global aggregator within another
|
|
|
|
bucket aggregator.
|
2013-11-24 06:13:08 -05:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2019-09-05 10:11:25 -04:00
|
|
|
[source,console]
|
2013-11-24 06:13:08 -05:00
|
|
|
--------------------------------------------------
|
2017-01-20 14:26:10 -05:00
|
|
|
POST /sales/_search?size=0
|
2013-11-24 06:13:08 -05:00
|
|
|
{
|
|
|
|
"query" : {
|
2017-01-20 14:26:10 -05:00
|
|
|
"match" : { "type" : "t-shirt" }
|
2013-11-24 06:13:08 -05:00
|
|
|
},
|
|
|
|
"aggs" : {
|
|
|
|
"all_products" : {
|
|
|
|
"global" : {}, <1>
|
|
|
|
"aggs" : { <2>
|
|
|
|
"avg_price" : { "avg" : { "field" : "price" } }
|
|
|
|
}
|
2017-01-20 14:26:10 -05:00
|
|
|
},
|
|
|
|
"t_shirts": { "avg" : { "field" : "price" } }
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-01-20 14:26:10 -05:00
|
|
|
// TEST[setup:sales]
|
2013-11-24 06:13:08 -05:00
|
|
|
|
|
|
|
<1> The `global` aggregation has an empty body
|
|
|
|
<2> The sub-aggregations that are registered for this `global` aggregation
|
|
|
|
|
2017-01-20 14:26:10 -05:00
|
|
|
The above aggregation demonstrates how one would compute aggregations
|
|
|
|
(`avg_price` in this example) on all the documents in the search context,
|
|
|
|
regardless of the query (in our example, it will compute the average price over
|
|
|
|
all products in our catalog, not just on the "shirts").
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2015-05-05 04:03:15 -04:00
|
|
|
The response for the above aggregation:
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2013-11-24 06:13:08 -05:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
...
|
|
|
|
"aggregations" : {
|
|
|
|
"all_products" : {
|
2017-01-20 14:26:10 -05:00
|
|
|
"doc_count" : 7, <1>
|
2014-05-12 19:35:58 -04:00
|
|
|
"avg_price" : {
|
2017-01-20 14:26:10 -05:00
|
|
|
"value" : 140.71428571428572 <2>
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
2017-01-20 14:26:10 -05:00
|
|
|
},
|
|
|
|
"t_shirts": {
|
|
|
|
"value" : 128.33333333333334 <3>
|
2013-11-24 06:13:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-01-20 14:26:10 -05:00
|
|
|
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
|
2013-11-24 06:13:08 -05:00
|
|
|
|
2017-01-20 14:26:10 -05:00
|
|
|
<1> The number of documents that were aggregated (in our case, all documents
|
|
|
|
within the search context)
|
|
|
|
<2> The average price of all products in the index
|
|
|
|
<3> The average price of all t-shirts
|