0
0
Elasticsearchquery~20 mins

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

Choose your learning style9 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.