What is a bool query in Elasticsearch and how do must, should, must_not, and filter work?
Learn the Elasticsearch bool query and how must, should, must_not and filter clauses combine scoring and non-scoring logic, with examples and interview tips.
Expected Interview Answer
A bool query is a compound Elasticsearch Query DSL query that combines several sub-queries with boolean logic using four clauses — must, should, must_not, and filter — to express complex matching and scoring rules.
The must clause requires every sub-query to match and contributes to the relevance score. The should clause is optional-OR: matches boost the score, and it becomes mandatory only when there is no must/filter (governed by minimum_should_match). The must_not clause excludes documents and runs in a non-scoring context, while filter also requires matches but in a non-scoring, cacheable context — ideal for exact yes/no criteria like dates, terms, and ranges. Combining scoring must/should with non-scoring filter/must_not is the standard pattern for fast, relevant search.
- Expresses complex AND/OR/NOT logic in one query
- filter and must_not skip scoring for speed and caching
- should clauses tune relevance without excluding results
- minimum_should_match gives fine control over optional matches
- Cleanly separates ranking signals from hard constraints
AI Mentor Explanation
Picking a cricket XI works like a bool query. 'Must' is a non-negotiable rule such as needing a wicketkeeper — drop it and the side is invalid. 'Filter' is a hard eligibility gate like being centrally contracted: pass or fail, no bonus for it. 'Should' is nice-to-have form, a recent century that nudges a player up the order. 'Must_not' is an outright ban, like anyone currently suspended. The selectors combine mandatory rules, gates, preferences, and exclusions exactly as the four clauses do.
Step-by-Step Explanation
Step 1
Start with the bool wrapper
Wrap your compound logic in a single bool query holding must, should, must_not, and filter arrays.
Step 2
Add hard requirements
Put scoring requirements in must (full-text matches) and non-scoring exact constraints in filter (terms, ranges, dates).
Step 3
Add preferences
Place optional boosting clauses in should; set minimum_should_match if you need at least some to match.
Step 4
Add exclusions
List documents to remove in must_not, which runs without contributing to the relevance score.
Step 5
Tune scoring vs speed
Move anything that is a yes/no constraint out of must into filter so Elasticsearch can cache it and skip scoring.
What Interviewer Expects
- Names all four clauses and whether each scores
- Knows filter and must_not are non-scoring (and filter is cacheable)
- Explains should as optional-OR governed by minimum_should_match
- Understands when should becomes mandatory (no must/filter present)
- Can articulate the filter-vs-must performance trade-off
Common Mistakes
- Putting exact term/range constraints in must instead of filter
- Thinking should always makes a clause optional even with no must
- Believing must_not contributes to relevance scoring
- Forgetting minimum_should_match defaults change with context
- Assuming filter affects ranking order
Best Answer (HR Friendly)
“A bool query is how Elasticsearch mixes several search conditions together. 'Must' means required, 'should' means preferred, 'must_not' means excluded, and 'filter' means a required yes/no check that runs faster because it skips relevance scoring.”
Code Example
GET /products/_search
{
"query": {
"bool": {
"must": [ { "match": { "title": "wireless headphones" } } ],
"filter": [ { "range": { "price": { "lte": 200 } } },
{ "term": { "in_stock": true } } ],
"should": [ { "match": { "brand": "acme" } } ],
"must_not": [ { "term": { "refurbished": true } } ],
"minimum_should_match": 0
}
}
}Follow-up Questions
- When does a should clause become mandatory?
- Why is filter faster than must for exact matches?
- How does the filter cache work in Elasticsearch?
- How would you boost specific documents within a bool query?
- What is the difference between a query context and a filter context?
MCQ Practice
1. Which bool clause requires a match but does NOT contribute to the relevance score?
filter requires the sub-query to match but runs in a non-scoring, cacheable context, making it ideal for exact constraints.
2. In a bool query with only should clauses (no must/filter), how many should clauses must match by default?
When there is no must or filter, minimum_should_match defaults to 1, so at least one should clause must match.
3. Which clause is used to exclude documents from the results?
must_not excludes matching documents and runs in a non-scoring context.
Flash Cards
What does the must clause do? — Requires every sub-query to match and contributes to the relevance score (AND, scoring).
What does the filter clause do? — Requires a match but in a non-scoring, cacheable context — great for exact yes/no constraints.
What does should do? — Optional-OR clauses that boost score; governed by minimum_should_match, mandatory only when no must/filter exists.
What does must_not do? — Excludes matching documents and does not affect the relevance score.
Continue Learning
Related Interview Questions
What is the difference between filter context and query context in Elasticsearch?
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
What is analysis in Elasticsearch and how do analyzers, tokenizers, and filters work?
medium