# Strict Dynamic Mappings With Dynamic Templates

**URL:** <https://discuss.elastic.co/t/strict-dynamic-mappings-with-dynamic-templates/211629>\
**Category:** Elasticsearch\
**Created:** [December 12, 2019, 11:07am UTC](https://discuss.elastic.co/t/strict-dynamic-mappings-with-dynamic-templates/211629 "2019-12-12T11:07:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phivos](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@phivos](https://discuss.elastic.co/u/phivos)\
**Post date:** [December 12, 2019, 11:07am UTC](https://discuss.elastic.co/t/strict-dynamic-mappings-with-dynamic-templates/211629/1 "2019-12-12T11:07:45Z")

</div>

Hi  
Is it possible to apply strict dynamic mappings using dynamic templates with patterns?  
The goal is to allow structured strict dynamic mappings.  
Example:

```
PUT _template/test-template
{
    "index_patterns": [
        "test.*"
    ],
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas":0
    },
    "mappings": {
        "dynamic": "strict",
        "dynamic_templates":[
            {
                "other_data":{
                    "match_mapping_type":"object",
                    "match":"*_data",
                    "mapping":{
                        "type":"object",
                        "dynamic":true
                    }
                }
            }
        ],
        "properties": {
            "timestamp": {
	        "type": "date"
	    },
	    "log_level": {
	        "type": "keyword"
	    },
	    "message": {
	        "type": "text"
	    }
	 }
    }
}

```

and try index a document, example:

```
POST test.index/_doc
{ 
   "timestamp":"2019-12-12T10:10:57.910",
   "log_level":"error",
   "message":"failed miserably",
   "service_data":{
     "foo":"bar",
     "baz":2354
   }
}

```

this will get the following response:

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

```

Is it even possible to create such mapping? If it is what am i missing?  
I would expect this behavior when trying to index something like the following

```
POST test.index/_doc
{ 
   "timestamp":"2019-12-12T10:10:57.910",
   "log_level":"error",
   "message":"failed miserably",
   "arbitrary_field":"test"
}
```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [December 14, 2019, 12:37pm UTC](https://discuss.elastic.co/t/strict-dynamic-mappings-with-dynamic-templates/211629/2 "2019-12-14T12:37:15Z")

</div>

No, you cannot dynamically map fields if you set `dynamic` to `strict`. You'd have keep `dynamic` to its default value of `true` to use dynamic templates.

---

<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 11, 2020, 12:46pm UTC](https://discuss.elastic.co/t/strict-dynamic-mappings-with-dynamic-templates/211629/3 "2020-01-11T12:46:11Z")

</div>

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