0
0
ElasticsearchConceptBeginner · 3 min read

Dynamic Mapping in Elasticsearch: What It Is and How It Works

In Elasticsearch, dynamic mapping automatically detects and adds new fields to the index schema when documents with unknown fields are indexed. This feature helps Elasticsearch adapt to changing data without manual schema updates.
⚙️

How It Works

Dynamic mapping in Elasticsearch works like a smart assistant that watches the data you send and automatically updates the index's schema to include any new fields it finds. Imagine you have a notebook where you write down details about your friends. If you suddenly start noting their favorite color, dynamic mapping is like adding a new column for "favorite color" without you having to rewrite the whole notebook.

When you index a document with fields Elasticsearch hasn't seen before, it guesses the type of each new field (like text, number, or date) and adds it to the mapping. This way, Elasticsearch can store and search the new data properly without you needing to define the schema upfront.

This automatic process saves time and makes Elasticsearch flexible, especially when dealing with data that changes often or has many different fields.

💻

Example

This example shows how Elasticsearch dynamically adds new fields when indexing documents without a predefined mapping.

json
PUT /my_dynamic_index
{
  "mappings": {
    "dynamic": true
  }
}

POST /my_dynamic_index/_doc/1
{
  "name": "Alice",
  "age": 30
}

POST /my_dynamic_index/_doc/2
{
  "name": "Bob",
  "age": 25,
  "city": "New York"
}

GET /my_dynamic_index/_mapping
Output
{ "my_dynamic_index": { "mappings": { "properties": { "name": {"type": "text"}, "age": {"type": "long"}, "city": {"type": "text"} } } } }
🎯

When to Use

Use dynamic mapping when you expect your data to have new or changing fields over time and you want Elasticsearch to handle schema updates automatically. This is helpful in early development stages, logging systems, or when ingesting data from varied sources where the exact fields are not fixed.

However, for critical production systems where strict control over data types and fields is needed, you might want to disable dynamic mapping or use explicit mappings to avoid unexpected data types or mapping conflicts.

Key Points

  • Dynamic mapping automatically adds new fields to the index schema when unknown fields appear.
  • It guesses the data type of new fields to store and search them correctly.
  • It makes Elasticsearch flexible and easy to use with changing or unknown data structures.
  • You can control dynamic mapping behavior with settings like dynamic: true, false, or strict.
  • Use with caution in production to avoid unexpected mapping changes.

Key Takeaways

Dynamic mapping lets Elasticsearch add new fields automatically when indexing unknown data.
It detects field types to keep data searchable without manual schema updates.
Ideal for flexible or evolving data but use explicit mappings for strict control.
You can enable, disable, or restrict dynamic mapping with simple settings.
Dynamic mapping helps speed up development and handle varied data sources easily.