Bird
Raised Fist0
Elasticsearchquery~20 mins

Bool query (must, should, must_not, filter) 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 Bool query with must and should clauses

Given the following Elasticsearch bool query, what documents will match?

{
  "query": {
    "bool": {
      "must": [
        { "term": { "status": "active" } }
      ],
      "should": [
        { "term": { "role": "admin" } },
        { "term": { "role": "user" } }
      ],
      "minimum_should_match": 1
    }
  }
}

Assuming documents have fields status and role, which option best describes the matching documents?

Elasticsearch
{
  "query": {
    "bool": {
      "must": [
        { "term": { "status": "active" } }
      ],
      "should": [
        { "term": { "role": "admin" } },
        { "term": { "role": "user" } }
      ],
      "minimum_should_match": 1
    }
  }
}
ADocuments with status 'active' and role 'admin' and 'user' simultaneously.
BDocuments with role 'admin' or 'user' only, regardless of status.
CDocuments with status 'active' and role either 'admin' or 'user'.
DDocuments with status 'active' or role 'admin' or 'user'.
Attempts:
2 left
💡 Hint

Remember that must means all conditions must be true, and should means at least one should match if minimum_should_match is set.

❓ Predict Output
intermediate
2:00remaining
Effect of must_not clause in Bool query

What documents will match this Elasticsearch bool query?

{
  "query": {
    "bool": {
      "must": [
        { "term": { "category": "books" } }
      ],
      "must_not": [
        { "term": { "author": "unknown" } }
      ]
    }
  }
}
Elasticsearch
{
  "query": {
    "bool": {
      "must": [
        { "term": { "category": "books" } }
      ],
      "must_not": [
        { "term": { "author": "unknown" } }
      ]
    }
  }
}
ADocuments with category 'books' and author 'unknown' only.
BDocuments with author 'unknown' only.
CDocuments in category 'books' or author 'unknown'.
DDocuments in category 'books' excluding those with author 'unknown'.
Attempts:
2 left
💡 Hint

The must_not clause excludes documents matching its conditions.

🔧 Debug
advanced
2:00remaining
Identify the error in this Bool query with filter

What error will this Elasticsearch query produce?

{
  "query": {
    "bool": {
      "filter": {
        "term": { "status": "active" }
      },
      "must": [
        { "term": { "role": "admin" } }
      ]
    }
  }
}
Elasticsearch
{
  "query": {
    "bool": {
      "filter": {
        "term": { "status": "active" }
      },
      "must": [
        { "term": { "role": "admin" } }
      ]
    }
  }
}
ASyntax error because filter must be an array, not an object.
BNo error; query runs and matches documents with status 'active' and role 'admin'.
CRuntime error because 'filter' cannot be combined with 'must'.
DQuery returns no results because 'filter' overrides 'must'.
Attempts:
2 left
💡 Hint

Check the Elasticsearch documentation for filter clause format.

📝 Syntax
advanced
2:00remaining
Which Bool query syntax is correct?

Which of the following Bool queries is syntactically correct in Elasticsearch?

A{ "bool": { "must": [ { "term": { "field": "value" } } ] } }
B{ "bool": { "must": { "terms": { "field": ["value1", "value2"] } } } }
C{ "bool": { "must": { "term": { "field": "value" } } } }
D{ "bool": { "must": [ { "terms": { "field": "value1" } } ] } }
Attempts:
2 left
💡 Hint

The must clause expects an array of queries.

🚀 Application
expert
3:00remaining
Construct a Bool query to find documents with complex conditions

You want to find documents that meet all these conditions:

  • Have status equal to 'published'.
  • Either category is 'technology' or tags contain 'innovation'.
  • Must not have author equal to 'anonymous'.
  • Filter to only include documents with views greater than 1000.

Which Bool query JSON correctly implements these conditions?

A
{
  "bool": {
    "must": [
      { "term": { "status": "published" } },
      { "bool": {
          "should": [
            { "term": { "category": "technology" } },
            { "term": { "tags": "innovation" } }
          ],
          "minimum_should_match": 1
        }
      }
    ],
    "must_not": [
      { "term": { "author": "anonymous" } }
    ],
    "filter": [
      { "range": { "views": { "gt": 1000 } } }
    ]
  }
}
B
{
  "bool": {
    "must": [
      { "term": { "status": "published" } },
      { "term": { "category": "technology" } },
      { "term": { "tags": "innovation" } }
    ],
    "must_not": [
      { "term": { "author": "anonymous" } }
    ],
    "filter": {
      "range": { "views": { "gt": 1000 } }
    }
  }
}
C
{
  "bool": {
    "must": [
      { "term": { "status": "published" } }
    ],
    "should": [
      { "term": { "category": "technology" } },
      { "term": { "tags": "innovation" } }
    ],
    "must_not": [
      { "term": { "author": "anonymous" } }
    ],
    "filter": {
      "range": { "views": { "gt": 1000 } }
    },
    "minimum_should_match": 1
  }
}
D
{
  "bool": {
    "must": [
      { "term": { "status": "published" } },
      { "bool": {
          "must": [
            { "term": { "category": "technology" } },
            { "term": { "tags": "innovation" } }
          ]
        }
      }
    ],
    "must_not": [
      { "term": { "author": "anonymous" } }
    ],
    "filter": {
      "range": { "views": { "gt": 1000 } }
    }
  }
}
Attempts:
2 left
💡 Hint

Use should with minimum_should_match for OR conditions inside must.

Practice

(1/5)
1. Which clause in an Elasticsearch bool query is used to exclude documents that match a condition?
easy
A. must
B. must_not
C. should
D. filter

Solution

  1. Step 1: Understand bool query clauses

    The bool query has four main clauses: must (required), should (optional), must_not (exclude), and filter (limit without scoring).
  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 B
  4. Quick Check:

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

Solution

  1. Step 1: Recall bool must clause syntax

    The must clause expects an array of query objects inside the bool query.
  2. Step 2: Check each option's syntax

    "bool": { "must": [ { "match": { "field": "value" } } ] } correctly uses an array with one match query object. The other options use incorrect types or missing brackets.
  3. Final Answer:

    "bool": { "must": [ { "match": { "field": "value" } } ] } -> Option A
  4. Quick Check:

    Must clause = array of queries [OK]
Hint: Must clause requires an array of query objects [OK]
Common Mistakes:
  • Using object instead of array for must
  • Missing brackets around match query
  • Using parentheses instead of brackets
3. Given this bool query snippet, which documents will be matched?
{
  "bool": {
    "must": [{ "term": { "status": "active" }}],
    "should": [{ "term": { "role": "admin" }}],
    "must_not": [{ "term": { "banned": true }}]
  }
}
medium
A. Documents with status 'active' and role 'admin', excluding banned ones
B. Documents with status 'active' or role 'admin', including banned ones
C. Documents with role 'admin' only, excluding banned ones
D. Documents with status 'active', optionally role 'admin', excluding banned ones

Solution

  1. Step 1: Analyze must clause effect

    The must clause requires documents to have status: active.
  2. Step 2: Analyze should and must_not clauses

    The should clause means documents with role: admin are preferred but not required. The must_not clause excludes documents where banned is true.
  3. Final Answer:

    Documents with status 'active', optionally role 'admin', excluding banned ones -> Option D
  4. Quick Check:

    Must + optional should + exclude must_not = Documents with status 'active', optionally role 'admin', excluding banned ones [OK]
Hint: Must is required, should is optional, must_not excludes [OK]
Common Mistakes:
  • Thinking should is required
  • Ignoring must_not exclusion
  • Confusing must and should roles
4. Identify the error in this bool query:
{
  "bool": {
    "must": { "match": { "title": "Elasticsearch" } },
    "filter": { "term": { "status": "published" } },
    "must_not": [ { "term": { "archived": true } } ]
  }
}
medium
A. "must" should be an array, not an object
B. "filter" cannot be used with "must_not"
C. "must_not" should be an object, not an array
D. The query is correct as is

Solution

  1. Step 1: Check must clause format

    The must clause expects an array of query objects, but here it is given as a single object.
  2. Step 2: Validate other clauses

    The filter and must_not clauses are correctly formatted as an object and an array respectively.
  3. Final Answer:

    "must" should be an array, not an object -> Option A
  4. Quick Check:

    Must clause = array required [OK]
Hint: Must clause must always be an array [OK]
Common Mistakes:
  • Using object instead of array for must
  • Thinking filter and must_not conflict
  • Using array for must_not incorrectly
5. You want to find documents where the category is 'books' and the price is less than 20, but exclude those that are out of stock. Which bool query correctly implements this?
hard
A. { "bool": { "should": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": [ { "term": { "stock": "out" } } ] } }
B. { "bool": { "filter": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": { "term": { "stock": "out" } } } }
C. { "bool": { "must": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": [ { "term": { "stock": "out" } } ] } }
D. { "bool": { "must": { "term": { "category": "books" } }, "filter": { "range": { "price": { "lt": 20 } } }, "must_not": [ { "term": { "stock": "out" } } ] } }

Solution

  1. Step 1: Identify required conditions

    Documents must have category: books and price < 20. Both are required, so they go in must as an array.
  2. Step 2: Exclude out of stock documents

    Documents with stock: out must be excluded, so use must_not with an array of term queries.
  3. Step 3: Check each option

    { "bool": { "must": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": [ { "term": { "stock": "out" } } ] } } correctly uses must array for required conditions and must_not array for exclusion. { "bool": { "filter": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": { "term": { "stock": "out" } } } } uses filter which is okay but must_not is an object, not array. { "bool": { "should": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": [ { "term": { "stock": "out" } } ] } } uses should which makes conditions optional, wrong here. { "bool": { "must": { "term": { "category": "books" } }, "filter": { "range": { "price": { "lt": 20 } } }, "must_not": [ { "term": { "stock": "out" } } ] } } uses must as object and filter as object, mixing styles incorrectly.
  4. Final Answer:

    { "bool": { "must": [ { "term": { "category": "books" } }, { "range": { "price": { "lt": 20 } } } ], "must_not": [ { "term": { "stock": "out" } } ] } } -> Option C
  5. Quick Check:

    Must array + must_not array for exclusion [OK]
Hint: Use must array for required, must_not array to exclude [OK]
Common Mistakes:
  • Using should instead of must for required filters
  • Using object instead of array for must or must_not
  • Mixing filter and must incorrectly
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