Getting error similar to “mapping set to strict, dynamic introduction of [location] within [_doc] is not allowed” when attempting to index a document to an Elasticsearch index.

Olamide OLAJIDE
3 min readJul 13, 2020

This error suggests that the client is trying to index a document that contains a field that is not currently defined in the index mapping. By default, the new field should be created automatically and the document should be indexed without any issues. But in this case, the mapping of the index has been configured to NOT allow the dynamic creation of any new field as the keyword strict must have been stated in the index mapping. Look at the following as an example from the tests that I performed in my own environment.

I created an index named strict_test and stated "dynamic": "strict". This setting would NOT allow the dynamic creation of a new field, if the field is not already in the index mapping. If such new fields are detected, an exception is thrown and the document is rejected. So, the new fields must be explicitly added to the mapping. Note that only the field user is created in this index.

PUT strict_test
{
"mappings": {
"dynamic": "strict",
"properties": {
"user": {
"type": "text"
}
}
}
}

I then added the following document (as shown below) and it was successful.

POST strict_test/_doc
{
"user": "olamide"
}

I then went further to add the following document which has an extra field that has not been explicitly created in the index.

POST strict_test/_doc
{
"user": "olamide",
"location": "iseyin"
}

I got the following exception:

{
"error" : {
"root_cause" : [
{
"type" : "strict_dynamic_mapping_exception",
"reason" : "mapping set to strict, dynamic introduction of [location] within [_doc] is not allowed"
}
],
"type" : "strict_dynamic_mapping_exception",
"reason" : "mapping set to strict, dynamic introduction of [location] within [_doc] is not allowed"
},
"status" : 400
}

On the other hand, I tested another index named no_strict_test where I stated "dynamic": true. Notice that this is the default behaviour. So, even if I didn’t define the line dynamic when creating your index, it will be automatically set to true for me, behind the scene

PUT no_strict_test
{
"mappings": {
"dynamic": true,
"properties": {
"user": {
"type": "text"
}
}
}
}

I indexed the following documents, which were successfully indexed. Notice that the second document contained an extra field named location , though I defined only the `user` field in the field mapping.

POST no_strict_test/_doc
{
"user": "olamide"
}
POST no_strict_test/_doc
{
"user": "olamide",
"location": "iseyin"
}

Run the GET no_strict_test/_mapping to verify that the new field location has been automatically added to the index. So, the second index is able to dynamically create a new index. See dynamic documentation for more information on dynamic setting in an index.

Hence to address this issue, you can take either of the following steps, depending on your use case. Both steps are NOT required, though taking both steps doesn’t hurt (depending on your use case).

  1. Set dynamic to true in the index mapping, then re-attempt to reindex your document. See below for how to set dynamic to true:
PUT strict_test/_mappings
{
"dynamic": true
}

Please note that this setting would now allow automatic creation of new fields in your index. If you do NOT want your index to behave in this manner, then you can rather manually add the field you desire now, for the document you want to index, and when you want to index other documents that has a field that is not already in the index, you would again have to manually, explicitly create that field in the index mapping. This leads us to the next step you may take.

2. Explicitly create the field in the index. Then retry reindexing your document. Please see the sample below for how to create a field mapping for an existing index.

PUT strict_test/_mappings
{
"properties": {
"location": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}

Note, I explicitly created a field called location of type text and keywordin my existing index strict_test . This is just an example. You would have to create your own field and define the data type based on your use case.

Note that how you create a field mapping may vary depending on the version of elasticsearch you’re using. Please check the appropriate PUT mapping API documentation that corresponds to your version of Elasticsearch for the correct syntax.

I hope you find this information helpful.

--

--