What is the difference between filter context and query context in Elasticsearch?
Understand filter context vs query context in Elasticsearch: scoring, caching, bool query clauses, constant_score, and when to use each for fast search.
Expected Interview Answer
Query context answers 'how well does this document match?' and produces a relevance score, while filter context answers 'does this document match, yes or no?' with no scoring and cacheable results.
In query context (for example the 'must' or 'should' clauses of a bool query), Elasticsearch computes a _score using BM25 to rank results by relevance. In filter context (the 'filter' and 'must_not' clauses, or a constant_score query), the clause only decides inclusion — every match is equal, no score is calculated, and Elasticsearch can cache the result set in the node query cache for fast reuse. Because filters are cheaper and cacheable, you put exact, binary conditions like status, date ranges, and term matches in filter context, and reserve query context for the free-text parts where ranking matters.
- Filters skip scoring, so they run faster
- Filter results are cached and reused across queries
- Query context ranks documents by relevance
- Combining both keeps searches both accurate and fast
- Reduces load for high-volume exact-match conditions
AI Mentor Explanation
Query context is like a selector rating each player on current form to rank who most deserves a place, producing a fine-grained score. Filter context is like the eligibility rule that a player must hold a valid contract with the board — it is a plain yes-or-no gate, the same for everyone who passes, and the board can keep that eligibility list cached rather than re-checking it every match.
Step-by-Step Explanation
Step 1
Identify binary conditions
Find clauses that are exact yes/no: status, dates, ownership, term matches.
Step 2
Place them in filter
Put those clauses in the bool query's 'filter' or 'must_not' array so they skip scoring.
Step 3
Keep ranking clauses in query
Put free-text 'match' clauses in 'must' or 'should' so BM25 scores relevance.
Step 4
Combine in a bool query
Use one bool query mixing must (scored) and filter (unscored) clauses together.
Step 5
Benefit from caching
Reusable filters are cached in the node query cache, speeding up repeated searches.
What Interviewer Expects
- Knowing query context computes _score and filter context does not
- Understanding that filters are cacheable and cheaper
- Which bool clauses map to which context (must/should vs filter/must_not)
- When to prefer filter over query for exact conditions
- Awareness of constant_score for scoreless matching
Common Mistakes
- Putting exact term conditions in must and paying scoring cost needlessly
- Assuming filter context affects relevance ranking
- Thinking must_not contributes to the score
- Not knowing filter results are cached
- Confusing filter context with the deprecated filtered query
Best Answer (HR Friendly)
“In Elasticsearch, query context ranks results by how relevant they are and gives each a score, while filter context just checks whether a document qualifies with a plain yes or no. Filters are faster and reusable, so exact conditions like a status or date go there, and the free-text search that needs ranking stays in query context.”
Code Example
GET /articles/_search
{
"query": {
"bool": {
"must": [
{ "match": { "body": "machine learning" } }
],
"filter": [
{ "term": { "status": "published" } },
{ "range": { "published_at": { "gte": "2024-01-01" } } }
]
}
}
}GET /articles/_search
{
"query": {
"constant_score": {
"filter": { "term": { "category": "news" } }
}
}
}Follow-up Questions
- Which bool clauses run in filter context?
- How does the node query cache decide what to cache?
- What does constant_score do?
- Does must_not affect the relevance score?
- When would you move a match clause into filter context?
MCQ Practice
1. Which bool clause runs in query context and contributes to _score?
must (and should) clauses run in query context and contribute to the relevance _score; filter and must_not do not.
2. Why place an exact status match in filter context?
Filter context does not compute a score and its results are cacheable, making exact conditions faster.
3. What does a constant_score query do?
constant_score executes its inner filter in filter context and assigns a uniform score to all matches.
Flash Cards
Does filter context compute a score? — No — it only decides inclusion (yes/no) and results are cacheable.
Does query context compute a score? — Yes — clauses like must and should produce a BM25 _score for ranking.
Which clauses are filter context? — The bool query's filter and must_not arrays, and constant_score.
Why are filters fast? — They skip relevance scoring and their bitset results are cached in the node query cache.
Continue Learning
Related Interview Questions
What is a bool query in Elasticsearch and how do must, should, must_not, and filter work?
medium
How does Elasticsearch handle full-text search vs exact matching?
medium
What is the difference between a term query and a match query in Elasticsearch?
medium
What is the difference between keyword and text field types in Elasticsearch?
medium