# A question on dynamic templates

**URL:** <https://discuss.elastic.co/t/a-question-on-dynamic-templates/226717>\
**Category:** Elasticsearch\
**Created:** [April 6, 2020, 1:24pm UTC](https://discuss.elastic.co/t/a-question-on-dynamic-templates/226717 "2020-04-06T13:24:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pk.241011](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pk.241011/32/86285_2.png) [@pk.241011](https://discuss.elastic.co/u/pk.241011)\
**Post date:** [April 6, 2020, 1:24pm UTC](https://discuss.elastic.co/t/a-question-on-dynamic-templates/226717/1 "2020-04-06T13:24:34Z")

</div>

I am putting in custom templates to take the guess work out of the mappings generated. The indices are created daily and collect logs. I took the mapping of one of the created index and then added the dynamic template section in it. Then I posted it back into the elasticsearch. Should I have removed the sections which would now be handled by dynamic template?

**Details**  
The field is **Result.NumericValue**

Now it can have say 34 as well as 67.56. Depending on the contents of the first log of the day, Result.NumericValue would get mapped as a long or a double. If it gets mapped as long then it rejects the values like 67.56.

So I wrote a dynamic template:

```
"dynamic_templates": [
  {
	"full_name": {
	  "path_match": "*.NumericValue",
	  "mapping": {
		"type": "float"
	  }
	}
  }
],

```

The existing template has sections like this

```
"type" : {
  "properties" : {
	"numericValue" : {
	  "type" : "float"
	}
  }
}

```

Should I remove them before pushing it in elasticsearch. Till now I have refrained from doing so. Does it make a difference? Or does dynamic template has higher precedence?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 6, 2020, 1:44pm UTC](https://discuss.elastic.co/t/a-question-on-dynamic-templates/226717/2 "2020-04-06T13:44:56Z")

</div>

The mapping in the template has precedence before the dynamic template. In this case it does not matter though, so you could just remove it.

---

<div class="post-metadata">

**Author:** ![pk.241011](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pk.241011/32/86285_2.png) [@pk.241011](https://discuss.elastic.co/u/pk.241011)\
**Post date:** [April 6, 2020, 1:48pm UTC](https://discuss.elastic.co/t/a-question-on-dynamic-templates/226717/3 "2020-04-06T13:48:31Z")

</div>

Thanks for quick reply. Will do that.

One question just to make things clear for myself.

Lets assume that the existing mapping was **long**.

```
"type" : {
  "properties" : {
	"numericValue" : {
	  "type" : "long"
	}
  }
}

```

If Result.NumericValue comes with a value 34.65 then based on above template, elasticsearch will reject it because mapping in template has the precedence over the dynamic template section of it. Right?  
Despite a dynamic template section which would have mapped this as a float.

Sorry for so many edits to answers.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 6, 2020, 2:15pm UTC](https://discuss.elastic.co/t/a-question-on-dynamic-templates/226717/4 "2020-04-06T14:15:11Z")

</div>

You can check via

```auto
DELETE foo

PUT foo
{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name": {
          "path_match": "value",
          "mapping": {
            "type": "float"
          }
        }
      }
    ],
    "properties": {
      "value": {
        "type": "long"
      }
    }
  }
}

GET foo/_mapping/field/value

```

---

<div class="post-metadata">

**Author:** ![pk.241011](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pk.241011/32/86285_2.png) [@pk.241011](https://discuss.elastic.co/u/pk.241011)\
**Post date:** [April 7, 2020, 12:11am UTC](https://discuss.elastic.co/t/a-question-on-dynamic-templates/226717/5 "2020-04-07T00:11:48Z")

</div>

Sorry for late reply. And I am bit confused now. I am able to put both 34 and 34.34 in the index using this mapping !! Should it not have refused 34.34 ??

```
POST foo/_doc/
{
  "value" : 34
}

POST foo/_doc/
{
  "value" : 34.34
}

GET foo/_search

```

Results contain both the values.

```
"hits" : [
  {
    "_index" : "foo",
    "_type" : "_doc",
    "_id" : "6vP4UXEBMk4Pt2X_nero",
    "_score" : 1.0,
    "_source" : {
      "value" : 34
    }
  },
  {
    "_index" : "foo",
    "_type" : "_doc",
    "_id" : "dE_4UXEBRU_resXUqMei",
    "_score" : 1.0,
    "_source" : {
      "value" : 34.34
    }
  }
]
```

---

<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:** [May 5, 2020, 12:11am UTC](https://discuss.elastic.co/t/a-question-on-dynamic-templates/226717/6 "2020-05-05T00:11:54Z")

</div>

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