⚡ ElasticsearchBook is crafted by Jozef Sorocin (🟢 Book a consulting hour) and powered by:
As touched upon in 3. Tables & Charts, Elasticsearch supports:
returning only the original documents by setting size > 0
returning only the aggregations by setting size: 0
returning both the hits and the aggregations via size > 0 and specifying the aggs param aggs is not just an abbreviation — it's also a valid request body parameter interchangeable with aggregations. On the other hand, the ES response will always include the spelled-out word word "aggregations" never the "aggs" abbreviation.)
An example aggregation request would then look like this:
It's tempting to think that since the query and aggs parameters are logically and spatially separated, the query only applies to the hits. That's wrong.
<aside>
🔑 The query always affects both the hits and the aggregations.
</aside>
The only exception to this rule is explained below.
global AggregationsSometimes you want to get filtered hits but at the same time prevent the query from affecting the aggregations.
Take e-shop facets. Even when a customer looking for TVs chooses the option Ultra HD 8K, we'd want to keep showing the remaining options with correctly updated counts →

products index containing Samsung TVs:If we were to apply a must query and a standard terms aggregation:
POST products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "Samsung TV" } },
{ "term": { "resolution.keyword": { "value": "8K" } } }
]
}
},
"aggs": {
"by_resolution": {
"terms": {
"field": "resolution.keyword"
}
}
}
}