0
0
Elasticsearchquery~20 mins

Nested queries for nested objects in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a nested query filtering by nested field
Given the following Elasticsearch nested query, what is the expected output when searching an index with documents containing nested 'comments' objects?

{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            { "match": { "comments.author": "alice" } },
            { "range": { "comments.likes": { "gte": 10 } } }
          ]
        }
      }
    }
  }
}

Assuming the index has documents with nested comments by various authors with different likes, which option best describes the result?
ADocuments that have comments with author 'alice' and comments with likes >= 10, but not necessarily in the same comment.
BDocuments that have at least one comment by 'alice' with 10 or more likes.
CDocuments that have all comments by 'alice' with 10 or more likes.
DDocuments that have any comment by 'alice' or any comment with 10 or more likes.
Attempts:
2 left
💡 Hint
Remember that nested queries match nested objects individually, not across different nested objects.
🧠 Conceptual
intermediate
1:30remaining
Understanding the purpose of nested queries
Why do we use nested queries in Elasticsearch when dealing with nested objects instead of regular queries?
ABecause nested queries allow matching conditions on the same nested object, preserving object relationships.
BBecause nested queries improve performance by indexing nested objects separately.
CBecause nested queries automatically flatten nested objects into the root document.
DBecause nested queries are required to search any field in Elasticsearch.
Attempts:
2 left
💡 Hint
Think about how nested objects are stored and how queries match them.
🔧 Debug
advanced
2:00remaining
Identify the error in this nested query
What error will this Elasticsearch query produce?

{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "match": {
          "author": "bob"
        }
      }
    }
  }
}

Assuming 'comments' is a nested field, what is wrong?
AIt will cause a type mismatch error because 'author' is not a string field.
BIt will run correctly and return documents with comments by 'bob'.
CIt will cause a syntax error due to missing closing braces.
DIt will cause a 'field [author] not found' error because the field path is incomplete inside nested query.
Attempts:
2 left
💡 Hint
Check the field name used inside the nested query's match clause.
📝 Syntax
advanced
2:00remaining
Correct syntax for nested query with inner hits
Which of the following nested queries correctly includes inner hits to return matching nested objects?
A{ "nested": { "path": "comments", "query": { "match": { "comments.text": "great" } }, "inner_hits": {} } }
B{ "nested": { "path": "comments", "inner_hits": {}, "query": { "match": { "comments.text": "great" } } } }
C{ "nested": { "query": { "match": { "comments.text": "great" } }, "path": "comments" } }
D{ "nested": { "path": "comments", "query": { "match": { "text": "great" } }, "inner_hits": {} } }
Attempts:
2 left
💡 Hint
Check the order and presence of required keys and correct field names.
🚀 Application
expert
3:00remaining
Construct a nested query to find documents with nested comments by 'eve' having likes between 5 and 15
You want to find documents where at least one nested comment has author 'eve' and likes between 5 and 15 inclusive. Which query correctly implements this?
A{ "query": { "nested": { "path": "comments", "query": { "match": { "comments.author": "eve" } }, "filter": { "range": { "comments.likes": { "gte": 5, "lte": 15 } } } } } }
B{ "query": { "bool": { "must": [ { "match": { "comments.author": "eve" } }, { "range": { "comments.likes": { "gte": 5, "lte": 15 } } } ] } } }
C{ "query": { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.author": "eve" } }, { "range": { "comments.likes": { "gte": 5, "lte": 15 } } } ] } } } } }
D{ "query": { "nested": { "path": "comments", "query": { "bool": { "should": [ { "match": { "comments.author": "eve" } }, { "range": { "comments.likes": { "gte": 5, "lte": 15 } } } ] } } } } }
Attempts:
2 left
💡 Hint
Use a bool must inside nested query to combine conditions on the same nested object.