0
0
Elasticsearchquery~20 mins

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