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?"