# How to disable dynamic mapping in 5.1.1?

**URL:** <https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019>\
**Category:** Elasticsearch\
**Created:** [December 14, 2016, 1:02pm UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019 "2016-12-14T13:02:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcollinsworth](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tcollinsworth](https://discuss.elastic.co/u/tcollinsworth)\
**Post date:** [December 14, 2016, 1:02pm UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019/1 "2016-12-14T13:02:40Z")

</div>

Running 5.1.1, followed the [5.1 docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-mapping.html) and set a template and setting on the mapping definition and it still creates dynamic mappings.

Set template to disable dynamic mapping for all:

> ```
> curl -XPUT 'http://localhost:9200/_template/template_all' -d '
> {
> "template": "*",
> "order":0,
> "settings": {
> "index.mapper.dynamic": false 
> }
> }'
> 
> ```

Create mapping:

> ```
> curl -XPUT 'http://localhost:9200/test_schema' -d '
> {
> "mappings": {
> "test_type": {
> "properties": {
> "foo": {
> "type": "string"
> }
> }
> }
> }
> }'
> 
> ```

Set mapping to disable dynamic mapping (since setting template failed to disable):

> ```
> curl -XPUT 'http://localhost:9200/test_schema/_settings' -d '
> {
> "index.mapper.dynamic":false 
> }'
> 
> ```

Attempt to index data with missing field mapping definition, should fail, but doesn't:

> ```
> curl -XPUT 'http://localhost:9200/test_schema/test_type/1' -d '
> {
> "foo": "hello",
> "bar": "should cause error"
> }'
> 
> ```

Successful, but should have thrown error:

> {"\_index":"test\_schema","\_type":"test\_type","\_id":"1","\_version":1,"result":"created","\_shards":{"total":2,"successful":1,"failed":0},"created":true}

Get the mapping to see if it contains the dynamic mapping:

> curl [http://localhost:9200/test\_schema/\_mapping?pretty=true](http://localhost:9200/test_schema/_mapping?pretty=true)

Yep, sure does, but shouldn't:

> ```
> dev@ip-172-31-10-217:~/workspace/generic-schema-scale-test$ curl http://localhost:9200/test_schema/_mapping?pretty=true
> {
> "test_schema" : {
> "mappings" : {
> "test_type" : {
> "properties" : {
> "bar" : {
> "type" : "text",
> "fields" : {
> "keyword" : {
> "type" : "keyword",
> "ignore_above" : 256
> }
> }
> },
> "foo" : {
> "type" : "text"
> }
> }
> }
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![nellicus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nellicus/32/51566_2.png) [@nellicus](https://discuss.elastic.co/u/nellicus)\
**Post date:** [December 20, 2016, 2:45pm UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019/2 "2016-12-20T14:45:08Z")

</div>

you want to look at [https://www.elastic.co/guide/en/elasticsearch/reference/5.1/dynamic-field-mapping.html#dynamic-field-mapping](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/dynamic-field-mapping.html#dynamic-field-mapping) to prevent new **fields** within a **type** from being added to mappings.

---

<div class="post-metadata">

**Author:** ![tcollinsworth](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tcollinsworth](https://discuss.elastic.co/u/tcollinsworth)\
**Post date:** [December 20, 2016, 8:25pm UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019/3 "2016-12-20T20:25:45Z")

</div>

There was nothing on your linked page that was useful. As I said, I followed the 5.1 docs for both global (template) and per index settings and neither disabled dynamic mapping. Try the code I pasted above to reproduce and you will observe it isn't working.

---

<div class="post-metadata">

**Author:** ![offshore](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/offshore/32/52735_2.png) [@offshore](https://discuss.elastic.co/u/offshore)\
**Post date:** [December 21, 2016, 12:07am UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019/4 "2016-12-21T00:07:23Z")

</div>

I recreated the behavior, not being able to 'lock down' the addition of new fields to the schema, when adding new data or updating the schema. This seemed odd, but is explained as follows at the bottom of the [docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-mapping.html):

> Regardless of the value of this setting, types can still be added explicitly when creating an index or with the PUT mapping API.

I was able to slip in new schemas just by posting non-schema data ({"wam": "bam"}, for instance), and add to the test\_schema index. This seemed adverse, but I was able to work around towards the behavior I think you were expecting, using the 'strict' mapping setting when creating custom types. In your example, test\_type should be 'strict', causing an exception to be thrown when a field besides 'foo' is present.

To test, I followed your setup above, but added strict mapping to the test\_type:

> ```
> curl -XPUT 'http://localhost:9200/test_schema' -d '
> {
> "mappings": {
> "test_type": {
> "dynamic": "strict",
> "properties": {
> "foo": {
> "type": "string"
> }
> }
> }
> }
> }'
> 
> ```

This prevented the next data from being indexed, instead throwing the exception:

Request:

> ```
> curl -XPOST 'http://localhost:9200/test_schema/test_type/2' -d ' 
> {
> "wam": "bam"
> }'
> 
> ```

Response:

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

---

<div class="post-metadata">

**Author:** ![nellicus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nellicus/32/51566_2.png) [@nellicus](https://discuss.elastic.co/u/nellicus)\
**Post date:** [December 21, 2016, 8:22am UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019/5 "2016-12-21T08:22:44Z")

</div>

yeah so see @offshore comment below.

---

<div class="post-metadata">

**Author:** ![tcollinsworth](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tcollinsworth](https://discuss.elastic.co/u/tcollinsworth)\
**Post date:** [December 21, 2016, 2:11pm UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019/6 "2016-12-21T14:11:29Z")

</div>

That's what I was expecting. This [page in the ES 5.1 docs](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/dynamic-mapping.html) is still misleading/incorrect.

---

<div class="post-metadata">

**Author:** ![nellicus](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nellicus/32/51566_2.png) [@nellicus](https://discuss.elastic.co/u/nellicus)\
**Post date:** [December 21, 2016, 2:25pm UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019/7 "2016-12-21T14:25:02Z")

</div>

glad to hear we've resolved your query. feel free to open a PR if you feel like contributing to our docs. thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 18, 2017, 2:25pm UTC](https://discuss.elastic.co/t/how-to-disable-dynamic-mapping-in-5-1-1/69019/8 "2017-01-18T14:25:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
