Aggregations & Buckets

Spatialized founder Jozef Sorocin
Jozef Soročin
Updated 07/13/2025

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.)
POST index_name/_search?size=10
{
	"size": 0,
  "query": {
    "bool": { ... }
  },
  "aggs": {
    "agg_name": {
      "agg_type": { ... }
    }
  }
}

Note that the size parameter was specified twice — once as a URI parameter and once as a request body parameter. It's important to know that the URI parameters always override the request body parameters!

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.

The query always affects both the hits and the aggregations.

The only exception to this rule is explained below.

Sometimes 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 →

Facets through global Aggregations
POST products/_doc
{
  "name": "Samsung QLED TV",
  "resolution": "8K"
}

POST products/_doc
{
  "name": "Samsung TV",
  "resolution": "4K"
}

POST products/_doc
{
  "name": "Samsung TV",
  "resolution": "FULL HD"
}

If we were to apply a must query and a standard terms aggregation:

Join 200+ developers who've mastered this! Get Complete Access — €19
Already a member? Sign in here