# Java API for mapping

**URL:** <https://discuss.elastic.co/t/java-api-for-mapping/3574>\
**Category:** Elasticsearch\
**Created:** [November 15, 2010, 8:21pm UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574 "2010-11-15T20:21:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvanBrusic](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IvanBrusic](https://discuss.elastic.co/u/IvanBrusic)\
**Post date:** [November 15, 2010, 8:21pm UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/1 "2010-11-15T20:21:08Z")

</div>

Perhaps I am missing something fundamental, but I cannot figure out  
how to add a mapping via the Java API. I can index and query  
documents, but I do not see the relevant methods in the client  
interface. You have prepareIndex, prepareSearch, prepareBulk, etc..,  
but I do not know which is applicable for mapping.

There is always the REST API, but I prefer to keep things in code for  
version control purposes. I can rollout a new version of the server  
and not have to remember to update the mappings.

If I must use the REST API, can I create a mapping at the type level  
(regardless of index)?

curl -XPUT '[http://localhost:9200/\_all/mytype/\_mapping](http://localhost:9200/_all/mytype/_mapping)' -d '  
{  
"mytype" : {  
"properties" : {  
"value" : {"type" : "string", "store" : "yes", "index" :  
"not\_analyzed"}  
}  
}  
}  
'

---

<div class="post-metadata">

**Author:** ![IvanBrusic](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IvanBrusic](https://discuss.elastic.co/u/IvanBrusic)\
**Post date:** [November 15, 2010, 8:22pm UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/2 "2010-11-15T20:22:45Z")

</div>

BTW, I found the answer to my last question in the docs. For those  
interested, yes you can use \_all.

On Nov 15, 3:21 pm, Ivan Brusic [ivan\_bru...@yahoo.com](mailto:ivan_bru...@yahoo.com) wrote:

> Perhaps I am missing something fundamental, but I cannot figure out  
> how to add a mapping via the Java API. I can index and query  
> documents, but I do not see the relevant methods in the client  
> interface. You have prepareIndex, prepareSearch, prepareBulk, etc..,  
> but I do not know which is applicable for mapping.
> 
> There is always the REST API, but I prefer to keep things in code for  
> version control purposes. I can rollout a new version of the server  
> and not have to remember to update the mappings.
> 
> If I must use the REST API, can I create a mapping at the type level  
> (regardless of index)?
> 
> curl -XPUT '[http://localhost:9200/\_all/mytype/\_mapping'-d](http://localhost:9200/_all/mytype/_mapping'-d) '  
> {  
> "mytype" : {  
> "properties" : {  
> "value" : {"type" : "string", "store" : "yes", "index" :  
> "not\_analyzed"}  
> }  
> }}
> 
> '

---

<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:** [November 15, 2010, 10:47pm UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/3 "2010-11-15T22:47:41Z")

</div>

There is the putMapping API in the client#admin#indices.

On Mon, Nov 15, 2010 at 10:22 PM, Ivan Brusic [ivan\_brusic@yahoo.com](mailto:ivan_brusic@yahoo.com) wrote:

> BTW, I found the answer to my last question in the docs. For those  
> interested, yes you can use \_all.
> 
> On Nov 15, 3:21 pm, Ivan Brusic [ivan\_bru...@yahoo.com](mailto:ivan_bru...@yahoo.com) wrote:
> 
> > Perhaps I am missing something fundamental, but I cannot figure out  
> > how to add a mapping via the Java API. I can index and query  
> > documents, but I do not see the relevant methods in the client  
> > interface. You have prepareIndex, prepareSearch, prepareBulk, etc..,  
> > but I do not know which is applicable for mapping.
> > 
> > There is always the REST API, but I prefer to keep things in code for  
> > version control purposes. I can rollout a new version of the server  
> > and not have to remember to update the mappings.
> > 
> > If I must use the REST API, can I create a mapping at the type level  
> > (regardless of index)?
> > 
> > curl -XPUT '[http://localhost:9200/\_all/mytype/\_mapping'-d](http://localhost:9200/_all/mytype/_mapping'-d) '  
> > {  
> > "mytype" : {  
> > "properties" : {  
> > "value" : {"type" : "string", "store" : "yes", "index" :  
> > "not\_analyzed"}  
> > }  
> > }}
> > 
> > '

---

<div class="post-metadata">

**Author:** ![IvanBrusic](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@IvanBrusic](https://discuss.elastic.co/u/IvanBrusic)\
**Post date:** [November 16, 2010, 2:34pm UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/4 "2010-11-16T14:34:22Z")

</div>

Thanks for the clarification. I was concentrating on the addMapping  
call in CreateIndexRequestBuilder and I couldn't figure out how to  
create a CreateIndexRequestBuilder from an IndexRequestBuilder. Did  
not scroll down to the putMapping method. 🙂

In the end, I found the method of using files for built-in custom  
mappings. This solution is even better since I can create my own roll  
out with the mapping files included and skip the explicit put mapping  
call on bootstrap. Awesome! However, before that I was struggling  
with the REST API. Putting a mapping to the \_all index had no effect  
on future indexing requests. You cannot put a mapping on an index  
that does not exist and an attempt to put a mapping after indexing  
returned the error:

{"error":"Merge failed with failures {[mapper [value] has different  
index values, mapper [value] has different store values, mapper  
[value] has different index\_analyzer, mapper [value] has different  
search\_analyzer]}"}

Built-in custom mappings work perfectly, but I was wondering, for  
future reference, how to get around the chicken-and-the-egg problem  
above?

Cheers,

Ivan

On Nov 15, 5:47 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> There is the putMapping API in the client#admin#indices.
> 
> On Mon, Nov 15, 2010 at 10:22 PM, Ivan Brusic [ivan\_bru...@yahoo.com](mailto:ivan_bru...@yahoo.com) wrote:
> 
> > BTW, I found the answer to my last question in the docs. For those  
> > interested, yes you can use \_all.
> 
> > On Nov 15, 3:21 pm, Ivan Brusic [ivan\_bru...@yahoo.com](mailto:ivan_bru...@yahoo.com) wrote:
> > 
> > > Perhaps I am missing something fundamental, but I cannot figure out  
> > > how to add a mapping via the Java API. I can index and query  
> > > documents, but I do not see the relevant methods in the client  
> > > interface. You have prepareIndex, prepareSearch, prepareBulk, etc..,  
> > > but I do not know which is applicable for mapping.
> 
> > > There is always the REST API, but I prefer to keep things in code for  
> > > version control purposes. I can rollout a new version of the server  
> > > and not have to remember to update the mappings.
> 
> > > If I must use the REST API, can I create a mapping at the type level  
> > > (regardless of index)?
> 
> > > curl -XPUT '[http://localhost:9200/\_all/mytype/\_mapping'-d'](http://localhost:9200/_all/mytype/_mapping'-d')  
> > > {  
> > > "mytype" : {  
> > > "properties" : {  
> > > "value" : {"type" : "string", "store" : "yes", "index" :  
> > > "not\_analyzed"}  
> > > }  
> > > }}
> 
> > > '

---

<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:** [November 17, 2010, 10:10am UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/5 "2010-11-17T10:10:26Z")

</div>

In "chicken and the egg" problem you mean creating an index and put mapping  
to it before any indexing has been done and the sync problem around it? The  
first thing is to allow to provide mappings in the REST create index  
operation.

-shay.banon

On Tue, Nov 16, 2010 at 4:34 PM, Ivan Brusic [ivan\_brusic@yahoo.com](mailto:ivan_brusic@yahoo.com) wrote:

> Thanks for the clarification. I was concentrating on the addMapping  
> call in CreateIndexRequestBuilder and I couldn't figure out how to  
> create a CreateIndexRequestBuilder from an IndexRequestBuilder. Did  
> not scroll down to the putMapping method. 🙂
> 
> In the end, I found the method of using files for built-in custom  
> mappings. This solution is even better since I can create my own roll  
> out with the mapping files included and skip the explicit put mapping  
> call on bootstrap. Awesome! However, before that I was struggling  
> with the REST API. Putting a mapping to the \_all index had no effect  
> on future indexing requests. You cannot put a mapping on an index  
> that does not exist and an attempt to put a mapping after indexing  
> returned the error:
> 
> {"error":"Merge failed with failures {[mapper [value] has different  
> index values, mapper [value] has different store values, mapper  
> [value] has different index\_analyzer, mapper [value] has different  
> search\_analyzer]}"}
> 
> Built-in custom mappings work perfectly, but I was wondering, for  
> future reference, how to get around the chicken-and-the-egg problem  
> above?
> 
> Cheers,
> 
> Ivan
> 
> On Nov 15, 5:47 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > There is the putMapping API in the client#admin#indices.
> > 
> > On Mon, Nov 15, 2010 at 10:22 PM, Ivan Brusic [ivan\_bru...@yahoo.com](mailto:ivan_bru...@yahoo.com)  
> > wrote:
> > 
> > > BTW, I found the answer to my last question in the docs. For those  
> > > interested, yes you can use \_all.
> > 
> > > On Nov 15, 3:21 pm, Ivan Brusic [ivan\_bru...@yahoo.com](mailto:ivan_bru...@yahoo.com) wrote:
> > > 
> > > > Perhaps I am missing something fundamental, but I cannot figure out  
> > > > how to add a mapping via the Java API. I can index and query  
> > > > documents, but I do not see the relevant methods in the client  
> > > > interface. You have prepareIndex, prepareSearch, prepareBulk, etc..,  
> > > > but I do not know which is applicable for mapping.
> > 
> > > > There is always the REST API, but I prefer to keep things in code for  
> > > > version control purposes. I can rollout a new version of the server  
> > > > and not have to remember to update the mappings.
> > 
> > > > If I must use the REST API, can I create a mapping at the type level  
> > > > (regardless of index)?
> > 
> > > > curl -XPUT '[http://localhost:9200/\_all/mytype/\_mapping'-d'](http://localhost:9200/_all/mytype/_mapping'-d')  
> > > > {  
> > > > "mytype" : {  
> > > > "properties" : {  
> > > > "value" : {"type" : "string", "store" : "yes", "index" :  
> > > > "not\_analyzed"}  
> > > > }  
> > > > }}
> > 
> > > > '

---

<div class="post-metadata">

**Author:** ![John\_Chang](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@John\_Chang](https://discuss.elastic.co/u/John_Chang)\
**Post date:** [December 7, 2010, 9:37pm UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/6 "2010-12-07T21:37:41Z")

</div>

Is there a java api for getting the mappings? Something like a java api that does this:  
[http://www.elasticsearch.com/docs/elasticsearch/rest\_api/admin/indices/get\_mapping/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/get_mapping/)

I want to have my service start up and query the mappings; if they are not as expected, I want the service to log errors and shutdown. We've had problems with services coming up and they are not running against the mappings they thought they had.

If what I am looking for does not exist, could you give any other advice on how to sanity check the mappings upon application startup? Or perhaps other programming/operational tips on making sure the correct mappings are in place?

Thanks.

---

<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:** [December 7, 2010, 9:46pm UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/7 "2010-12-07T21:46:29Z")

</div>

All the REST APIs use the Java APIs internally. The get mapping is simply using the cluster state API, to get the relevant index information, which also has the mappings there.

On Tuesday, December 7, 2010 at 11:37 PM, John Chang wrote:

> Is there a java api for getting the mappings? Something like a java api that  
> does this:  
> [http://www.elasticsearch.com/docs/elasticsearch/rest\_api/admin/indices/get\_mapping/](http://www.elasticsearch.com/docs/elasticsearch/rest_api/admin/indices/get_mapping/)
> 
> I want to have my service start up and query the mappings; if they are not  
> as expected, I want the service to log errors and shutdown. We've had  
> problems with services coming up and they are not running against the  
> mappings they thought they had.
> 
> If what I am looking for does not exist, could you give any other advice on  
> how to sanity check the mappings upon application startup? Or perhaps other  
> programming/operational tips on making sure the correct mappings are in  
> place?
> 
> ## Thanks.
> 
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Java-API-for-mapping-tp1906936p2036369.html](http://elasticsearch-users.115913.n3.nabble.com/Java-API-for-mapping-tp1906936p2036369.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![samCougars](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@samCougars](https://discuss.elastic.co/u/samCougars)\
**Post date:** [May 30, 2012, 5:24pm UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/8 "2012-05-30T17:24:49Z")

</div>

I'm trying to add a mapping for sorting while creating index.  
I'm indexing on two fields title and abbreviation. When I do a search I want to sort the result on title or abbreviation. Both the fields can have multi text values like title can "Harry Potter And Adventures" or abbreviation can be "HP AD And War".  
While during search, if I only add sort as (searchRequestBuilder.addSort("title",SearchOrder.ASC)) then it gave me an error saying that "Can't sort on string types with more than one value per doc, or more than one token per field". I know I'm getting this error because my indexed field "title" is "not\_analyzed". I'm creating index as  
client.prepareIndex("elasticSearch", "myFields", "1000").setSource(myContentBuilder()).execute().actionGet().

And myContentBuilder() creates a XcontentBuilder like  
XContentBuilder myContentBuilder()  
{  
Map myValues = new HashMap();  
myValues.put("title","Harry Potter And Adventures");

myValues.put("abbreviation", "HP AD And War");  
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("title").field("index", "not\_analyzed").field("type", "string").endObject().endObject();  
return builder.map(myValues);  
}

I think there is some issues w/ my XConterBuilder (" XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("title").field("index", "not\_analyzed").field("type", "string").endObject().endObject()") but I can't think of any issue on it. It does not throw an error while creating an index but when I do a search it still throws "Can't sort on string types with more than one value per doc, or more than one token per field".  
Can anyone think of the issue while I'm creating builder to add as a mapping to achieve sorting on title?

---

<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:26am UTC](https://discuss.elastic.co/t/java-api-for-mapping/3574/9 "2017-07-06T03:26:15Z")

</div>


