A bool query helps you combine many search rules in Elasticsearch. It lets you say what must happen, what should happen, what must not happen, and what to filter.
Bool query (must, should, must_not, filter) in Elasticsearch
{
"bool": {
"must": [ { /* conditions that must match */ } ],
"should": [ { /* conditions that should match */ } ],
"must_not": [ { /* conditions that must NOT match */ } ],
"filter": [ { /* conditions to filter without scoring */ } ]
}
}Each part (must, should, must_not, filter) takes an array of queries.
Must means all these queries have to match.
{
"bool": {
"must": [ { "match": { "title": "apple" } } ]
}
}{
"bool": {
"should": [ { "term": { "color": "red" } }, { "term": { "color": "green" } } ]
}
}{
"bool": {
"must_not": [ { "term": { "status": "sold" } } ]
}
}{
"bool": {
"filter": [ { "range": { "price": { "lte": 100 } } } ]
}
}This query finds documents that must mention "fresh fruit" in description, should be red or green color, must not be expired, and price must be 50 or less.
{
"query": {
"bool": {
"must": [
{ "match": { "description": "fresh fruit" } }
],
"should": [
{ "term": { "color": "red" } },
{ "term": { "color": "green" } }
],
"must_not": [
{ "term": { "status": "expired" } }
],
"filter": [
{ "range": { "price": { "lte": 50 } } }
]
}
}
}Use must for required matches that affect scoring.
Use filter for conditions that don't affect scoring but limit results.
should clauses boost relevance if matched but are optional unless combined with minimum_should_match.
Bool query combines multiple conditions with must, should, must_not, and filter.
Must means required, should means optional but preferred, must_not excludes, filter limits without scoring.
This helps build flexible and powerful search queries in Elasticsearch.
Practice
Solution
Step 1: Understand bool query clauses
The bool query has four main clauses:must(required),should(optional),must_not(exclude), andfilter(limit without scoring).Step 2: Identify exclusion clause
Themust_notclause excludes documents that match its conditions from the results.Final Answer:
must_not-> Option BQuick Check:
Exclude = must_not [OK]
- Confusing must_not with must
- Using should to exclude documents
- Thinking filter excludes documents
must clause inside a bool query in Elasticsearch?Solution
Step 1: Recall bool must clause syntax
Themustclause expects an array of query objects inside the bool query.Step 2: Check each option's syntax
"bool": { "must": [ { "match": { "field": "value" } } ] } correctly uses an array with onematchquery object. The other options use incorrect types or missing brackets.Final Answer:
"bool": { "must": [ { "match": { "field": "value" } } ] } -> Option AQuick Check:
Must clause = array of queries [OK]
- Using object instead of array for must
- Missing brackets around match query
- Using parentheses instead of brackets
{
"bool": {
"must": [{ "term": { "status": "active" }}],
"should": [{ "term": { "role": "admin" }}],
"must_not": [{ "term": { "banned": true }}]
}
}Solution
Step 1: Analyze must clause effect
Themustclause requires documents to havestatus: active.Step 2: Analyze should and must_not clauses
Theshouldclause means documents withrole: adminare preferred but not required. Themust_notclause excludes documents wherebannedis true.Final Answer:
Documents with status 'active', optionally role 'admin', excluding banned ones -> Option DQuick Check:
Must + optional should + exclude must_not = Documents with status 'active', optionally role 'admin', excluding banned ones [OK]
- Thinking should is required
- Ignoring must_not exclusion
- Confusing must and should roles
{
"bool": {
"must": { "match": { "title": "Elasticsearch" } },
"filter": { "term": { "status": "published" } },
"must_not": [ { "term": { "archived": true } } ]
}
}Solution
Step 1: Check must clause format
Themustclause expects an array of query objects, but here it is given as a single object.Step 2: Validate other clauses
Thefilterandmust_notclauses are correctly formatted as an object and an array respectively.Final Answer:
"must" should be an array, not an object -> Option AQuick Check:
Must clause = array required [OK]
- Using object instead of array for must
- Thinking filter and must_not conflict
- Using array for must_not incorrectly
category is 'books' and the price is less than 20, but exclude those that are out of stock. Which bool query correctly implements this?Solution
Step 1: Identify required conditions
Documents must havecategory: booksandprice < 20. Both are required, so they go inmustas an array.Step 2: Exclude out of stock documents
Documents withstock: outmust be excluded, so usemust_notwith an array of term queries.Step 3: Check each option
{ "bool": { "must": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": [ { "term": { "stock": "out" } } ] } } correctly usesmustarray for required conditions andmust_notarray for exclusion. { "bool": { "filter": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": { "term": { "stock": "out" } } } } usesfilterwhich is okay butmust_notis an object, not array. { "bool": { "should": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": [ { "term": { "stock": "out" } } ] } } usesshouldwhich makes conditions optional, wrong here. { "bool": { "must": { "term": { "category": "books" } }, "filter": { "range": { "price": { "lt": 20 } } }, "must_not": [ { "term": { "stock": "out" } } ] } } usesmustas object andfilteras object, mixing styles incorrectly.Final Answer:
{ "bool": { "must": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": [ { "term": { "stock": "out" } } ] } } -> Option CQuick Check:
Must array + must_not array for exclusion [OK]
- Using should instead of must for required filters
- Using object instead of array for must or must_not
- Mixing filter and must incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
