Skip to content

Lesson 4 of 9 · 25m

Query DSL Execution: Filter Context, Query Context, and shard Behavior

Read queries as execution plans, separate scoring from filtering, understand shard fan-out, and avoid expensive query shapes by design.

A search request is not a black box that returns ten blue links. It is a distributed execution plan: coordinating node parses it, shards execute it locally, results merge, and the final hits are fetched. Once you see that shape, performance incidents stop looking mysterious. The expensive parts are usually high-cardinality matching, scoring work that did not need to happen, deep pagination, and fan-out across too many shards.

Query context scores; filter context does not

This distinction is the first relevance and performance control:

{
  "query": {
    "bool": {
      "must": [
        {"match": {"message": "connection timeout"}}
      ],
      "filter": [
        {"term": {"service.raw": "api-gateway"}},
        {"range": {"@timestamp": {"gte": "now-24h"}}}
      ]
    }
  }
}

must contributes to relevance. filter answers yes/no and can often be cached. Putting exact constraints in must wastes scoring work and can distort ranking. Putting user text in filter destroys relevance.

Every query fans out

For an index with five primary shards, a normal search is sent to one copy of each shard. Each shard returns its best candidates to the coordinating node, which merges them. More shards mean more parallelism but also more coordination overhead. Tiny indexes split into many shards can be slower than one well-sized shard.

client -> coordinating node
             -> shard 1 local top hits
             -> shard 2 local top hits
             -> shard 3 local top hits
             -> merge -> fetch phase -> response

This is why shard sizing is a search-performance decision, not merely a storage decision.

Deep pagination is a trap

from + size looks simple and becomes expensive quickly. If from is 10,000 and size is 10, every shard must identify enough candidates for the coordinator to discard most of them. Use search_after for stable deep pagination:

{
  "size": 100,
  "sort": [
    {"@timestamp": "desc"},
    {"_id": "desc"}
  ],
  "search_after": [1764520000000, "abc123"],
  "query": {"match_all": {}}
}

Include a unique tie-breaker such as _id or another stable field. Timestamp-only sorting is ambiguous when many events share a millisecond.

Use profile before guessing

The Profile API shows where time is spent:

curl -s 'http://localhost:9200/logs-*/_search?pretty'   -H 'content-type: application/json' -d '{
    "profile": true,
    "query": {"match": {"message": "timeout"}}
  }'

Look at query type, rewrite behavior, per-shard timing, and collector cost. Profile on realistic data. An empty development index teaches almost nothing about production latency.

Field notes

  • Leading wildcards such as *timeout are notoriously expensive because they cannot use the normal term index efficiently.
  • match analyzes input; term does not. Using term against an analyzed text field is a classic no-results bug.
  • Preference and routing can reduce fan-out when data is co-located by tenant or time, but only if the indexing strategy actually supports it.
  • Cache behavior is a performance feature, not a correctness guarantee. Do not design around undocumented cache hits.

Was this lesson helpful?

Sign in to rate