Dynamic vs explicit mapping in Elasticsearch - Performance Comparison
When Elasticsearch indexes data, it decides how to store each field using mapping. This can happen automatically (dynamic) or by predefined rules (explicit).
We want to understand how the time to process data grows depending on which mapping method is used.
Analyze the time complexity of this mapping setup:
PUT /my_index
{
"mappings": {
"dynamic": true,
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" }
}
}
}
// vs explicit mapping with dynamic disabled
PUT /my_index_explicit
{
"mappings": {
"dynamic": false,
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" }
}
}
}
This shows two index setups: one lets Elasticsearch guess new fields dynamically, the other only accepts predefined fields.
Look at what happens when documents are indexed:
- Primary operation: Checking each field in the document against the mapping rules.
- How many times: For every field in every document indexed.
Dynamic mapping adds extra checks to detect new fields, while explicit mapping skips this step.
As the number of fields and documents grows, the work to check and update mappings changes:
| Input Size (fields x docs) | Approx. Operations |
|---|---|
| 10 x 10 | Low, quick checks for new fields |
| 100 x 100 | More checks, some new fields trigger mapping updates |
| 1000 x 1000 | Many checks, frequent mapping updates slow indexing |
With dynamic mapping, the time grows faster because it must inspect and possibly update mappings for new fields. Explicit mapping keeps this steady by skipping new field checks.
Time Complexity: O(n × f)
This means the time grows with the number of documents (n) times the number of fields (f) because each field in each document is checked.
[X] Wrong: "Dynamic mapping has no impact on indexing speed because it just happens once."
[OK] Correct: Dynamic mapping checks every field in every document to see if it is new, which adds work each time, slowing down indexing as data grows.
Understanding how dynamic and explicit mappings affect indexing time shows you can think about how Elasticsearch handles data behind the scenes. This skill helps you design better data models and explain trade-offs clearly.
"What if we indexed documents with many nested objects? How would dynamic mapping affect the time complexity then?"
Practice
Solution
Step 1: Understand dynamic mapping
Dynamic mapping lets Elasticsearch guess the data type of fields automatically when new data arrives.Step 2: Understand explicit mapping
Explicit mapping means you define each field's type and settings manually before indexing data.Final Answer:
Dynamic mapping automatically detects field types, explicit mapping requires manual definition. -> Option AQuick Check:
Dynamic = auto detect, Explicit = manual define [OK]
- Confusing which mapping guesses types
- Thinking dynamic disables indexing
- Assuming explicit mapping is only for numbers
age as an integer?Solution
Step 1: Check correct mapping structure
Explicit mapping requires "mappings" with "properties" and field name with type inside.Step 2: Verify field type for age
"age" field must have "type": "integer" to define it as integer.Final Answer:
{"mappings": {"properties": {"age": {"type": "integer"}}}} -> Option DQuick Check:
Correct JSON structure with integer type [OK]
- Missing 'properties' level in JSON
- Using wrong type names like 'number' instead of 'integer'
- Placing fields outside 'mappings'
price field after indexing?{"mappings": {"properties": {"price": {"type": "float"}}}}Solution
Step 1: Read the explicit mapping for price
The mapping defines "price" with "type": "float", so Elasticsearch will store it as a floating-point number.Step 2: Understand type assignment
Explicit mapping overrides dynamic guessing, so price will be float regardless of input data.Final Answer:
float -> Option CQuick Check:
Explicit float mapping means price = float [OK]
- Assuming price becomes integer automatically
- Thinking dynamic mapping applies here
- Confusing float with text or keyword
{"mappings": {"properties": {"date": {"type": "date"}}}}After indexing a document with
"date": "not-a-date", what error will occur?Solution
Step 1: Check mapping type for date
The field "date" is explicitly mapped as "date" type, so Elasticsearch expects a valid date format.Step 2: Indexing invalid date string
Indexing "not-a-date" will cause a parsing exception because it cannot convert the string to a date.Final Answer:
Document rejected with parsing exception -> Option BQuick Check:
Invalid date string causes parsing error [OK]
- Thinking field stores as text automatically
- Assuming no error occurs
- Believing field is ignored silently
Solution
Step 1: Understand dynamic mapping options
"dynamic": "true" allows new fields to be added automatically, "false" ignores new fields, "strict" rejects documents with unknown fields.Step 2: Choose setting to prevent new fields but allow existing
"strict" rejects documents with new fields, so only existing fields are indexed, preventing unwanted additions.Final Answer:
"dynamic": "strict" -> Option AQuick Check:
Use strict to block new fields, allow existing [OK]
- Using dynamic: false which ignores new fields silently
- Using dynamic: true which allows new fields
- Confusing runtime with dynamic setting
Ready to prove it?
Pass the test and this pattern moves unknown
