🏡 Home 👈 Prev 👉 Next

⚡  ElasticsearchBook.com is crafted by Jozef Sorocin and powered by:

Request Options

As touched upon in 3. Tables & Charts, Elasticsearch supports:

Search Contexts

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.

Facets through global Aggregations

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 →

https://i.stack.imgur.com/02MKg.png

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"
      }
    }
  }
}