0
0
Elasticsearchquery~10 mins

Dynamic vs explicit mapping in Elasticsearch - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Dynamic vs explicit mapping
Start: Indexing document
Check mapping type
Dynamic mapping
Infer field types
Add fields to mapping
Store document in index
End
When a document is indexed, Elasticsearch either infers field types dynamically or uses explicit predefined mappings before storing the document.
Execution Sample
Elasticsearch
{
  "mappings": {
    "properties": {
      "name": {"type": "text"},
      "age": {"type": "integer"}
    }
  }
}

// Index document with fields name and age
This example shows explicit mapping where field types are defined before indexing documents.
Execution Table
StepActionMapping TypeField ProcessedMapping UpdateResult
1Index document {"name": "Alice", "age": 30}ExplicitnameExists as textField accepted as text
2Index document {"name": "Alice", "age": 30}ExplicitageExists as integerField accepted as integer
3Index document {"city": "Paris"}ExplicitcityNot in mappingRejected or error (depends on settings)
4Index document {"name": "Bob", "age": 25, "city": "Paris"}DynamicnameExists as textField accepted as text
5Index document {"name": "Bob", "age": 25, "city": "Paris"}DynamicageExists as integerField accepted as integer
6Index document {"name": "Bob", "age": 25, "city": "Paris"}DynamiccityInferred as textField added dynamically
7Index document {"name": "Charlie", "age": "thirty"}DynamicageInferred as integer beforeType conflict error or dynamic mapping update
8Stop---No more documents to index
💡 Execution stops when no more documents are indexed or mapping conflicts occur.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
Mapping fields{}{"name": "text"}{"name": "text", "age": "integer"}{"name": "text", "age": "integer"}{"name": "text", "age": "integer", "city": "text"}{"name": "text", "age": "integer", "city": "text"}{"name": "text", "age": "integer", "city": "text"}{"name": "text", "age": "integer", "city": "text"}
Document indexedNone{"name": "Alice", "age": 30}{"name": "Alice", "age": 30}{"city": "Paris"} (rejected in explicit){"name": "Bob", "age": 25, "city": "Paris"}{"name": "Bob", "age": 25, "city": "Paris"}{"name": "Charlie", "age": "thirty"} (conflict)Stopped
Key Moments - 3 Insights
Why does indexing a new field fail in explicit mapping?
In explicit mapping, only predefined fields are accepted. As shown in step 3 of the execution_table, the 'city' field is not defined, so Elasticsearch rejects it.
How does dynamic mapping add new fields?
Dynamic mapping infers the field type from the document and adds it automatically to the mapping, as seen in steps 4-6 where 'city' is added dynamically.
What happens if a field's type conflicts with previous mapping?
If a new document has a field with a different type than previously mapped, Elasticsearch raises a type conflict error, as in step 7 with 'age' field.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is a new field added dynamically?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Check the 'Mapping Update' column for when 'city' is added dynamically.
According to variable_tracker, what fields exist after step 2 in explicit mapping?
AOnly 'name'
B'name', 'age', and 'city'
C'name' and 'age'
DNo fields
💡 Hint
Look at the 'Mapping fields' row after 'After 2' column.
If explicit mapping allowed unknown fields, how would step 3 change?
AField 'city' would be rejected
BField 'city' would be accepted without error
CField 'city' would be added dynamically
DMapping would be deleted
💡 Hint
Refer to the difference between explicit and dynamic mapping in the execution_table steps 3 and 4.
Concept Snapshot
Dynamic vs explicit mapping in Elasticsearch:
- Explicit mapping: define fields and types before indexing
- Dynamic mapping: Elasticsearch infers and adds fields automatically
- Explicit rejects unknown fields unless updated
- Dynamic can cause type conflicts if data changes
- Choose explicit for control, dynamic for flexibility
Full Transcript
When you add data to Elasticsearch, it needs to know what type each field is, like text or number. You can tell it exactly by making an explicit mapping before adding data. Then, only those fields are allowed. If you try to add a new field not in the mapping, it will reject it. Or, you can let Elasticsearch guess the types by using dynamic mapping. It looks at your data and adds new fields automatically. But if later you add a field with a different type, it causes an error. This example showed how explicit mapping accepts only known fields and dynamic mapping adds new fields on the fly. Understanding this helps you decide how to organize your data in Elasticsearch.