# Bug in Java PutMapping API?

**URL:** <https://discuss.elastic.co/t/bug-in-java-putmapping-api/16045>\
**Category:** Elasticsearch\
**Created:** [February 26, 2014, 11:51pm UTC](https://discuss.elastic.co/t/bug-in-java-putmapping-api/16045 "2014-02-26T23:51:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andre\_Encarnacao](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Andre\_Encarnacao](https://discuss.elastic.co/u/Andre_Encarnacao)\
**Post date:** [February 26, 2014, 11:51pm UTC](https://discuss.elastic.co/t/bug-in-java-putmapping-api/16045/1 "2014-02-26T23:51:32Z")

</div>

I am trying to use the Java API (v1.0.0) to create a custom mapping for my  
index but have run into a problem. More specifically, some of my mapping  
fields (for example, "format" and "index") are not being stored as part of  
the mapping in Elastic Search. In fact, the only field that is being stored  
is the "type" field. However, if I use the CURL API, everything works as  
expected and all fields are stored properly in my mapping. Has anyone seen  
this problem when using the Java API? Is this a bug? I put together a very  
simple example that demonstrates my bug below:

--- SOURCE CODE ---

Client client = new TransportClient().addTransportAddress(new  
InetSocketTransportAddress(\_hostname,\_port));

XContentBuilder mapping = XContentFactory.jsonBuilder()  
.startObject().startObject("type\_name")  
.startObject("properties")  
.startObject("ATTRIBUTE1")  
.field("type","string")  
.field("format","dateOptionalTime")  
.endObject()  
.startObject("ATTRIBUTE2")  
.field("type","long")  
.field("index","not\_analyzed")  
.endObject()  
.endObject()  
.endObject()  
.endObject();

client.admin().indices().preparePutMapping("index\_name").setType("type\_name").setSource(mapping).execute().actionGet();

--- OUTPUT ---

Below is the mapping created in ES after running the code snippet above  
(from [http://localhost:9200/index\_name/\_mapping?pretty](http://localhost:9200/index_name/_mapping?pretty)). Notice that only  
the "type" fields were stored in the mapping.  
{  
"index\_name" : {  
"mappings" : {  
"type\_name" : {  
"properties" : {  
"ATTRIBUTE1" : {  
"type" : "string"  
},  
"ATTRIBUTE2" : {  
"type" : "long"  
}  
}  
}  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d8a1ae1b-bb29-45d7-8387-e938b1c0cedc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d8a1ae1b-bb29-45d7-8387-e938b1c0cedc%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 27, 2014, 1:33am UTC](https://discuss.elastic.co/t/bug-in-java-putmapping-api/16045/2 "2014-02-27T01:33:57Z")

</div>

It looks good to me.  
Could you check the response?

```
            PutMappingResponse response = client.admin().indices()
                    .preparePutMapping(index)
                    .setType(type)
                    .setSource(xcontent)
                    .execute().actionGet();
            if (!response.isAcknowledged()) {
                throw new Exception("Could not define mapping for type [" + index + "]/[" + type + "].");
            } 

```

May be there is something else wrong in your actual code. Could you gist your full code?

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 27 févr. 2014 à 00:51, Andre Encarnacao [andre.v.encarnacao@gmail.com](mailto:andre.v.encarnacao@gmail.com) a écrit :

I am trying to use the Java API (v1.0.0) to create a custom mapping for my index but have run into a problem. More specifically, some of my mapping fields (for example, "format" and "index") are not being stored as part of the mapping in Elastic Search. In fact, the only field that is being stored is the "type" field. However, if I use the CURL API, everything works as expected and all fields are stored properly in my mapping. Has anyone seen this problem when using the Java API? Is this a bug? I put together a very simple example that demonstrates my bug below:

--- SOURCE CODE ---

Client client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(\_hostname,\_port));

XContentBuilder mapping = XContentFactory.jsonBuilder()  
.startObject().startObject("type\_name")  
.startObject("properties")  
.startObject("ATTRIBUTE1")  
.field("type","string")  
.field("format","dateOptionalTime")  
.endObject()  
.startObject("ATTRIBUTE2")  
.field("type","long")  
.field("index","not\_analyzed")  
.endObject()  
.endObject()  
.endObject()  
.endObject();

client.admin().indices().preparePutMapping("index\_name").setType("type\_name").setSource(mapping).execute().actionGet();

--- OUTPUT ---

Below is the mapping created in ES after running the code snippet above (from [http://localhost:9200/index\_name/\_mapping?pretty](http://localhost:9200/index_name/_mapping?pretty)). Notice that only the "type" fields were stored in the mapping.  
{  
"index\_name" : {  
"mappings" : {  
"type\_name" : {  
"properties" : {  
"ATTRIBUTE1" : {  
"type" : "string"  
},  
"ATTRIBUTE2" : {  
"type" : "long"  
}  
}  
}  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d8a1ae1b-bb29-45d7-8387-e938b1c0cedc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d8a1ae1b-bb29-45d7-8387-e938b1c0cedc%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/C476001D-D1F7-4518-A2ED-88A1DC080F4C%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/C476001D-D1F7-4518-A2ED-88A1DC080F4C%40pilato.fr).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Tim\_S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tim_s/32/958_2.png) [@Tim\_S](https://discuss.elastic.co/u/Tim_S)\
**Post date:** [February 27, 2014, 8:24am UTC](https://discuss.elastic.co/t/bug-in-java-putmapping-api/16045/3 "2014-02-27T08:24:27Z")

</div>

@Andre might be a stupid question, but why are you trying to set a format  
for a string field and set 'index' on a long field? Afaics from the guide  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/mapping-core-types.html))  
neither of these things are supported, so is it that surprising that  
elasticsearch drops the invalid attributes?

On Wednesday, February 26, 2014 11:51:32 PM UTC, Andre Encarnacao wrote:

> I am trying to use the Java API (v1.0.0) to create a custom mapping for my  
> index but have run into a problem. More specifically, some of my mapping  
> fields (for example, "format" and "index") are not being stored as part of  
> the mapping in Elastic Search. In fact, the only field that is being stored  
> is the "type" field. However, if I use the CURL API, everything works as  
> expected and all fields are stored properly in my mapping. Has anyone seen  
> this problem when using the Java API? Is this a bug? I put together a very  
> simple example that demonstrates my bug below:
> 
> --- SOURCE CODE ---
> 
> Client client = new TransportClient().addTransportAddress(new  
> InetSocketTransportAddress(\_hostname,\_port));
> 
> XContentBuilder mapping = XContentFactory.jsonBuilder()  
> .startObject().startObject("type\_name")  
> .startObject("properties")  
> .startObject("ATTRIBUTE1")  
> .field("type","string")  
> .field("format","dateOptionalTime")  
> .endObject()  
> .startObject("ATTRIBUTE2")  
> .field("type","long")  
> .field("index","not\_analyzed")  
> .endObject()  
> .endObject()  
> .endObject()  
> .endObject();
> 
> client.admin().indices().preparePutMapping("index\_name").setType("type\_name").setSource(mapping).execute().actionGet();
> 
> --- OUTPUT ---
> 
> Below is the mapping created in ES after running the code snippet above  
> (from [http://localhost:9200/index\_name/\_mapping?pretty](http://localhost:9200/index_name/_mapping?pretty)). Notice that only  
> the "type" fields were stored in the mapping.  
> {  
> "index\_name" : {  
> "mappings" : {  
> "type\_name" : {  
> "properties" : {  
> "ATTRIBUTE1" : {  
> "type" : "string"  
> },  
> "ATTRIBUTE2" : {  
> "type" : "long"  
> }  
> }  
> }  
> }  
> }  
> }

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fae41878-ce9d-41ea-9f84-24acd0657095%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fae41878-ce9d-41ea-9f84-24acd0657095%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Andre\_Encarnacao](https://avatars.discourse-cdn.com/v4/letter/a/9f8e36/32.png) [@Andre\_Encarnacao](https://discuss.elastic.co/u/Andre_Encarnacao)\
**Post date:** [February 28, 2014, 4:17pm UTC](https://discuss.elastic.co/t/bug-in-java-putmapping-api/16045/4 "2014-02-28T16:17:11Z")

</div>

David and Tim:

Thanks for the quick feedback. It turns out that Tim was correct in that ES  
was dropping the invalid attributes from my custom mapping. Thanks for  
pointing that out and for providing a reference to the mapping core types  
guide (very useful). I'm quickly getting my feet wet with ES and will  
likely run into more issues like this in the future. I appreciate the help.

Thanks,  
Andre

On Thu, Feb 27, 2014 at 1:24 AM, Tim S [timstibbs@gmail.com](mailto:timstibbs@gmail.com) wrote:

> @Andre might be a stupid question, but why are you trying to set a format  
> for a string field and set 'index' on a long field? Afaics from the guide (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/mapping-core-types.html))  
> neither of these things are supported, so is it that surprising that  
> elasticsearch drops the invalid attributes?
> 
> On Wednesday, February 26, 2014 11:51:32 PM UTC, Andre Encarnacao wrote:
> 
> > I am trying to use the Java API (v1.0.0) to create a custom mapping for  
> > my index but have run into a problem. More specifically, some of my mapping  
> > fields (for example, "format" and "index") are not being stored as part of  
> > the mapping in Elastic Search. In fact, the only field that is being stored  
> > is the "type" field. However, if I use the CURL API, everything works as  
> > expected and all fields are stored properly in my mapping. Has anyone seen  
> > this problem when using the Java API? Is this a bug? I put together a very  
> > simple example that demonstrates my bug below:
> > 
> > --- SOURCE CODE ---
> > 
> > Client client = new TransportClient().addTransportAddress(new  
> > InetSocketTransportAddress(\_hostname,\_port));
> > 
> > XContentBuilder mapping = XContentFactory.jsonBuilder()  
> > .startObject().startObject("type\_name")  
> > .startObject("properties")  
> > .startObject("ATTRIBUTE1")  
> > .field("type","string")  
> > .field("format","dateOptionalTime")  
> > .endObject()  
> > .startObject("ATTRIBUTE2")  
> > .field("type","long")  
> > .field("index","not\_analyzed")  
> > .endObject()  
> > .endObject()  
> > .endObject()  
> > .endObject();
> > 
> > client.admin().indices().preparePutMapping("index\_name"  
> > ).setType("type\_name").setSource(mapping).execute().actionGet();
> > 
> > --- OUTPUT ---
> > 
> > Below is the mapping created in ES after running the code snippet above  
> > (from [http://localhost:9200/index\_name/\_mapping?pretty](http://localhost:9200/index_name/_mapping?pretty)). Notice that  
> > only the "type" fields were stored in the mapping.  
> > {  
> > "index\_name" : {  
> > "mappings" : {  
> > "type\_name" : {  
> > "properties" : {  
> > "ATTRIBUTE1" : {  
> > "type" : "string"  
> > },  
> > "ATTRIBUTE2" : {  
> > "type" : "long"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/KYNGZljgCO8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/KYNGZljgCO8/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/fae41878-ce9d-41ea-9f84-24acd0657095%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fae41878-ce9d-41ea-9f84-24acd0657095%40googlegroups.com)  
> > .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAM4eDdX%3DkyOfAvxp0-YvOtwc2nh0cixtatupV57X\_1XQGov1RA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAM4eDdX%3DkyOfAvxp0-YvOtwc2nh0cixtatupV57X_1XQGov1RA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:46am UTC](https://discuss.elastic.co/t/bug-in-java-putmapping-api/16045/5 "2017-07-06T01:46:32Z")

</div>


