0
0
Elasticsearchquery~10 mins

Object and nested types in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object and nested types
Define mapping with object type
Store JSON document with nested objects
Query on object fields
Define mapping with nested type
Store JSON document with nested arrays
Query with nested query to match inner objects
Return matching documents
This flow shows how Elasticsearch handles object and nested types: defining mappings, storing documents, and querying them differently.
Execution Sample
Elasticsearch
PUT my_index
{
  "mappings": {
    "properties": {
      "user": { "type": "nested" }
    }
  }
}
Defines an index with a nested type field called 'user' to store arrays of objects.
Execution Table
StepActionInput/ConditionResult/Output
1Define mapping with object type{"properties":{"address":{"type":"object"}}}Mapping created with 'address' as object type
2Index document with nested JSON{"address":{"city":"NY","zip":"10001"}}Document stored with 'address' object
3Query on object fieldMatch 'address.city' = 'NY'Document returned if city matches
4Define mapping with nested type{"properties":{"user":{"type":"nested"}}}Mapping created with 'user' as nested type
5Index document with nested array{"user":[{"name":"Alice","age":30},{"name":"Bob","age":25}]}Document stored with nested 'user' array
6Query with nested queryMatch 'user.name' = 'Alice' and 'user.age' = 30Only documents with matching nested object returned
7Query with simple object queryMatch 'user.name' = 'Alice' and 'user.age' = 25Document returned incorrectly because fields from different nested objects are mixed
8ExitNo more queriesEnd of example
💡 Finished showing difference between object and nested types in mapping and querying
Variable Tracker
VariableStartAfter Step 2After Step 5Final
MappingEmpty{"address": object}{"address": object, "user": nested}{"address": object, "user": nested}
DocumentNone{"address":{"city":"NY","zip":"10001"}}{"address":{"city":"NY","zip":"10001"}, "user":[{"name":"Alice","age":30},{"name":"Bob","age":25}]}Same
Query ResultNoneDocument returned if city matchesDocument returned if nested user matchesDepends on query
Key Moments - 2 Insights
Why does a simple object query on nested fields sometimes return wrong results?
Because nested objects are stored separately, querying nested fields as simple objects can mix values from different nested objects, as shown in step 7 where the document is returned unexpectedly.
What is the main difference between object and nested types in Elasticsearch?
Object type stores JSON objects as a single document, while nested type stores each object in an array as a separate hidden document, enabling precise queries on inner objects (see steps 4-6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the nested type mapping defined?
AStep 2
BStep 4
CStep 6
DStep 1
💡 Hint
Check the 'Action' column for 'Define mapping with nested type' in the execution_table.
According to the variable tracker, what is the state of 'Document' after step 5?
ADocument with address and nested user array
BEmpty document
COnly address object stored
DDocument with only nested user array
💡 Hint
Look at the 'Document' row under 'After Step 5' in variable_tracker.
If you query 'user.name' = 'Alice' and 'user.age' = 25 with nested type, what happens according to the execution table?
ANo document is returned
BQuery causes error
CDocument is returned
DAll documents are returned
💡 Hint
See step 7 in execution_table where simple object query on nested fields returns the document.
Concept Snapshot
Elasticsearch Object and Nested Types:
- Object type stores JSON objects as single documents.
- Nested type stores arrays of objects as separate hidden docs.
- Nested queries allow precise matching inside arrays.
- Use nested type for arrays of objects needing exact inner matches.
- Object type is simpler but can mix inner object fields in queries.
Full Transcript
This visual execution shows how Elasticsearch handles object and nested types. First, we define a mapping with an object type field and store a document with nested JSON. Queries on object fields return documents if the field matches. Then, we define a mapping with a nested type field to store arrays of objects. Documents with nested arrays are stored, and nested queries allow matching specific inner objects precisely. Simple queries on nested fields without nested query fail to match correctly because nested objects are stored separately. The variable tracker shows how mappings and documents evolve. Key moments clarify common confusions about querying nested fields and the difference between object and nested types. The quiz tests understanding of mapping steps, document states, and query results. This helps beginners see step-by-step how Elasticsearch manages complex JSON structures.