# Elasticsearch index mapping in java

**URL:** https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591
**Category:** Elasticsearch
**Created:** [February 4, 2014, 2:27pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591 "2014-02-04T14:27:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Doru\_Sular](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Doru\_Sular](https://discuss.elastic.co/u/Doru_Sular)
#### Post date: [February 4, 2014, 2:27pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591/1 "2014-02-04T14:27:41Z")

</div>

Hi guys,

I am trying to create an index with the following code:  
XContentBuilder source = XContentFactory.jsonBuilder().startObject()//  
.startObject("settings")  
.field("number\_of\_shards", 1)  
.endObject()// end settings  
.startObject("mappings")  
.startObject(INDEX\_TYPE)//  
.startObject("properties")//  
.startObject("user")  
.field("type", "string") // start user  
.field("store", "yes")  
.field("index", "analyzed")//  
.endObject()// end user  
.startObject("postDate")//  
.field("type", "date")  
.field("store", "yes")  
.field("index", "analyzed")//  
.endObject()// end post date  
.startObject("message") //  
.field("type", "string")  
.field("store", "yes")  
.field("index", "not\_analyzed")  
.endObject() // end user field  
.endObject() // end properties  
.endObject() // end index type  
.endObject() // end mappings  
.endObject(); // end the container object

```
    IndexResponse response = this.client.prepareIndex(INDEX, INDEX_TYPE

```

).setSource(source)  
.setType(INDEX\_TYPE).execute()  
.actionGet();

I want to have the "message" field not analyzed, because later I want to  
use facets to obtain unique messages.  
Unfortunately my code seems to add just a document in index with the  
following structure:  
{  
"settings": {  
"number\_of\_shards": 1  
},  
"mappings": {  
"tweet": {  
"properties": {  
"user": {  
"type": "string",  
"store": "yes",  
"index": "analyzed"  
},  
"postDate": {  
"type": "date",  
"store": "yes",  
"index": "analyzed"  
},  
"message": {  
"type": "string",  
"store": "yes",  
"index": "not\_analyzed"  
}  
}  
}  
}  
}

Please help me to spot the error, it seems that mapping are not created.  
Thank you very much,  
Doru

--  
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/d6635c65-41e5-43e9-b477-908f320127c5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d6635c65-41e5-43e9-b477-908f320127c5%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: ![Kevin\_Wang](https://avatars.discourse-cdn.com/v4/letter/k/f6c823/32.png) [@Kevin\_Wang](https://discuss.elastic.co/u/Kevin_Wang)
#### Post date: [February 5, 2014, 1:00am UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591/2 "2014-02-05T01:00:30Z")

</div>

The index request is used to index document, you should use put mapping  
request.

e,g,  
PutMappingResponse response =  
client.admin().indices().preparePutMapping(INDEX).setType(INDEX\_TYPE).setSource(source).get();

On Wednesday, February 5, 2014 1:27:41 AM UTC+11, Doru Sular wrote:

> Hi guys,
> 
> I am trying to create an index with the following code:  
> XContentBuilder source = XContentFactory.jsonBuilder().startObject()//  
> .startObject("settings")  
> .field("number\_of\_shards", 1)  
> .endObject()// end settings  
> .startObject("mappings")  
> .startObject(INDEX\_TYPE)//  
> .startObject("properties")//  
> .startObject("user")  
> .field("type", "string") // start user  
> .field("store", "yes")  
> .field("index", "analyzed")//  
> .endObject()// end user  
> .startObject("postDate")//  
> .field("type", "date")  
> .field("store", "yes")  
> .field("index", "analyzed")//  
> .endObject()// end post date  
> .startObject("message") //  
> .field("type", "string")  
> .field("store", "yes")  
> .field("index", "not\_analyzed")  
> .endObject() // end user field  
> .endObject() // end properties  
> .endObject() // end index type  
> .endObject() // end mappings  
> .endObject(); // end the container object
> 
> ```
> IndexResponse response = this.client.prepareIndex(INDEX,INDEX_TYPE
> 
> ```
> 
> ).setSource(source)  
> .setType(INDEX\_TYPE).execute()  
> .actionGet();
> 
> I want to have the "message" field not analyzed, because later I want to  
> use facets to obtain unique messages.  
> Unfortunately my code seems to add just a document in index with the  
> following structure:  
> {  
> "settings": {  
> "number\_of\_shards": 1  
> },  
> "mappings": {  
> "tweet": {  
> "properties": {  
> "user": {  
> "type": "string",  
> "store": "yes",  
> "index": "analyzed"  
> },  
> "postDate": {  
> "type": "date",  
> "store": "yes",  
> "index": "analyzed"  
> },  
> "message": {  
> "type": "string",  
> "store": "yes",  
> "index": "not\_analyzed"  
> }  
> }  
> }  
> }  
> }
> 
> Please help me to spot the error, it seems that mapping are not created.  
> Thank you very much,  
> Doru

--  
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/38ae7a3e-b8ef-4a05-8f7a-5ff20917f85e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/38ae7a3e-b8ef-4a05-8f7a-5ff20917f85e%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: ![Nikita\_Tovstoles](https://avatars.discourse-cdn.com/v4/letter/n/f17d59/32.png) [@Nikita\_Tovstoles](https://discuss.elastic.co/u/Nikita_Tovstoles)
#### Post date: [March 13, 2014, 4:58pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591/3 "2014-03-13T16:58:56Z")

</div>

Hi, Kevin:

Create Index ref[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html#mappings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html#mappings)says mappings can be included in index settings JSON. Are you saying that's  
not supported by the Java client? (Fwiw, I am seeing the same - just wanted  
to confirm):  
the following settings successfully create index with predefined mappings  
when using curl put /idx -d @settings.json but not when fed to java client  
like so:

```
    Settings settings = 

```

ImmutableSettings.settingsBuilder().loadFromClasspath("es\_admin\_idx\_settings.json").build();  
return  
getClient().admin().indices().prepareCreate(idxName).setSettings(settings).execute();

INFO metadata:114 - [Seth] [idx-elasticsearchserviceintegrationtest]  
creating index, cause [api], shards [2]/[0], mappings //EMPTY MAPPINGS

{  
"index" : {  
"number\_of\_shards" : 1,  
"number\_of\_replicas": 0  
},  
"mappings": {  
"eligibility\_criteria": {  
"properties": {  
"name": {  
"type" : "string",  
"fields": {  
"raw": {  
"type" : "string",  
"index": "not\_analyzed"  
}  
}  
}  
}  
}  
}  
}

On Tuesday, February 4, 2014 5:00:30 PM UTC-8, Kevin Wang wrote:

> The index request is used to index document, you should use put mapping  
> request.
> 
> e,g,  
> PutMappingResponse response =  
> client.admin().indices().preparePutMapping(INDEX).setType(INDEX\_TYPE).setSource(source).get();
> 
> On Wednesday, February 5, 2014 1:27:41 AM UTC+11, Doru Sular wrote:
> 
> > Hi guys,
> > 
> > I am trying to create an index with the following code:  
> > XContentBuilder source = XContentFactory.jsonBuilder().startObject()  
> > //  
> > .startObject("settings")  
> > .field("number\_of\_shards", 1)  
> > .endObject()// end settings  
> > .startObject("mappings")  
> > .startObject(INDEX\_TYPE)//  
> > .startObject("properties")//  
> > .startObject("user")  
> > .field("type", "string") // start user  
> > .field("store", "yes")  
> > .field("index", "analyzed")//  
> > .endObject()// end user  
> > .startObject("postDate")//  
> > .field("type", "date")  
> > .field("store", "yes")  
> > .field("index", "analyzed")//  
> > .endObject()// end post date  
> > .startObject("message") //  
> > .field("type", "string")  
> > .field("store", "yes")  
> > .field("index", "not\_analyzed")  
> > .endObject() // end user field  
> > .endObject() // end properties  
> > .endObject() // end index type  
> > .endObject() // end mappings  
> > .endObject(); // end the container object
> > 
> > ```
> > IndexResponse response = this.client.prepareIndex(INDEX,INDEX_TYPE
> > 
> > ```
> > 
> > ).setSource(source)  
> > .setType(INDEX\_TYPE).execute()  
> > .actionGet();
> > 
> > I want to have the "message" field not analyzed, because later I want to  
> > use facets to obtain unique messages.  
> > Unfortunately my code seems to add just a document in index with the  
> > following structure:  
> > {  
> > "settings": {  
> > "number\_of\_shards": 1  
> > },  
> > "mappings": {  
> > "tweet": {  
> > "properties": {  
> > "user": {  
> > "type": "string",  
> > "store": "yes",  
> > "index": "analyzed"  
> > },  
> > "postDate": {  
> > "type": "date",  
> > "store": "yes",  
> > "index": "analyzed"  
> > },  
> > "message": {  
> > "type": "string",  
> > "store": "yes",  
> > "index": "not\_analyzed"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > Please help me to spot the error, it seems that mapping are not created.  
> > Thank you very much,  
> > Doru

--  
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/c04558a1-5faa-48eb-8861-0145d4d8c5e3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c04558a1-5faa-48eb-8861-0145d4d8c5e3%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)
#### Post date: [March 13, 2014, 8:08pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591/4 "2014-03-13T20:08:52Z")

</div>

prepareIndex() is to index a document. What you want is prepareCreate(). I  
have an example here (check the method createIndexFullMapping()):

> <https://github.com/bly2k/es-java-examples/blob/master/admin/IndexAdminExample.java>

--  
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/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Nikita\_Tovstoles](https://avatars.discourse-cdn.com/v4/letter/n/f17d59/32.png) [@Nikita\_Tovstoles](https://discuss.elastic.co/u/Nikita_Tovstoles)
#### Post date: [March 13, 2014, 8:26pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591/5 "2014-03-13T20:26:06Z")

</div>

yep, that's what i used (see my prior post)

On Thu, Mar 13, 2014 at 1:08 PM, Binh Ly [binhly\_es@yahoo.com](mailto:binhly_es@yahoo.com) wrote:

> prepareIndex() is to index a document. What you want is prepareCreate(). I  
> have an example here (check the method createIndexFullMapping()):
> 
> [https://github.com/bly2k/es-java-examples/blob/master/admin/IndexAdminExample.java](https://github.com/bly2k/es-java-examples/blob/master/admin/IndexAdminExample.java)
> 
> --  
> 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/NjBqXhwloq4/unsubscribe](https://groups.google.com/d/topic/elasticsearch/NjBqXhwloq4/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/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/ef4c75f2-4f29-4fc2-92ea-79077df54a9c%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAJwaA224\_AMwCoLRUTWJj6JLzrqJKRFhdNFeGByf4%2Bye3g18sQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAJwaA224_AMwCoLRUTWJj6JLzrqJKRFhdNFeGByf4%2Bye3g18sQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)
#### Post date: [March 13, 2014, 8:28pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591/6 "2014-03-13T20:28:18Z")

</div>

🙂 I was confused. I meant to reply to OP. Thanks!

--  
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/96f1a30f-f4ba-4cbf-9d05-680ac25e1086%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/96f1a30f-f4ba-4cbf-9d05-680ac25e1086%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Nikita\_Tovstoles](https://avatars.discourse-cdn.com/v4/letter/n/f17d59/32.png) [@Nikita\_Tovstoles](https://discuss.elastic.co/u/Nikita_Tovstoles)
#### Post date: [March 13, 2014, 8:50pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591/7 "2014-03-13T20:50:31Z")

</div>

fwiw, I fixed my issue below by using prepareCreate().setSource() - rather  
than .setSettings() with idx config in the following format:

{"settings" : {  
"index": {  
"number\_of\_shards" : 1,  
"number\_of\_replicas": 0,  
"analysis" : {  
"analyzer": {  
"lowercase\_keyword": {  
"tokenizer": "keyword",  
"filter" : ["lowercase"]  
}  
}  
}  
}  
}, "mappings": {  
"type1": {  
"properties": {  
"name": {  
"type" : "string",  
"index": "not\_analyzed"  
}  
}  
}  
}}

On Thursday, March 13, 2014 9:58:56 AM UTC-7, Nikita Tovstoles wrote:

> Hi, Kevin:
> 
> Create Index ref[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html#mappings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html#mappings)says mappings can be included in index settings JSON. Are you saying that's  
> not supported by the Java client? (Fwiw, I am seeing the same - just wanted  
> to confirm):  
> the following settings successfully create index with predefined mappings  
> when using curl put /idx -d @settings.json but not when fed to java client  
> like so:
> 
> ```
> Settings settings = 
> 
> ```
> 
> ImmutableSettings.settingsBuilder().loadFromClasspath("es\_admin\_idx\_settings.json").build();  
> return  
> getClient().admin().indices().prepareCreate(idxName).setSettings(settings).execute();
> 
> INFO metadata:114 - [Seth] [idx-elasticsearchserviceintegrationtest]  
> creating index, cause [api], shards [2]/[0], mappings //EMPTY MAPPINGS
> 
> {  
> "index" : {  
> "number\_of\_shards" : 1,  
> "number\_of\_replicas": 0  
> },  
> "mappings": {  
> "eligibility\_criteria": {  
> "properties": {  
> "name": {  
> "type" : "string",  
> "fields": {  
> "raw": {  
> "type" : "string",  
> "index": "not\_analyzed"  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> On Tuesday, February 4, 2014 5:00:30 PM UTC-8, Kevin Wang wrote:
> 
> > The index request is used to index document, you should use put mapping  
> > request.
> > 
> > e,g,  
> > PutMappingResponse response =  
> > client.admin().indices().preparePutMapping(INDEX).setType(INDEX\_TYPE).setSource(source).get();
> > 
> > On Wednesday, February 5, 2014 1:27:41 AM UTC+11, Doru Sular wrote:
> > 
> > > Hi guys,
> > > 
> > > I am trying to create an index with the following code:  
> > > XContentBuilder source = XContentFactory.jsonBuilder().startObject()  
> > > //  
> > > .startObject("settings")  
> > > .field("number\_of\_shards", 1)  
> > > .endObject()// end settings  
> > > .startObject("mappings")  
> > > .startObject(INDEX\_TYPE)//  
> > > .startObject("properties")//  
> > > .startObject("user")  
> > > .field("type", "string") // start user  
> > > .field("store", "yes")  
> > > .field("index", "analyzed")//  
> > > .endObject()// end user  
> > > .startObject("postDate")//  
> > > .field("type", "date")  
> > > .field("store", "yes")  
> > > .field("index", "analyzed")//  
> > > .endObject()// end post date  
> > > .startObject("message") //  
> > > .field("type", "string")  
> > > .field("store", "yes")  
> > > .field("index", "not\_analyzed")  
> > > .endObject() // end user field  
> > > .endObject() // end properties  
> > > .endObject() // end index type  
> > > .endObject() // end mappings  
> > > .endObject(); // end the container object
> > > 
> > > ```
> > > IndexResponse response = this.client.prepareIndex(INDEX,INDEX_TYPE
> > > 
> > > ```
> > > 
> > > ).setSource(source)  
> > > .setType(INDEX\_TYPE).execute()  
> > > .actionGet();
> > > 
> > > I want to have the "message" field not analyzed, because later I want to  
> > > use facets to obtain unique messages.  
> > > Unfortunately my code seems to add just a document in index with the  
> > > following structure:  
> > > {  
> > > "settings": {  
> > > "number\_of\_shards": 1  
> > > },  
> > > "mappings": {  
> > > "tweet": {  
> > > "properties": {  
> > > "user": {  
> > > "type": "string",  
> > > "store": "yes",  
> > > "index": "analyzed"  
> > > },  
> > > "postDate": {  
> > > "type": "date",  
> > > "store": "yes",  
> > > "index": "analyzed"  
> > > },  
> > > "message": {  
> > > "type": "string",  
> > > "store": "yes",  
> > > "index": "not\_analyzed"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > Please help me to spot the error, it seems that mapping are not created.  
> > > Thank you very much,  
> > > Doru

--  
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/8d0946a7-be01-4962-bfbb-f43ac303275a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8d0946a7-be01-4962-bfbb-f43ac303275a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:43am UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-in-java/15591/8 "2017-07-06T01:43:18Z")

</div>


