Bool query in depth in Elasticsearch - Time & Space Complexity
When using a bool query in Elasticsearch, it is important to understand how the query's execution time changes as the number of conditions grows.
We want to know how the cost of combining multiple queries with AND, OR, and NOT changes with more clauses.
Analyze the time complexity of the following bool query.
{
"query": {
"bool": {
"must": [
{ "match": { "field1": "value1" } },
{ "term": { "field2": "value2" } }
],
"should": [
{ "range": { "field3": { "gte": 10 } } }
],
"must_not": [
{ "exists": { "field": "field4" } }
]
}
}
}
This query combines multiple conditions using must (AND), should (OR), and must_not (NOT) clauses.
Look at how many times the query engine processes each clause.
- Primary operation: Evaluating each clause in the bool query.
- How many times: Once per clause inside must, should, and must_not arrays.
As you add more clauses, the query engine must check each one separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 clauses | 3 checks |
| 10 clauses | 10 checks |
| 100 clauses | 100 checks |
Pattern observation: The number of operations grows directly with the number of clauses.
Time Complexity: O(n)
This means the time to run the bool query grows linearly with the number of clauses.
[X] Wrong: "Adding more clauses won't affect query speed much because Elasticsearch is fast."
[OK] Correct: Each clause adds work, so more clauses mean more checks and longer execution time.
Understanding how bool queries scale helps you write efficient search queries and shows you can think about performance in real projects.
What if we replaced the must array with nested bool queries? How would that affect the time complexity?
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
