📖 Scenario: You work with a bookstore database in Elasticsearch. Each book document has a nested field called authors that stores multiple authors with their name and age. You want to find books where at least one author is younger than 40.
🎯 Goal: Build an Elasticsearch query using nested queries to find books with authors younger than 40.
📋 What You'll Learn
Create an index mapping with a nested authors field
Add a config variable for the age threshold
Write a nested query to find books with authors younger than the threshold
Print the final query JSON
💡 Why This Matters
🌍 Real World
Nested queries are used when you have complex data with arrays of objects, like books with multiple authors, and you want to search inside those nested objects.
💼 Career
Many jobs working with Elasticsearch require building nested queries to filter or search data accurately inside nested fields.
Progress0 / 4 steps
1
Create index mapping with nested authors
Create a variable called mapping that holds the Elasticsearch index mapping JSON. It should define a books index with a authors field of type nested. Each author has name (text) and age (integer).
Elasticsearch
Hint
Use a dictionary with mappings and properties. The authors field must have "type": "nested".
2
Set age threshold variable
Create a variable called age_threshold and set it to 40. This will be used to find authors younger than this age.
Elasticsearch
Hint
Just create a variable age_threshold and assign the number 40.
3
Write nested query for authors younger than threshold
Create a variable called query that holds an Elasticsearch nested query. It should search the authors nested field for authors with age less than age_threshold. Use nested query with path set to authors and a range query on authors.age with lt set to age_threshold.
Elasticsearch
Hint
Use a dictionary with query key, inside it a nested query with path and a range query on authors.age.
4
Print the final query JSON
Write a print statement to display the query variable.
Elasticsearch
Hint
Use print(query) to show the query dictionary.
Practice
(1/5)
1. What is the main purpose of a nested query in Elasticsearch?
easy
A. To aggregate numeric values across documents
B. To search inside objects stored within other objects
C. To perform full-text search on string fields
D. To update documents in bulk
Solution
Step 1: Understand nested objects
Nested objects are objects stored inside other objects in Elasticsearch documents.
Step 2: Purpose of nested query
The nested query lets you search inside these nested objects specifically.
Final Answer:
To search inside objects stored within other objects -> Option B
Quick Check:
Nested query = search inside nested objects [OK]
Hint: Nested queries search inside nested objects only [OK]
Common Mistakes:
Confusing nested query with full-text search
Using nested query on non-nested fields
Thinking nested query updates documents
2. Which of the following is the correct syntax to specify the path in a nested query?
easy
A. "nested": { "path": "comments", "query": { ... } }
B. "nested": { "field": "comments", "query": { ... } }
C. "nested": { "location": "comments", "query": { ... } }
D. "nested": { "object": "comments", "query": { ... } }
Solution
Step 1: Identify the correct key for nested path
The nested query requires the key path to specify the nested object location.
A. The field "message" should be prefixed with the nested path "comments.message"
B. The "path" key is missing
C. The nested query cannot use match queries
D. The query should be inside a bool query
Solution
Step 1: Check field names in nested query
Fields inside nested queries must be fully qualified with the nested path prefix.
Step 2: Identify the missing prefix
The query uses "message" instead of "comments.message" which is incorrect.
Final Answer:
The field "message" should be prefixed with the nested path "comments.message" -> Option A
Quick Check:
Nested query fields need full path prefix [OK]
Hint: Prefix nested fields with path in queries [OK]
Common Mistakes:
Omitting nested path prefix in field names
Assuming nested query disallows match queries
Forgetting to include the "path" key
5. You have documents with a nested field reviews containing rating and comment. You want to find documents with at least one review having rating >= 4 and comment containing "excellent". Which nested query correctly achieves this?