# Apply dynamic template to all fields of an object

**URL:** <https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968>\
**Category:** Elasticsearch\
**Created:** [June 4, 2012, 2:52pm UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968 "2012-06-04T14:52:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mkleen/32/1694_2.png) [@mkleen](https://discuss.elastic.co/u/mkleen)\
**Post date:** [June 4, 2012, 2:52pm UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/1 "2012-06-04T14:52:40Z")

</div>

Hello,

I am trying to apply a dynamic template mapping to all fields of an object  
including the aggregated objects, but it gets only applied to the fields of  
the root object:

curl -XPUT [http://localhost:9200/test/data/\_mapping](http://localhost:9200/test/data/_mapping) -d '{  
"_default_" : {  
"dynamic\_templates" : [  
{  
"string\_template" : {  
"match" : "\*",  
"match\_mapping\_type" : "string",  
"mapping" : {  
"type" : "multi\_field",  
"fields" : {  
"{name}" : {"type": "string", "index" :  
"analyzed", "analyzer" : "default", "store" : "false", "include\_in\_all":  
"true"},  
"{name}\_untouched" : {"type": "string", "index" :  
"analyzed", "analyzer" : "simple", "store" : "false", "include\_in\_all":  
"true"}

```
               }
           }
       }
     }                                                         
   ]
}        

```

}'

When I now add an object:

curl -XPUT [http://localhost:9200/test/data/1](http://localhost:9200/test/data/1) -d '{  
"name" : "michael",  
"company" : {  
name: "foobar"  
}  
}'

I can only query the fields of the root object with the \_untouched postfix:

curl -XGET [http://localhost:9200/test/data/\_search](http://localhost:9200/test/data/_search) -d '{  
"query" : {  
"term" : { "name\_untouched": "michael" }  
}  
}'

but it doesnt get applied to company:

curl -XGET [http://localhost:9200/test/data/\_search](http://localhost:9200/test/data/_search) -d '{  
"query" : {  
"term" : { "company.name\_untouched": "foobar" }  
}  
}'

Is this feature not supported, or do I do something wrong ?

Many Thanks,

Michael

---

<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:** [June 8, 2012, 10:02pm UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/2 "2012-06-08T22:02:31Z")

</div>

You set the mapping wrongly, you set the \_mapping on the data mapping, but  
then specify _default_ there. Here is a gist for both options, either  
directly on data, or setting it in a generic fashion on _default_:  
[explicit data mapping · GitHub](https://gist.github.com/2898285).

On Mon, Jun 4, 2012 at 4:52 PM, mkleen [mkleen@gmail.com](mailto:mkleen@gmail.com) wrote:

> Hello,
> 
> I am trying to apply a dynamic template mapping to all fields of an object  
> including the aggregated objects, but it gets only applied to the fields of  
> the root object:
> 
> curl -XPUT [http://localhost:9200/test/data/\_mapping](http://localhost:9200/test/data/_mapping) -d '{  
> "_default_" : {  
> "dynamic\_templates" : [  
> {  
> "string\_template" : {  
> "match" : "\*",  
> "match\_mapping\_type" : "string",  
> "mapping" : {  
> "type" : "multi\_field",  
> "fields" : {  
> "{name}" : {"type": "string", "index" :  
> "analyzed", "analyzer" : "default", "store" : "false", "include\_in\_all":  
> "true"},  
> "{name}\_untouched" : {"type": "string", "index" :  
> "analyzed", "analyzer" : "simple", "store" : "false", "include\_in\_all":  
> "true"}
> 
> ```
> }
> }
> }
> }
> ]
> }
> 
> ```
> 
> }'
> 
> When I now add an object:
> 
> curl -XPUT [http://localhost:9200/test/data/1](http://localhost:9200/test/data/1) -d '{  
> "name" : "michael",  
> "company" : {  
> name: "foobar"  
> }  
> }'
> 
> I can only query the fields of the root object with the \_untouched  
> postfix:
> 
> curl -XGET [http://localhost:9200/test/data/\_search](http://localhost:9200/test/data/_search) -d '{  
> "query" : {  
> "term" : { "name\_untouched": "michael" }  
> }  
> }'
> 
> but it doesnt get applied to company:
> 
> curl -XGET [http://localhost:9200/test/data/\_search](http://localhost:9200/test/data/_search) -d '{  
> "query" : {  
> "term" : { "company.name\_untouched": "foobar" }  
> }  
> }'
> 
> Is this feature not supported, or do I do something wrong ?
> 
> Many Thanks,
> 
> Michael

---

<div class="post-metadata">

**Author:** ![mkleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mkleen/32/1694_2.png) [@mkleen](https://discuss.elastic.co/u/mkleen)\
**Post date:** [June 11, 2012, 8:35am UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/3 "2012-06-11T08:35:26Z")

</div>

Ok thanks. One more questions:

curl -XPUT [http://localhost:9200/test](http://localhost:9200/test) -d '{  
"mappings" : {

Which equivalent request is that in the java api ? It doesnt look like a mapping request.

Many thanks,

Michael

curl -XPUT [http://localhost:9200/test/\_default\_/\_mapping](http://localhost:9200/test/_default_/_mapping) -d '{

On Saturday, June 9, 2012 12:02:31 AM UTC+2, kimchy wrote:

> You set the mapping wrongly, you set the \_mapping on the data mapping, but  
> then specify _default_ there. Here is a gist for both options, either  
> directly on data, or setting it in a generic fashion on _default_:  
> [explicit data mapping · GitHub](https://gist.github.com/2898285).
> 
> On Mon, Jun 4, 2012 at 4:52 PM, mkleen [mkleen@gmail.com](mailto:mkleen@gmail.com) wrote:
> 
> > Hello,
> > 
> > I am trying to apply a dynamic template mapping to all fields of an  
> > object including the aggregated objects, but it gets only applied to the  
> > fields of the root object:
> > 
> > curl -XPUT [http://localhost:9200/test/data/\_mapping](http://localhost:9200/test/data/_mapping) -d '{  
> > "_default_" : {  
> > "dynamic\_templates" : [  
> > {  
> > "string\_template" : {  
> > "match" : "\*",  
> > "match\_mapping\_type" : "string",  
> > "mapping" : {  
> > "type" : "multi\_field",  
> > "fields" : {  
> > "{name}" : {"type": "string", "index" :  
> > "analyzed", "analyzer" : "default", "store" : "false", "include\_in\_all":  
> > "true"},  
> > "{name}\_untouched" : {"type": "string", "index" :  
> > "analyzed", "analyzer" : "simple", "store" : "false", "include\_in\_all":  
> > "true"}
> > 
> > ```
> > }
> > }
> > }
> > }                                                         
> > ]
> > }        
> > 
> > ```
> > 
> > }'
> > 
> > When I now add an object:
> > 
> > curl -XPUT [http://localhost:9200/test/data/1](http://localhost:9200/test/data/1) -d '{  
> > "name" : "michael",  
> > "company" : {  
> > name: "foobar"  
> > }  
> > }'
> > 
> > I can only query the fields of the root object with the \_untouched  
> > postfix:
> > 
> > curl -XGET [http://localhost:9200/test/data/\_search](http://localhost:9200/test/data/_search) -d '{  
> > "query" : {  
> > "term" : { "name\_untouched": "michael" }  
> > }  
> > }'
> > 
> > but it doesnt get applied to company:
> > 
> > curl -XGET [http://localhost:9200/test/data/\_search](http://localhost:9200/test/data/_search) -d '{  
> > "query" : {  
> > "term" : { "company.name\_untouched": "foobar" }  
> > }  
> > }'
> > 
> > Is this feature not supported, or do I do something wrong ?
> > 
> > Many Thanks,
> > 
> > Michael

---

<div class="post-metadata">

**Author:** ![Michael\_Kleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_kleen/32/2496_2.png) [@Michael\_Kleen](https://discuss.elastic.co/u/Michael_Kleen)\
**Post date:** [June 13, 2012, 8:41am UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/4 "2012-06-13T08:41:22Z")

</div>

Thanks for you answer. It still doesnt work, i tried the following setup:

[https://gist.github.com/2922822](https://gist.github.com/2922822)

Thanks for your time,

Michael

On Saturday, June 9, 2012 12:02:31 AM UTC+2, kimchy wrote:

> You set the mapping wrongly, you set the \_mapping on the data mapping, but  
> then specify _default_ there. Here is a gist for both options, either  
> directly on data, or setting it in a generic fashion on _default_:  
> [explicit data mapping · GitHub](https://gist.github.com/2898285).

---

<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:** [June 14, 2012, 9:08pm UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/5 "2012-06-14T21:08:41Z")

</div>

It should be company.name.untouched, not with an underscore.

On Wed, Jun 13, 2012 at 10:41 AM, Michael Kleen \<michael.kleen@numberfour.eu

> wrote:

> Thanks for you answer. It still doesnt work, i tried the following setup:
> 
> [https://gist.github.com/2922822](https://gist.github.com/2922822)
> 
> Thanks for your time,
> 
> Michael
> 
> On Saturday, June 9, 2012 12:02:31 AM UTC+2, kimchy wrote:
> 
> > You set the mapping wrongly, you set the \_mapping on the data mapping,  
> > but then specify _default_ there. Here is a gist for both options, either  
> > directly on data, or setting it in a generic fashion on _default_:  
> > [https://gist](https://gist).\*\*[github.com/2898285](http://github.com/2898285) [https://gist.github.com/2898285](https://gist.github.com/2898285).

---

<div class="post-metadata">

**Author:** ![Michael\_Kleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_kleen/32/2496_2.png) [@Michael\_Kleen](https://discuss.elastic.co/u/Michael_Kleen)\
**Post date:** [June 15, 2012, 8:13am UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/6 "2012-06-15T08:13:28Z")

</div>

I tried that as well, it doesnt make a difference. There most be something  
wrong in the configuration which i cannot see. I updated the gist including  
the server responses [https://gist.github.com/2922822](https://gist.github.com/2922822)

I do get results for  
{  
"query" : {  
"term" : { "name.untouched": "michael" }  
}  
}

and for:

{  
"query" : {  
"term" : { "company.name": "foobar" }  
}  
}

but not for

{  
"query" : {  
"term" : { "company.name.untouched": "foobar" }  
}  
}

Thanks for your time,

Michael

On Thursday, June 14, 2012 11:08:41 PM UTC+2, kimchy wrote:

> It should be company.name.untouched, not with an underscore.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 15, 2012, 6:40pm UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/7 "2012-06-15T18:40:30Z")

</div>

In your example, untouched company name is indexed as  
"company.name.name.untouched" not as "company.name.untouched". Try deleting  
your mapping and recreating it like this:

curl -XPUT [http://localhost:9200/test/data/\_mapping](http://localhost:9200/test/data/_mapping) -d '{  
"data" : {  
"dynamic\_templates" : [  
{  
"string\_template" : {  
"match" : "\*",  
"match\_mapping\_type" : "string",  
"mapping" : {  
"type" : "multi\_field",  
"fields" : {  
"{name}" : {"type": "string", "index" :  
"analyzed", "analyzer" : "default", "store" : "false"},  
"untouched" : {"type": "string", "index" :  
"analyzed", "analyzer" : "default", "store" : "false"}

```
               }
           }
       }
     }                                                         
   ]
}        

```

}'

On Friday, June 15, 2012 4:13:28 AM UTC-4, Michael Kleen wrote:

> I tried that as well, it doesnt make a difference. There most be something  
> wrong in the configuration which i cannot see. I updated the gist including  
> the server responses [https://gist.github.com/2922822](https://gist.github.com/2922822)
> 
> I do get results for  
> {  
> "query" : {  
> "term" : { "name.untouched": "michael" }  
> }  
> }
> 
> and for:
> 
> {  
> "query" : {  
> "term" : { "company.name": "foobar" }  
> }  
> }
> 
> but not for
> 
> {  
> "query" : {  
> "term" : { "company.name.untouched": "foobar" }  
> }  
> }
> 
> Thanks for your time,
> 
> Michael
> 
> On Thursday, June 14, 2012 11:08:41 PM UTC+2, kimchy wrote:
> 
> > It should be company.name.untouched, not with an underscore.

---

<div class="post-metadata">

**Author:** ![Michael\_Kleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_kleen/32/2496_2.png) [@Michael\_Kleen](https://discuss.elastic.co/u/Michael_Kleen)\
**Post date:** [June 21, 2012, 9:51am UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/8 "2012-06-21T09:51:26Z")

</div>

rocknroll, its all working now. thank you so much for pointing that out!

Michael

---

<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:23am UTC](https://discuss.elastic.co/t/apply-dynamic-template-to-all-fields-of-an-object/7968/9 "2017-07-06T03:23:07Z")

</div>


