Bird
0
0

Given this mapping with a nested field comments and this document:

medium📝 Predict Output Q13 of 15
Elasticsearch - Mappings and Data Types
Given this mapping with a nested field comments and this document:
{"title": "Post", "comments": [{"user": "alice", "message": "Hi"}, {"user": "bob", "message": "Hello"}]}

Which query will correctly find documents where comments.user is "alice" and comments.message is "Hello" in the same comment object?
A{ "query": { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.user": "alice" } }, { "match": { "comments.message": "Hello" } } ] } } } } }
B{ "query": { "bool": { "must": [ { "match": { "comments.user": "alice" } }, { "match": { "comments.message": "Hello" } } ] } } }
C{ "query": { "term": { "comments.user": "alice" } } }
D{ "query": { "nested": { "path": "comments", "query": { "term": { "comments.message": "Hi" } } } } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested query usage

    To search inside nested fields and keep field relationships, use a nested query with the correct path and combined conditions inside it.
  2. Step 2: Analyze options

    { "query": { "nested": { "path": "comments", "query": { "bool": { "must": [ { "match": { "comments.user": "alice" } }, { "match": { "comments.message": "Hello" } } ] } } } } } uses a nested query on "comments" with a bool must for both user "alice" and message "Hello" in the same nested object. { "query": { "bool": { "must": [ { "match": { "comments.user": "alice" } }, { "match": { "comments.message": "Hello" } } ] } } } searches flattened fields, losing the link. { "query": { "term": { "comments.user": "alice" } } } only matches user. { "query": { "nested": { "path": "comments", "query": { "term": { "comments.message": "Hi" } } } } } searches for message "Hi" only.
  3. Final Answer:

    Uses nested query to match both conditions in the same nested object. -> Option A
  4. Quick Check:

    Nested query with bool must inside = A [OK]
Quick Trick: Use nested query with bool must for multiple nested conditions [OK]
Common Mistakes:
MISTAKES
  • Using bool query without nested for nested fields
  • Searching fields separately losing object linkage
  • Using term query without nested path

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Elasticsearch Quizzes