In Elasticsearch, query context asks "how well does this document match?" and produces a relevance score, while filter context asks "does this document match, yes or no?" and produces no score. The elasticsearch filter vs query context choice decides ranking, speed and caching: use query context for text the user typed, and filter context for exact conditions such as status, dates and categories.
The same query type can run in either context depending on where you place it. Understanding that placement is one of the most useful performance habits in the Query DSL.
Every clause in a search request runs in one of two contexts:
| Aspect | Query context | Filter context |
|---|---|---|
| Question answered | How well does the document match? | Does the document match? |
| Relevance score | Calculated and added to _score | Not calculated |
| Caching | Not cached | Eligible for the node query cache |
| Typical clauses | match, multi_match, query_string | term, range, exists, terms |
| Where it lives | Top-level query, must, should | filter, must_not, constant_score.filter |
A document either passes a filter or it does not. In query context, a document can match with a high or low score, and the score decides its position in the results.
The top-level query of a search request starts in query context. Inside a bool query, the clauses split like this:
must and should run in query context and add to the score.filter and must_not run in filter context and add nothing to the score.Other places that put a query into filter context are the filter parameter of a constant_score query, and the filter aggregation, which only decides which documents fall into a bucket.
The match on title is in query context, so documents that mention the phrase more often or in shorter fields rank higher. The status and published_at conditions are in filter context: they only remove documents that do not qualify, and every remaining document keeps the score from the match.
Elasticsearch scores full-text matches using BM25 by default, taking into account how often the terms appear in the document, how rare they are across the index and how long the field is. A filter has none of this work to do.
You can see the effect directly. Run the same search with the term condition in must and then in filter, and compare _score:
Every hit in this response has "_score": 0.0, because a bool query with only filter clauses has nothing to rank. If you need a constant non-zero score instead, wrap the filter in a constant_score query:
All matching documents get a score of exactly 1.2.
Because filters never need scores, Elasticsearch can skip scoring work and can store the set of matching documents for reuse. The node query cache keeps results for filter clauses that Lucene considers worth caching, typically ones used repeatedly across searches. Caching decisions are automatic and are mostly useful on segments that are large and not constantly changing.
Practical consequences:
status: published or a tenant id, gets cheap on repeat.now-based date range is different on every request if it uses fine-grained time, so rounding such as now/d helps it be reused.must to filter usually reduces CPU use and does not change which documents match.Do not assume a filter is always faster in absolute terms. For a tiny index, the difference is negligible. The habit still matters because it keeps scoring meaningful.
Use this checklist when you write a bool query:
must (or should), so it ranks.category, tenant_id, price range, or in_stock? Put it in filter.must_not, which is filter context.should with a boost, since a scored clause is the point.A common mistake is placing a range or term in must and then wondering why scores vary in odd ways. Those clauses still contribute small scores that can disturb ordering you expected to come only from text relevance. Moving them to filter fixes that.
Another subtle point is that the field type matters more than the context. A term query on an analysed text field does not match as you may expect, regardless of context. Use keyword fields for exact matching and text fields for full-text matching.
Usually, because no scoring is needed and results can be cached. The gain depends on index size and data, so measure with the _search profile API ("profile": true) when it matters.
Yes. A match clause placed inside filter runs without scoring, and it still analyses the text. It just does not rank.
Nothing in the query asked for relevance, so there is no score to compute. Use constant_score if you need a fixed value, or add a scored clause.
No. must_not runs in filter context, so it removes documents without changing the scores of the others.
Query context ranks documents by how well they match, and filter context only includes or excludes them. In a bool query, must and should run in query context while filter and must_not run in filter context. Put typed text in the scored clauses and exact conditions in filters, and you get meaningful ranking, less scoring work and better cache reuse.