How does Elasticsearch handle nested objects and the nested data type?
Learn how Elasticsearch flattens object arrays, why it breaks relationships, and how the nested data type and nested queries preserve them accurately.
Expected Interview Answer
By default Elasticsearch flattens an array of objects, storing each field's values as separate parallel arrays, which loses the association between fields within the same object. The nested data type fixes this by indexing each object in the array as a hidden, separate Lucene document, preserving the relationships so queries can match conditions on the same sub-object using a nested query.
With the default object type, a field like users: [{first: 'Alice', last: 'Smith'}, {first: 'John', last: 'White'}] becomes users.first: ['Alice','John'] and users.last: ['Smith','White'], so a search for first=Alice AND last=White wrongly matches because the cross-object link is gone. Mapping users as nested stores each element as its own internal document, and a nested query scopes conditions to a single element. The trade-off is cost: nested documents multiply the underlying Lucene doc count, updates reindex the whole parent, and you need nested aggregations and inner_hits to work with them.
- Preserves field relationships within each array object
- Prevents false cross-object matches from flattening
- nested queries scope conditions to one sub-document
- inner_hits returns exactly which nested elements matched
- nested aggregations analyse sub-objects accurately
AI Mentor Explanation
Flattening an array is like listing all batters' names in one column and all their scores in another, then losing which score belongs to whom — you might credit a tail-ender with an opener's century. The nested type keeps each player's name and score stapled together on one card, so a query for 'this batter AND this score' only matches when both belong to the same person, not two mixed-up records.
Step-by-Step Explanation
Step 1
See the flattening default
An array of objects mapped as the default object type stores each field as a parallel array, breaking the link between fields of the same element.
Step 2
Map the field as nested
Declare the array field with "type": "nested" so each element is indexed as its own hidden Lucene document.
Step 3
Query with nested
Use a nested query with the field path so conditions are scoped to a single sub-document, not the flattened arrays.
Step 4
Return matches with inner_hits
Add inner_hits to the nested query to know exactly which array elements satisfied the conditions.
Step 5
Aggregate with nested aggs
Wrap metrics in a nested aggregation to analyse sub-objects correctly instead of the flattened values.
What Interviewer Expects
- Explains array-of-objects flattening and the lost associations
- Knows nested type indexes each element as a separate document
- Can write a nested query scoped to a field path
- Mentions inner_hits and nested aggregations
- Acknowledges the cost: doc multiplication and whole-parent updates
Common Mistakes
- Assuming the default object type preserves per-object relationships
- Querying nested fields with a normal bool query and getting false matches
- Ignoring the doc-count and performance cost of nested mappings
- Forgetting inner_hits, so you cannot tell which element matched
- Overusing nested for data that could be denormalized or a parent-child join
Best Answer (HR Friendly)
“Elasticsearch normally splits the fields of items in a list apart, which can accidentally mix details from different items. The nested type keeps each item's fields together as its own mini record, so searches that combine conditions only match when everything belongs to the same item.”
Code Example
PUT /people
{
"mappings": {
"properties": {
"users": { "type": "nested" }
}
}
}
GET /people/_search
{
"query": {
"nested": {
"path": "users",
"query": {
"bool": {
"must": [
{ "match": { "users.first": "Alice" } },
{ "match": { "users.last": "Smith" } }
]
}
},
"inner_hits": {}
}
}
}Follow-up Questions
- What is the performance cost of the nested data type?
- How do nested queries differ from parent-child (join) relationships?
- What does inner_hits return and why is it useful?
- How does index.mapping.nested_objects.limit protect a cluster?
- When would you denormalize instead of using nested?
MCQ Practice
1. What happens to an array of objects mapped with the default object type?
The default object type flattens each field into a parallel array, so the association between fields of the same element is lost.
2. How does the nested data type preserve relationships?
Each nested element is indexed as its own internal document, so a nested query can match conditions within a single sub-object.
3. Which feature tells you which nested elements matched?
inner_hits returns the specific nested sub-documents that satisfied the nested query's conditions.
Flash Cards
What does default object flattening do? — Stores each field of an object array as a parallel array, losing which values belong to the same element.
What does the nested type do? — Indexes each array element as a hidden separate Lucene document, preserving intra-object relationships.
What is inner_hits? — An option on nested queries that returns exactly which nested sub-documents matched.
Main cost of nested? — Doc-count multiplication and whole-parent reindex on any nested field update.
Continue Learning
Related Interview Questions
What is mapping in Elasticsearch and why does it matter?
medium
What is an alias in Elasticsearch and how does it help with reindexing?
medium
What is the difference between a term query and a match query in Elasticsearch?
medium
What is analysis in Elasticsearch and how do analyzers, tokenizers, and filters work?
medium