A bool query helps you combine many search rules together. It lets you say what must happen, what should happen, and what must not happen in your search.
Bool query in depth in Elasticsearch
{
"bool": {
"must": [ /* queries that must match */ ],
"filter": [ /* queries that filter results but don't affect score */ ],
"should": [ /* queries that should match to boost score */ ],
"must_not": [ /* queries that must NOT match */ ]
}
}must means all these queries have to match.
filter is like must but faster and does not change score.
{
"bool": {
"must": [
{ "match": { "title": "apple" } }
]
}
}{
"bool": {
"must": [
{ "match": { "title": "apple" } }
],
"must_not": [
{ "term": { "status": "sold" } }
]
}
}{
"bool": {
"should": [
{ "match": { "description": "fresh" } },
{ "match": { "description": "organic" } }
],
"minimum_should_match": 1
}
}{
"bool": {
"filter": [
{ "range": { "price": { "gte": 10, "lte": 50 } } }
]
}
}This query finds documents that have "coffee" in the title and "fresh" in the description. It only includes items priced between 5 and 20. It excludes items with status "sold". If the brand is "acme", the document scores higher but it is not required.
{
"query": {
"bool": {
"must": [
{ "match": { "title": "coffee" } },
{ "match": { "description": "fresh" } }
],
"filter": [
{ "range": { "price": { "gte": 5, "lte": 20 } } }
],
"must_not": [
{ "term": { "status": "sold" } }
],
"should": [
{ "match": { "brand": "acme" } }
],
"minimum_should_match": 0
}
}
}You can use minimum_should_match to say how many should queries must match.
filter clauses are cached and faster for repeated queries.
Use must_not to exclude unwanted results.
A bool query combines many queries with rules: must, should, filter, and must_not.
Use must for required matches, filter for fast filtering, should for optional boosts, and must_not to exclude.
Bool queries help build powerful and flexible searches in Elasticsearch.
Practice
Solution
Step 1: Understand bool query clauses
The bool query has four main clauses:must(required matches),filter(fast filtering),should(optional matches), andmust_not(exclusions).Step 2: Identify exclusion clause
Themust_notclause excludes documents that match its conditions from the results.Final Answer:
must_not -> Option CQuick Check:
Exclusion = must_not [OK]
- Confusing must_not with should
- Using filter to exclude documents
- Thinking must excludes documents
filter clause inside a bool query in Elasticsearch?Solution
Step 1: Recall bool query syntax for filter
Thefilterclause expects an array of queries inside the bool query, e.g., "filter": [ { ... } ].Step 2: Match correct syntax
"bool": { "filter": [ { "term": { "status": "active" } } ] } correctly uses "filter": [ { "term": { "status": "active" } } ] which is valid syntax.Final Answer:
"bool": { "filter": [ { "term": { "status": "active" } } ] } -> Option DQuick Check:
Filter clause uses array syntax [OK]
- Using object instead of array for filter
- Confusing must_not or should syntax
- Missing brackets around filter queries
{ "bool": { "must": [ { "match": { "title": "python" } } ], "should": [ { "term": { "tags": "programming" } } ], "must_not": [ { "term": { "status": "draft" } } ] } }Solution
Step 1: Analyze must clause
Themustclause requires documents to have 'python' in the title.Step 2: Analyze should and must_not clauses
Theshouldclause boosts documents tagged 'programming' but is optional. Themust_notclause excludes documents with status 'draft'.Final Answer:
Documents with title containing 'python', optionally tagged 'programming', excluding drafts -> Option AQuick Check:
Must + should + must_not = Documents with title containing 'python', optionally tagged 'programming', excluding drafts [OK]
- Thinking should is required
- Including documents with status 'draft'
- Ignoring must clause
{ "bool": { "must": { "term": { "user": "kimchy" } }, "filter": { "term": { "status": "active" } } } }Solution
Step 1: Check bool clause types
In bool queries,mustandfilterexpect arrays of queries, not single objects.Step 2: Identify syntax error
Here, bothmustandfilterare single objects, which is invalid syntax.Final Answer:
"must" and "filter" should be arrays, not single objects -> Option BQuick Check:
Bool clauses require arrays [OK]
- Using single object instead of array
- Forgetting to wrap queries in []
- Assuming should is mandatory
category is 'books' and either the author is 'Alice' or the year is after 2015, but exclude any documents marked as archived. Which bool query structure correctly implements this?Solution
Step 1: Identify required and optional conditions
The category must be 'books' (required), and either author is 'Alice' or year > 2015 (optional, so use should).Step 2: Exclude archived documents
Usemust_notto exclude documents with status 'archived'.Step 3: Match query structure
{ "bool": { "must": [ { "term": { "category": "books" } } ], "should": [ { "term": { "author": "Alice" } }, { "range": { "year": { "gt": 2015 } } } ], "must_not": [ { "term": { "status": "archived" } } ] } } correctly usesmustfor category,shouldfor author or year, andmust_notfor exclusion.Final Answer:
Option A's bool query structure -> Option AQuick Check:
Must + should + must_not for conditions [OK]
- Putting author and year in must (both required)
- Using filter instead of should for alternatives
- Missing must_not for exclusion
Ready to prove it?
Pass the test and this pattern moves unknown
