# How to define mappings using Java API

**URL:** <https://discuss.elastic.co/t/how-to-define-mappings-using-java-api/6352>\
**Category:** Elasticsearch\
**Created:** [January 11, 2012, 6:59pm UTC](https://discuss.elastic.co/t/how-to-define-mappings-using-java-api/6352 "2012-01-11T18:59:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorrin\_Nelson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lorrin_nelson/32/3018_2.png) [@Lorrin\_Nelson](https://discuss.elastic.co/u/Lorrin_Nelson)\
**Post date:** [January 11, 2012, 6:59pm UTC](https://discuss.elastic.co/t/how-to-define-mappings-using-java-api/6352/1 "2012-01-11T18:59:38Z")

</div>

Could someone share a working example of using the Java API to define  
type mappings?

I'm trying things like:  
Settings.Builder indexSettings =  
ImmutableSettings.settingsBuilder().loadFromSource( XContentFactory.jsonBuilder()  
.startObject()  
.startObject("settings")  
.startObject("index")  
.field("number\_of\_shards","BOGUS")  
.endObject()  
.endObject()  
.startObject("mappings")  
.startObject("my\_type\_name")  
.startObject("sequence")  
.field("type",  
"boolean")  
.endObject()  
.startObject("message.text")  
.field("type",  
"string")  
.field("index",  
"no")  
.endObject()  
.endObject()  
.endObject()  
.endObject().string());

CreateIndexResponse createIndexResponse = client.admin().indices()  
.prepareCreate(newSlice).setSettings(indexSettings)  
.execute().actionGet();

I haven't managed to twiddle the builder structure such that they take  
effect. E.g. message.text is searchable despite not being index,  
sequence accepts numbers despite being boolean, and number\_of\_shards  
being set to BOGUS doesn't produce any errors.

Thanks  
-Lorrin

---

<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:** [January 11, 2012, 8:53pm UTC](https://discuss.elastic.co/t/how-to-define-mappings-using-java-api/6352/2 "2012-01-11T20:53:27Z")

</div>

Hi Lorrin,

Have a look here :  
[https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/elasticse](https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/elasticse)  
arch/river/rss/AbstractRssRiverTest.java  
Create the index with an abstract mapping() method

And here :  
[https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/elasticse](https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/elasticse)  
arch/river/rss/RssRiverTest.java  
(for an implementation of the mapping() method)

HTH  
David.

> -----Message d'origine-----  
> De : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
> [[mailto:elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)] De la part de Lorrin Nelson  
> Envoyé : mercredi 11 janvier 2012 20:00  
> À : elasticsearch  
> Objet : How to define mappings using Java API
> 
> Could someone share a working example of using the Java API to define  
> type mappings?
> 
> I'm trying things like:  
> Settings.Builder indexSettings =  
> ImmutableSettings.settingsBuilder().loadFromSource(  
> XContentFactory.jsonBuilder()  
> .startObject()  
> .startObject("settings")  
> .startObject("index")
> 
> .field("number\_of\_shards","BOGUS")  
> .endObject()  
> .endObject()  
> .startObject("mappings")
> 
> .startObject("my\_type\_name")
> 
> .startObject("sequence")  
> .field("type",  
> "boolean")  
> .endObject()
> 
> .startObject("message.text")  
> .field("type",  
> "string")  
> .field("index",  
> "no")  
> .endObject()  
> .endObject()  
> .endObject()  
> .endObject().string());
> 
> CreateIndexResponse createIndexResponse = client.admin().indices()  
> .prepareCreate(newSlice).setSettings(indexSettings)  
> .execute().actionGet();
> 
> I haven't managed to twiddle the builder structure such that they take  
> effect. E.g. message.text is searchable despite not being index,  
> sequence accepts numbers despite being boolean, and number\_of\_shards  
> being set to BOGUS doesn't produce any errors.
> 
> Thanks  
> -Lorrin

---

<div class="post-metadata">

**Author:** ![Lorrin\_Nelson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lorrin_nelson/32/3018_2.png) [@Lorrin\_Nelson](https://discuss.elastic.co/u/Lorrin_Nelson)\
**Post date:** [January 13, 2012, 1:09am UTC](https://discuss.elastic.co/t/how-to-define-mappings-using-java-api/6352/3 "2012-01-13T01:09:01Z")

</div>

Hi David,

That helped, thanks. After getting the builder stuff working following  
the example, I switched over to supplying the settings as a JSON  
string with the following command:  
CreateIndexResponse createIndexResponse =  
client.admin().indices()  
.prepareCreate(indexName)  
.setSettings(elasticSearchSettings)  
.addMapping(elasticSearchType,  
elasticSearchMappings)  
.execute().actionGet();

where elasticSearchSettings is:  
{  
"index" : {  
"number\_of\_shards" : 2,  
"number\_of\_replicas" : 1  
}  
}

and elasticSearchMappings is in the format  
{  
"" : {  
"properties" : {  
"\<column\_name\>" : {  
"type" : ...  
},  
...  
}  
}

I wasn't able to apply settings using a nested Java Map\<String,Object\>  
structure, but I'm happy with supplying the JSON.

Cheers  
-Lorrin

On 11 Jan., 12:53, "David Pilato" [da...@pilato.fr](mailto:da...@pilato.fr) wrote:

> Hi Lorrin,
> 
> Have a look here :[https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el](https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el)...  
> arch/river/rss/AbstractRssRiverTest.java  
> Create the index with an abstract mapping() method
> 
> And here :[https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el](https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el)...  
> arch/river/rss/RssRiverTest.java  
> (for an implementation of the mapping() method)
> 
> HTH  
> David.
> 
> > -----Message d'origine-----  
> > De : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
> > [[mailto:elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)] De la part de Lorrin Nelson  
> > Envoyé : mercredi 11 janvier 2012 20:00  
> > À : elasticsearch  
> > Objet : How to definemappingsusingJava API
> 
> > Could someone share a working example ofusingthe Java API to define  
> > typemappings?
> 
> > I'm trying things like:  
> > Settings.Builder indexSettings =  
> > ImmutableSettings.settingsBuilder().loadFromSource(  
> > XContentFactory.jsonBuilder()  
> > .startObject()  
> > .startObject("settings")  
> > .startObject("index")
> 
> > .field("number\_of\_shards","BOGUS")  
> > .endObject()  
> > .endObject()  
> > .startObject("mappings")
> 
> > .startObject("my\_type\_name")
> 
> > .startObject("sequence")  
> > .field("type",  
> > "boolean")  
> > .endObject()
> 
> > .startObject("message.text")  
> > .field("type",  
> > "string")  
> > .field("index",  
> > "no")  
> > .endObject()  
> > .endObject()  
> > .endObject()  
> > .endObject().string());
> 
> > CreateIndexResponse createIndexResponse = client.admin().indices()  
> > .prepareCreate(newSlice).setSettings(indexSettings)  
> > .execute().actionGet();
> 
> > I haven't managed to twiddle the builder structure such that they take  
> > effect. E.g. message.text is searchable despite not being index,  
> > sequence accepts numbers despite being boolean, and number\_of\_shards  
> > being set to BOGUS doesn't produce any errors.
> 
> > Thanks  
> > -Lorrin

---

<div class="post-metadata">

**Author:** ![Ethan\_Pailes](https://avatars.discourse-cdn.com/v4/letter/e/ba9def/32.png) [@Ethan\_Pailes](https://discuss.elastic.co/u/Ethan_Pailes)\
**Post date:** [May 8, 2014, 9:49pm UTC](https://discuss.elastic.co/t/how-to-define-mappings-using-java-api/6352/4 "2014-05-08T21:49:43Z")

</div>

This was quite helpful.

I do have one question though. Is elasticSearchMappings of type Settings?

Ethan

On Thursday, January 12, 2012 8:09:01 PM UTC-5, Lorrin Nelson wrote:

> Hi David,
> 
> That helped, thanks. After getting the builder stuff working following  
> the example, I switched over to supplying the settings as a JSON  
> string with the following command:  
> CreateIndexResponse createIndexResponse =  
> client.admin().indices()  
> .prepareCreate(indexName)  
> .setSettings(elasticSearchSettings)  
> .addMapping(elasticSearchType,  
> elasticSearchMappings)  
> .execute().actionGet();
> 
> where elasticSearchSettings is:  
> {  
> "index" : {  
> "number\_of\_shards" : 2,  
> "number\_of\_replicas" : 1  
> }  
> }
> 
> and elasticSearchMappings is in the format  
> {  
> "" : {  
> "properties" : {  
> "\<column\_name\>" : {  
> "type" : ...  
> },  
> ...  
> }  
> }
> 
> I wasn't able to apply settings using a nested Java Map\<String,Object\>  
> structure, but I'm happy with supplying the JSON.
> 
> Cheers  
> -Lorrin
> 
> On 11 Jan., 12:53, "David Pilato" [da...@pilato.fr](mailto:da...@pilato.fr) wrote:
> 
> > Hi Lorrin,
> > 
> > Have a look here :  
> > [https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el](https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el)...  
> > arch/river/rss/AbstractRssRiverTest.java  
> > Create the index with an abstract mapping() method
> > 
> > And here :  
> > [https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el](https://github.com/dadoonet/rssriver/blob/master/src/test/java/org/el)...  
> > arch/river/rss/RssRiverTest.java  
> > (for an implementation of the mapping() method)
> > 
> > HTH  
> > David.
> > 
> > > -----Message d'origine-----  
> > > De : [elasti...@googlegroups.com](mailto:elasti...@googlegroups.com) \<javascript:\>  
> > > [[mailto:elasti...@googlegroups.com](mailto:elasti...@googlegroups.com) \<javascript:\>] De la part de  
> > > Lorrin Nelson  
> > > Envoyé : mercredi 11 janvier 2012 20:00  
> > > À : elasticsearch  
> > > Objet : How to definemappingsusingJava API
> > 
> > > Could someone share a working example ofusingthe Java API to define  
> > > typemappings?
> > 
> > > I'm trying things like:  
> > > Settings.Builder indexSettings =  
> > > ImmutableSettings.settingsBuilder().loadFromSource(  
> > > XContentFactory.jsonBuilder()  
> > > .startObject()  
> > > .startObject("settings")  
> > > .startObject("index")
> > 
> > > .field("number\_of\_shards","BOGUS")  
> > > .endObject()  
> > > .endObject()  
> > > .startObject("mappings")
> > 
> > > .startObject("my\_type\_name")
> > 
> > > .startObject("sequence")  
> > > .field("type",  
> > > "boolean")  
> > > .endObject()
> > 
> > > .startObject("message.text")  
> > > .field("type",  
> > > "string")  
> > > .field("index",  
> > > "no")  
> > > .endObject()  
> > > .endObject()  
> > > .endObject()  
> > > .endObject().string());
> > 
> > > CreateIndexResponse createIndexResponse = client.admin().indices()  
> > > .prepareCreate(newSlice).setSettings(indexSettings)  
> > > .execute().actionGet();
> > 
> > > I haven't managed to twiddle the builder structure such that they take  
> > > effect. E.g. message.text is searchable despite not being index,  
> > > sequence accepts numbers despite being boolean, and number\_of\_shards  
> > > being set to BOGUS doesn't produce any errors.
> > 
> > > Thanks  
> > > -Lorrin

--  
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/12cd2570-66dd-4934-9daa-7f06d0879208%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/12cd2570-66dd-4934-9daa-7f06d0879208%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:30am UTC](https://discuss.elastic.co/t/how-to-define-mappings-using-java-api/6352/5 "2017-07-06T01:30:38Z")

</div>


