Problem:
Demonstrate the use of different values for dynamic mappings: true, false, strict.
Solution Summary:
With dynamic mappings enabled (true), elasticsearch will automatically assign types to our fields. Dynamic mapping is enabled by default.
Setting the dynamic parameter to false ignorea new fields.
When dynamic mapping is set to strict, it will throw an exception if an unknown field is encountered.
Dynamic mapping can be set, both at the document and at the object level.
Prerequisites:
Working Elasticsearch cluster and Kibana.
Delete employee_mappingdemo if already exist:
DELETE /employee_mappingdemo
Solution Steps:
Initial Document
PUT /employee_mappingdemo/_doc/1?pretty
{
"name": {
"first":"Heartin"
}
}
Check the current mapping:
GET employee_mappingdemo/_mapping
Response (Current Mapping):
{
"employee_mappingdemo": {
"mappings": {
"_doc": {
"properties": {
"name": {
"properties": {
"first": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
Adding a document with new field (dynamic is true)
PUT /employee_mappingdemo/_doc/2?pretty
{
"name": {
"first":"Heartin",
"last": "Kanikathottu"
},
"phone": "9999999999"
}
Note: If you verify the mapping, you see that mapping for field "last" and "phone" are added.
Disabling Dynamic Mapping (dynamic = false)
Disable dynamic mapping as:
PUT /employee_mappingdemo/_mapping/_doc
{
"dynamic": "false"
}
Verify mapping to see dynamic is set to false.
Now execute below PUT request with new "middle" field and "address" field:
PUT /employee_mappingdemo/_doc/3?pretty
{
"name": {
"first":"Heartin",
"middle": "Jacob",
"last": "Kanikathottu"
},
"address": "Bangalore, India"
}
Note:
-
There will be no error.
-
No mapping is created for "middle" field and "address" field. Verify the mappings.
-
Data for "middle" field and "address" field are still part of the document. Verify using a GET request.
-
However, we will not be able to use them in queries (e.g. match query).
Strict Dynamic Mapping (dynamic = strict)
Set strict dynamic mapping as:
PUT /employee_mappingdemo/_mapping/_doc
{
"dynamic": "strict"
}
Verify mapping to see dynamic is set to strict.
Now execute below PUT request with new "middle" field and "address" field:
PUT /employee_mappingdemo/_doc/4?pretty
{
"name": {
"first":"Heartin",
"middle": "Jacob",
"last": "Kanikathottu"
},
"address": "Bangalore, India"
}
Note:
-
There will be no error: strict_dynamic_mapping_exception: mapping set to strict, dynamic introduction of [middle] within [name] is not allowed.
-
No mapping is created for "middle" field and "address" field. Verify the mappings.
-
Document is also not created. Verify using a GET request.
Overriding mapping for Nested Objects
Set document level mapping to false and nested object level mapping to true:
PUT /employee_mappingdemo/_mapping/_doc
{
"dynamic": "false",
"properties": {
"name": {
"dynamic": "true",
"properties": {
}
}
}
}
Now execute below PUT request with new "middle" field and "address" field:
PUT /employee_mappingdemo/_doc/5?pretty
{
"name": {
"first":"Heartin",
"middle": "Jacob",
"last": "Kanikathottu"
},
"address": "Bangalore, India"
}
Note:
-
There will be no errors.
-
Mapping will be created for "middle" field, but NOT for "address field". Verify the mappings.
-
Data for "middle" field and "address" field are part of the document. Verify using a GET request.
-
However, we will not be able to use "address" in queries, but only "middle" (e.g. match query).
References:
https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-...
https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html
https://stackoverflow.com/questions/19309131/dynamic-mapping-for-nested-...
https://stackoverflow.com/questions/34090080/how-to-change-type-of-a-val...
Recent comments