# Dynamic templates combined with mappings

**URL:** <https://discuss.elastic.co/t/dynamic-templates-combined-with-mappings/5245>\
**Category:** Elasticsearch\
**Created:** [August 24, 2011, 8:32pm UTC](https://discuss.elastic.co/t/dynamic-templates-combined-with-mappings/5245 "2011-08-24T20:32:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sakrafd](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sakrafd](https://discuss.elastic.co/u/sakrafd)\
**Post date:** [August 24, 2011, 8:32pm UTC](https://discuss.elastic.co/t/dynamic-templates-combined-with-mappings/5245/1 "2011-08-24T20:32:31Z")

</div>

Hi,

I have an index that contains fields I'd like to set default dynamic  
mappings for as well as a few customized mappings and have a couple  
questions:

First, Is this the proper way to create the index?

$ curl -X POST "[http://localhost:9200/users\_test](http://localhost:9200/users_test)" -d '  
{ "user": {  
"dynamic\_templates":[{  
"long": {  
"match\_mapping\_type":"long",  
"match":"\*",  
"mapping":{ "omit\_term\_freq\_and\_positions":true,  
"store":"no",  
"type":"long",  
"omit\_norms":true,  
"index":"analyzed",  
"include\_in\_all":false }  
}}]},  
"mapping": {  
"properties":{"birthdate":  
{"null\_value":-62167392000,"type":"long"}  
}  
}  
}'  
{"ok":true,"acknowledged":true}

I would expect to see the birthdate mapping show up when doing a get  
on the \_mapping. but it doesn't:

$ curl -X GET "[http://localhost:9200/users\_test/\_mapping?pretty=true](http://localhost:9200/users_test/_mapping?pretty=true)"  
{  
"users\_test" : {  
}

Instead it shows up in settings:

$ curl -X GET "[http://localhost:9200/users\_test/\_settings?pretty=true](http://localhost:9200/users_test/_settings?pretty=true)"  
{  
"users\_test" : {  
"settings" : {  
"index.mapping.properties.birthdate.null\_value" :  
"-62167392000",  
"index.user.dynamic\_templates.0.long.mapping.store" : "no",  
"index.user.dynamic\_templates.0.long.match\_mapping\_type" :  
"long",  
"index.user.dynamic\_templates.0.long.mapping.include\_in\_all" :  
"false",  
"index.user.dynamic\_templates.0.long.match" : "\*",  
"index.user.dynamic\_templates.  
0.long.mapping.omit\_term\_freq\_and\_positions" : "true",  
"index.user.dynamic\_templates.0.long.mapping.index" :  
"analyzed",  
"index.mapping.properties.birthdate.type" : "long",  
"index.user.dynamic\_templates.0.long.mapping.omit\_norms" :  
"true",  
"index.user.dynamic\_templates.0.long.mapping.type" : "long",  
"index.number\_of\_shards" : "5",  
"index.number\_of\_replicas" : "1"  
}  
}  
}

Also, will the default "long" template values apply to birthday (ie.  
birthday will have a null value set as well as omit\_norms: true)?

Here is a gist if it's easier:

> <https://gist.github.com/sakrafd/1169047>

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [August 24, 2011, 10:38pm UTC](https://discuss.elastic.co/t/dynamic-templates-combined-with-mappings/5245/2 "2011-08-24T22:38:30Z")

</div>

There are several problems in what you try to do, and a misunderstanding of  
what dynamic\_templates do...

First, the curl you use is to create the index, but, the mappings should be  
under a "mappings" element, otherwise, those are used as settings for the  
index, and not mappings.

Once we are over that, here is what dynamic\_templates do: They apply to  
_dynamically_ added fields, not ones that are explicitly set. I think you  
expect birthdate to have the settings you set in the long dynamic\_template,  
but that won't work, since it has its own mappings set already, so its not  
dynamically introduced.

I think what you are after are custom types, which is something I have been  
thinking on adding to elasticsearch. Something like defining a type called  
"my\_long", and then use it in explicit mappings.

Here is a gist of a working sample that uses dynamic templates for a  
dynamically introduced birthdate: [gist:1169471 · GitHub](https://gist.github.com/1169471).

Here is a gist for explicit mapping for birthdate, that shows that the  
dynamic template does not apply (and also to show another mistake you made  
in the mappings): [gist:1169474 · GitHub](https://gist.github.com/1169474).

On Wed, Aug 24, 2011 at 11:32 PM, Dave Farkas [dave@signalhq.com](mailto:dave@signalhq.com) wrote:

> Hi,
> 
> I have an index that contains fields I'd like to set default dynamic  
> mappings for as well as a few customized mappings and have a couple  
> questions:
> 
> First, Is this the proper way to create the index?
> 
> $ curl -X POST "[http://localhost:9200/users\_test](http://localhost:9200/users_test)" -d '  
> { "user": {  
> "dynamic\_templates":[{  
> "long": {  
> "match\_mapping\_type":"long",  
> "match":"\*",  
> "mapping":{ "omit\_term\_freq\_and\_positions":true,  
> "store":"no",  
> "type":"long",  
> "omit\_norms":true,  
> "index":"analyzed",  
> "include\_in\_all":false }  
> }}]},  
> "mapping": {  
> "properties":{"birthdate":  
> {"null\_value":-62167392000,"type":"long"}  
> }  
> }  
> }'  
> {"ok":true,"acknowledged":true}
> 
> I would expect to see the birthdate mapping show up when doing a get  
> on the \_mapping. but it doesn't:
> 
> $ curl -X GET "[http://localhost:9200/users\_test/\_mapping?pretty=true](http://localhost:9200/users_test/_mapping?pretty=true)"  
> {  
> "users\_test" : {  
> }
> 
> Instead it shows up in settings:
> 
> $ curl -X GET "[http://localhost:9200/users\_test/\_settings?pretty=true](http://localhost:9200/users_test/_settings?pretty=true)"  
> {  
> "users\_test" : {  
> "settings" : {  
> "index.mapping.properties.birthdate.null\_value" :  
> "-62167392000",  
> "index.user.dynamic\_templates.0.long.mapping.store" : "no",  
> "index.user.dynamic\_templates.0.long.match\_mapping\_type" :  
> "long",  
> "index.user.dynamic\_templates.0.long.mapping.include\_in\_all" :  
> "false",  
> "index.user.dynamic\_templates.0.long.match" : "\*",  
> "index.user.dynamic\_templates.  
> 0.long.mapping.omit\_term\_freq\_and\_positions" : "true",  
> "index.user.dynamic\_templates.0.long.mapping.index" :  
> "analyzed",  
> "index.mapping.properties.birthdate.type" : "long",  
> "index.user.dynamic\_templates.0.long.mapping.omit\_norms" :  
> "true",  
> "index.user.dynamic\_templates.0.long.mapping.type" : "long",  
> "index.number\_of\_shards" : "5",  
> "index.number\_of\_replicas" : "1"  
> }  
> }  
> }
> 
> Also, will the default "long" template values apply to birthday (ie.  
> birthday will have a null value set as well as omit\_norms: true)?
> 
> Here is a gist if it's easier:  
> [dynamic templates combined with mappings · GitHub](https://gist.github.com/1169047)
> 
> Thanks,  
> Dave

---

<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:** [July 6, 2017, 3:56am UTC](https://discuss.elastic.co/t/dynamic-templates-combined-with-mappings/5245/3 "2017-07-06T03:56:04Z")

</div>


