Bird
Raised Fist0
Elasticsearchquery~20 mins

Bool query in depth in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bool Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple bool query with must and filter
What is the output of the following Elasticsearch bool query when searching documents with fields status and age?
Elasticsearch
{
  "query": {
    "bool": {
      "must": [
        { "match": { "status": "active" } }
      ],
      "filter": [
        { "range": { "age": { "gte": 30 } } }
      ]
    }
  }
}
ADocuments where status is 'active' and age is 30 or more
BDocuments where status is 'active' but age is less than 30
CDocuments where status is 'active' or age is 30 or more
DDocuments where age is 30 or more only
Attempts:
2 left
💡 Hint
Remember that must means all conditions must match, and filter applies additional filtering without affecting scoring.
❓ Predict Output
intermediate
2:00remaining
Effect of must_not in bool query
Given this bool query, what documents will it return?
Elasticsearch
{
  "query": {
    "bool": {
      "must": [
        { "match": { "category": "books" } }
      ],
      "must_not": [
        { "term": { "author": "John Doe" } }
      ]
    }
  }
}
ADocuments not in category 'books' and not authored by 'John Doe'
BDocuments authored by 'John Doe' in any category
CDocuments in category 'books' authored by 'John Doe' only
DDocuments in category 'books' excluding those authored by 'John Doe'
Attempts:
2 left
💡 Hint
The must_not clause excludes documents matching its condition.
🔧 Debug
advanced
2:00remaining
Identify the error in this bool query
What error will this Elasticsearch bool query cause?
Elasticsearch
{
  "query": {
    "bool": {
      "must": {
        "match": { "title": "Elasticsearch" }
      },
      "filter": {
        "range": { "date": { "gte": "2023-01-01" } }
      },
      "must_not": [
        { "term": { "status": "archived" } }
      ]
    }
  }
}
ARuntimeError: 'range' query requires a 'lte' parameter
BNo error, query runs successfully
CSyntaxError: 'must' and 'filter' must be arrays, not objects
DTypeError: 'must_not' must be an object, not an array
Attempts:
2 left
💡 Hint
Check the expected data types for must and filter clauses.
🧠 Conceptual
advanced
2:00remaining
Understanding scoring in bool queries
Which clause in a bool query affects the relevance score of documents?
A<code>must</code> and <code>should</code> clauses affect scoring; <code>filter</code> does not
B<code>filter</code> clause affects scoring; <code>must</code> does not
COnly <code>must_not</code> affects scoring
DAll clauses affect scoring equally
Attempts:
2 left
💡 Hint
Think about which clauses contribute to relevance calculation.
🚀 Application
expert
3:00remaining
Compose a bool query for complex conditions
You want to find documents where:
- The field tags contains 'python' or 'elasticsearch'
- The field published is true
- The field views is greater than 1000
- The field author is NOT 'anonymous'

Which bool query correctly implements these conditions?
A
{
  "query": {
    "bool": {
      "should": [
        { "terms": { "tags": ["python", "elasticsearch"] } }
      ],
      "must": [
        { "term": { "published": true } },
        { "range": { "views": { "gt": 1000 } } }
      ],
      "must_not": [
        { "term": { "author": "anonymous" } }
      ]
    }
  }
}
B
{
  "query": {
    "bool": {
      "filter": [
        { "terms": { "tags": ["python", "elasticsearch"] } },
        { "term": { "published": true } },
        { "range": { "views": { "gt": 1000 } } }
      ],
      "must_not": [
        { "term": { "author": "anonymous" } }
      ]
    }
  }
}
C
{
  "query": {
    "bool": {
      "should": [
        { "term": { "tags": "python" } },
        { "term": { "tags": "elasticsearch" } }
      ],
      "filter": [
        { "term": { "published": true } },
        { "range": { "views": { "gt": 1000 } } }
      ],
      "must_not": [
        { "term": { "author": "anonymous" } }
      ]
    }
  }
}
D
{
  "query": {
    "bool": {
      "must": [
        { "term": { "tags": "python" } },
        { "term": { "tags": "elasticsearch" } },
        { "term": { "published": true } },
        { "range": { "views": { "gt": 1000 } } }
      ],
      "must_not": [
        { "term": { "author": "anonymous" } }
      ]
    }
  }
}
Attempts:
2 left
💡 Hint
Use terms query within filter or must for required OR conditions, must_not to exclude.

Practice

(1/5)
1. Which clause in an Elasticsearch bool query is used to exclude documents from the search results?
easy
A. must
B. should
C. must_not
D. filter

Solution

  1. Step 1: Understand bool query clauses

    The bool query has four main clauses: must (required matches), filter (fast filtering), should (optional matches), and must_not (exclusions).
  2. Step 2: Identify exclusion clause

    The must_not clause excludes documents that match its conditions from the results.
  3. Final Answer:

    must_not -> Option C
  4. Quick Check:

    Exclusion = must_not [OK]
Hint: Exclude with must_not clause in bool query [OK]
Common Mistakes:
  • Confusing must_not with should
  • Using filter to exclude documents
  • Thinking must excludes documents
2. Which of the following is the correct syntax to add a filter clause inside a bool query in Elasticsearch?
easy
A. "bool": { "must": [ { "term": { "status": "active" } } ] }
B. "bool": { "must_not": { "term": { "status": "active" } } }
C. "bool": { "should": { "term": { "status": "active" } } }
D. "bool": { "filter": [ { "term": { "status": "active" } } ] }

Solution

  1. Step 1: Recall bool query syntax for filter

    The filter clause expects an array of queries inside the bool query, e.g., "filter": [ { ... } ].
  2. Step 2: Match correct syntax

    "bool": { "filter": [ { "term": { "status": "active" } } ] } correctly uses "filter": [ { "term": { "status": "active" } } ] which is valid syntax.
  3. Final Answer:

    "bool": { "filter": [ { "term": { "status": "active" } } ] } -> Option D
  4. Quick Check:

    Filter clause uses array syntax [OK]
Hint: Filter clause always uses an array of queries [OK]
Common Mistakes:
  • Using object instead of array for filter
  • Confusing must_not or should syntax
  • Missing brackets around filter queries
3. Given this bool query snippet, what documents will it match?
{ "bool": { "must": [ { "match": { "title": "python" } } ], "should": [ { "term": { "tags": "programming" } } ], "must_not": [ { "term": { "status": "draft" } } ] } }
medium
A. Documents with title containing 'python', optionally tagged 'programming', excluding drafts
B. Documents tagged 'programming' only, ignoring title and status
C. Documents with status 'draft' and title 'python'
D. Documents with title 'python' or status 'draft'

Solution

  1. Step 1: Analyze must clause

    The must clause requires documents to have 'python' in the title.
  2. Step 2: Analyze should and must_not clauses

    The should clause boosts documents tagged 'programming' but is optional. The must_not clause excludes documents with status 'draft'.
  3. Final Answer:

    Documents with title containing 'python', optionally tagged 'programming', excluding drafts -> Option A
  4. Quick Check:

    Must + should + must_not = Documents with title containing 'python', optionally tagged 'programming', excluding drafts [OK]
Hint: Must is required, should is optional, must_not excludes [OK]
Common Mistakes:
  • Thinking should is required
  • Including documents with status 'draft'
  • Ignoring must clause
4. Identify the error in this bool query snippet:
{ "bool": { "must": { "term": { "user": "kimchy" } }, "filter": { "term": { "status": "active" } } } }
medium
A. Missing "should" clause
B. "must" and "filter" should be arrays, not single objects
C. Using "term" inside bool is invalid
D. The query is correct as is

Solution

  1. Step 1: Check bool clause types

    In bool queries, must and filter expect arrays of queries, not single objects.
  2. Step 2: Identify syntax error

    Here, both must and filter are single objects, which is invalid syntax.
  3. Final Answer:

    "must" and "filter" should be arrays, not single objects -> Option B
  4. Quick Check:

    Bool clauses require arrays [OK]
Hint: Bool clauses must and filter need arrays [OK]
Common Mistakes:
  • Using single object instead of array
  • Forgetting to wrap queries in []
  • Assuming should is mandatory
5. You want to find documents where the 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?
hard
A. { "bool": { "must": [ { "term": { "category": "books" } } ], "should": [ { "term": { "author": "Alice" } }, { "range": { "year": { "gt": 2015 } } } ], "must_not": [ { "term": { "status": "archived" } } ] } }
B. { "bool": { "filter": [ { "term": { "category": "books" } }, { "term": { "author": "Alice" } } ], "must_not": [ { "term": { "status": "archived" } } ], "should": [ { "range": { "year": { "gt": 2015 } } } ] } }
C. { "bool": { "must": [ { "term": { "category": "books" } }, { "term": { "author": "Alice" } } ], "must_not": [ { "term": { "status": "archived" } } ], "should": [ { "range": { "year": { "gt": 2015 } } } ] } }
D. { "bool": { "must": [ { "term": { "category": "books" } } ], "filter": [ { "term": { "author": "Alice" } }, { "range": { "year": { "gt": 2015 } } } ], "must_not": [ { "term": { "status": "archived" } } ] } }

Solution

  1. 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).
  2. Step 2: Exclude archived documents

    Use must_not to exclude documents with status 'archived'.
  3. Step 3: Match query structure

    { "bool": { "must": [ { "term": { "category": "books" } } ], "should": [ { "term": { "author": "Alice" } }, { "range": { "year": { "gt": 2015 } } } ], "must_not": [ { "term": { "status": "archived" } } ] } } correctly uses must for category, should for author or year, and must_not for exclusion.
  4. Final Answer:

    Option A's bool query structure -> Option A
  5. Quick Check:

    Must + should + must_not for conditions [OK]
Hint: Use must for required, should for alternatives, must_not to exclude [OK]
Common Mistakes:
  • Putting author and year in must (both required)
  • Using filter instead of should for alternatives
  • Missing must_not for exclusion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard