# Dynamic Templates using Java API

**URL:** <https://discuss.elastic.co/t/dynamic-templates-using-java-api/5730>\
**Category:** Elasticsearch\
**Created:** [October 31, 2011, 9:05pm UTC](https://discuss.elastic.co/t/dynamic-templates-using-java-api/5730 "2011-10-31T21:05:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bojidar\_Penchev](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@Bojidar\_Penchev](https://discuss.elastic.co/u/Bojidar_Penchev)\
**Post date:** [October 31, 2011, 9:05pm UTC](https://discuss.elastic.co/t/dynamic-templates-using-java-api/5730/1 "2011-10-31T21:05:21Z")

</div>

Hi,

I'm facing a problem with the dynamic templates when I try to set them  
using the Java API.

When I'm using the following curl command everything is fine.

curl -X POST "[http://localhost:9200/myindex](http://localhost:9200/myindex)" -d '{  
"mappings" : {  
"mytype": {  
"dynamic\_templates":[{  
"list\_mapping": {  
"match":"list\_\__",  
"mapping":{  
"index" : "not\_analyzed"  
}  
}  
},{  
"set\_mapping": {  
"match":"set\_\__",  
"mapping":{  
"index" : "not\_analyzed"  
}  
}  
}  
]  
}  
}  
}'

I tried to do the same in various ways using Java, but non of them was  
successful.  
Here is a gist [https://gist.github.com/1328836](https://gist.github.com/1328836) with my initial guess in  
Java.

If you run it, the output will be "{"mytype":{"properties":{}}}" , but this  
is obviously wrong, and If I try to index some documents with field names  
matching list\_\_\* or set\_\_\* they will be analyzed.  
On the other side with the curl command everything works great.

---

<div class="post-metadata">

**Author:** ![K\_B](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@K\_B](https://discuss.elastic.co/u/K_B)\
**Post date:** [October 31, 2011, 9:50pm UTC](https://discuss.elastic.co/t/dynamic-templates-using-java-api/5730/2 "2011-10-31T21:50:28Z")

</div>

Didn't try out the code, but the bracket in line 37 seems wrong IMHO;

Best Regards

On 31 Okt., 22:05, Bojidar Penchev [dcode...@gmail.com](mailto:dcode...@gmail.com) wrote:

> Hi,
> 
> I'm facing a problem with the dynamic templates when I try to set them  
> using the Java API.
> 
> When I'm using the following curl command everything is fine.
> 
> curl -X POST "[http://localhost:9200/myindex](http://localhost:9200/myindex)" -d '{  
> "mappings" : {  
> "mytype": {  
> "dynamic\_templates":[{  
> "list\_mapping": {  
> "match":"list\_\__",  
> "mapping":{  
> "index" : "not\_analyzed"  
> }  
> }  
> },{  
> "set\_mapping": {  
> "match":"set\_\__",  
> "mapping":{  
> "index" : "not\_analyzed"  
> }  
> }  
> }  
> ]  
> }  
> }
> 
> }'
> 
> I tried to do the same in various ways using Java, but non of them was  
> successful.  
> Here is a gist [https://gist.github.com/1328836with](https://gist.github.com/1328836with) my initial guess in  
> Java.
> 
> If you run it, the output will be "{"mytype":{"properties":{}}}" , but this  
> is obviously wrong, and If I try to index some documents with field names  
> matching list\_\_\* or set\_\_\* they will be analyzed.  
> On the other side with the curl command everything works great.

---

<div class="post-metadata">

**Author:** ![Bojidar\_Penchev](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@Bojidar\_Penchev](https://discuss.elastic.co/u/Bojidar_Penchev)\
**Post date:** [November 1, 2011, 8:57am UTC](https://discuss.elastic.co/t/dynamic-templates-using-java-api/5730/3 "2011-11-01T08:57:51Z")

</div>

Unfortunately the problem is not with the bracket in line 37.  
I tried without the brackets but , then I got exception related to the  
parsing of the JSON.  
And also the same JSON string works fine with curl.

I think the problem is with the Java command, maybe I don't send the JSON  
mapping in right way, or maybe it's a bug.

On Mon, Oct 31, 2011 at 11:50 PM, K.B. [korbinian.bachl@googlemail.com](mailto:korbinian.bachl@googlemail.com)wrote:

> Didn't try out the code, but the bracket in line 37 seems wrong IMHO;
> 
> Best Regards
> 
> On 31 Okt., 22:05, Bojidar Penchev [dcode...@gmail.com](mailto:dcode...@gmail.com) wrote:
> 
> > Hi,
> > 
> > I'm facing a problem with the dynamic templates when I try to set them  
> > using the Java API.
> > 
> > When I'm using the following curl command everything is fine.
> > 
> > curl -X POST "[http://localhost:9200/myindex](http://localhost:9200/myindex)" -d '{  
> > "mappings" : {  
> > "mytype": {  
> > "dynamic\_templates":[{  
> > "list\_mapping": {  
> > "match":"list\_\__",  
> > "mapping":{  
> > "index" : "not\_analyzed"  
> > }  
> > }  
> > },{  
> > "set\_mapping": {  
> > "match":"set\_\__",  
> > "mapping":{  
> > "index" : "not\_analyzed"  
> > }  
> > }  
> > }  
> > ]  
> > }  
> > }
> > 
> > }'
> > 
> > I tried to do the same in various ways using Java, but non of them was  
> > successful.  
> > Here is a gist [https://gist.github.com/1328836with](https://gist.github.com/1328836with) my initial guess in  
> > Java.
> > 
> > If you run it, the output will be "{"mytype":{"properties":{}}}" , but  
> > this  
> > is obviously wrong, and If I try to index some documents with field names  
> > matching list\_\_\* or set\_\_\* they will be analyzed.  
> > On the other side with the curl command everything works great.

---

<div class="post-metadata">

**Author:** ![Bojidar\_Penchev](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@Bojidar\_Penchev](https://discuss.elastic.co/u/Bojidar_Penchev)\
**Post date:** [November 1, 2011, 9:57am UTC](https://discuss.elastic.co/t/dynamic-templates-using-java-api/5730/4 "2011-11-01T09:57:50Z")

</div>

OK, the problem is solved 🙂

I removed the the root mapping element from JSON and now the dynamic  
templates are working.

This is the JSON string that do the work:

String DT\_MAPPING = "{" +  
" "mytype": {" +  
" "dynamic\_templates": [{" +  
" "list\_mapping": {" +  
" "match":"list\_\__"," +  
" "mapping":{" +  
" "index" : "not\_analyzed"" +  
" }" +  
" }" +  
" }, {" +  
" "set\_mapping": {" +  
" "match":"set\_\__"," +  
" "mapping":{" +  
" "index" : "not\_analyzed"" +  
" }" +  
" }" +  
" }]" +  
" }" +  
"}";

On Tue, Nov 1, 2011 at 10:57 AM, Bojidar Penchev [dcodeboy@gmail.com](mailto:dcodeboy@gmail.com) wrote:

> Unfortunately the problem is not with the bracket in line 37.  
> I tried without the brackets but , then I got exception related to the  
> parsing of the JSON.  
> And also the same JSON string works fine with curl.
> 
> I think the problem is with the Java command, maybe I don't send the JSON  
> mapping in right way, or maybe it's a bug.
> 
> On Mon, Oct 31, 2011 at 11:50 PM, K.B. [korbinian.bachl@googlemail.com](mailto:korbinian.bachl@googlemail.com)wrote:
> 
> > Didn't try out the code, but the bracket in line 37 seems wrong IMHO;
> > 
> > Best Regards
> > 
> > On 31 Okt., 22:05, Bojidar Penchev [dcode...@gmail.com](mailto:dcode...@gmail.com) wrote:
> > 
> > > Hi,
> > > 
> > > I'm facing a problem with the dynamic templates when I try to set them  
> > > using the Java API.
> > > 
> > > When I'm using the following curl command everything is fine.
> > > 
> > > curl -X POST "[http://localhost:9200/myindex](http://localhost:9200/myindex)" -d '{  
> > > "mappings" : {  
> > > "mytype": {  
> > > "dynamic\_templates":[{  
> > > "list\_mapping": {  
> > > "match":"list\_\__",  
> > > "mapping":{  
> > > "index" : "not\_analyzed"  
> > > }  
> > > }  
> > > },{  
> > > "set\_mapping": {  
> > > "match":"set\_\__",  
> > > "mapping":{  
> > > "index" : "not\_analyzed"  
> > > }  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }
> > > 
> > > }'
> > > 
> > > I tried to do the same in various ways using Java, but non of them was  
> > > successful.  
> > > Here is a gist [https://gist.github.com/1328836with](https://gist.github.com/1328836with) my initial guess in  
> > > Java.
> > > 
> > > If you run it, the output will be "{"mytype":{"properties":{}}}" , but  
> > > this  
> > > is obviously wrong, and If I try to index some documents with field  
> > > names  
> > > matching list\_\_\* or set\_\_\* they will be analyzed.  
> > > On the other side with the curl command everything works great.

---

<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:50am UTC](https://discuss.elastic.co/t/dynamic-templates-using-java-api/5730/5 "2017-07-06T03:50:16Z")

</div>


