Global aggregation
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.
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.
Example:
resp = client.search(
index="sales",
size="0",
query={
"match": {
"type": "t-shirt"
}
},
aggs={
"all_products": {
"global": {},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
},
"t_shirts": {
"avg": {
"field": "price"
}
}
},
)
print(resp) response = client.search(
index: 'sales',
size: 0,
body: {
query: {
match: {
type: 't-shirt'
}
},
aggregations: {
all_products: {
global: {},
aggregations: {
avg_price: {
avg: {
field: 'price'
}
}
}
},
t_shirts: {
avg: {
field: 'price'
}
}
}
}
)
puts response const response = await client.search({
index: "sales",
size: 0,
query: {
match: {
type: "t-shirt",
},
},
aggs: {
all_products: {
global: {},
aggs: {
avg_price: {
avg: {
field: "price",
},
},
},
},
t_shirts: {
avg: {
field: "price",
},
},
},
});
console.log(response); POST /sales/_search?size=0
{
"query": {
"match": { "type": "t-shirt" }
},
"aggs": {
"all_products": {
"global": {},
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
},
"t_shirts": { "avg": { "field": "price" } }
}
} | The | |
| The sub-aggregations that are registered for this |
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").
The response for the above aggregation:
{
...
"aggregations": {
"all_products": {
"doc_count": 7,
"avg_price": {
"value": 140.71428571428572
}
},
"t_shirts": {
"value": 128.33333333333334
}
}
} | The number of documents that were aggregated (in our case, all documents within the search context) | |
| The average price of all products in the index | |
| The average price of all t-shirts |
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/search-aggregations-bucket-global-aggregation.html