Dynamic mapping is a feature in Elasticsearch that automatically detects and adds new fields to the index when a document contains previously unknown fields. This behavior is configurable. The dynamic
index-level setting controls whether Elasticsearch should dynamically create field mappings when it encounters new fields in documents during indexing.
By default, dynamic mapping is enabled in Elasticsearch. The possible values for this setting are:
true
: New fields are automatically added to the mapping (default).false
: New fields are ignored. They won't be indexed or searchable, but still appear in_source
.strict
: If new fields are encountered, an exception is thrown and the document is rejected.
It's recommended to use dynamic mapping during development and initial data exploration. However, for production environments, it's often better to define explicit mappings to have more control over field types and indexing behavior.
Example
To change the dynamic mapping setting for an index:
PUT /my_index/_settings
{
"index.mapping.dynamic": "false"
}
This example disables dynamic mapping for the my_index
index. You might want to do this to prevent unexpected field additions and to maintain strict control over your index structure.
Common Issues and Misuses
- Unexpected field types: Dynamic mapping may infer incorrect field types, leading to suboptimal search performance.
- Mapping explosion: In large datasets with many unique fields, dynamic mapping can lead to a large number of fields, potentially hitting mapping limits.
- Performance impact: Frequent dynamic mapping updates can impact indexing performance.
Do's and Don'ts
Do's:
- Use dynamic mapping for rapid prototyping and data exploration.
- Regularly review and optimize your mappings based on actual usage patterns.
- Consider using dynamic templates for more fine-grained control over new fields.
- When promoting to production, set
dynamic
tofalse
orstrict
to avoid new fields from being added.
Don'ts:
- Don't rely solely on dynamic mapping in production environments with critical data.
- Avoid using dynamic mapping with highly variable data structures that could lead to mapping explosion.
- Don't ignore mapping updates in long-running indices; periodically review and update as needed.
Frequently Asked Questions
Q: How does dynamic mapping affect indexing performance?
A: Dynamic mapping can slightly slow down indexing as Elasticsearch needs to update the mapping for each new field. For high-volume indexing, predefined mappings are generally faster.
Q: Can I use dynamic mapping for some fields and not others?
A: Yes, you can use dynamic templates to specify different dynamic mapping rules for different field patterns within the same index.
Q: What happens to existing documents when I change the dynamic mapping setting?
A: Changing the dynamic mapping setting only affects future documents. Existing documents and their mappings remain unchanged.
Q: How can I view the current dynamic mapping settings for an index?
A: You can use the Get Index API: GET /my_index
to view all settings, including dynamic mapping configurations.
Q: Is it possible to revert dynamic mapping changes?
A: Once fields are added via dynamic mapping, they cannot be easily removed. It's often necessary to reindex data into a new index with the desired mapping.