# Using the java API to create/update mappings

**URL:** <https://discuss.elastic.co/t/using-the-java-api-to-create-update-mappings/4727>\
**Category:** Elasticsearch\
**Created:** [June 28, 2011, 8:23pm UTC](https://discuss.elastic.co/t/using-the-java-api-to-create-update-mappings/4727 "2011-06-28T20:23:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spring\_Ninja](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spring_ninja/32/2719_2.png) [@Spring\_Ninja](https://discuss.elastic.co/u/Spring_Ninja)\
**Post date:** [June 28, 2011, 8:23pm UTC](https://discuss.elastic.co/t/using-the-java-api-to-create-update-mappings/4727/1 "2011-06-28T20:23:09Z")

</div>

Hi all,

A couple questions on mapping definition. I run ES embedded in my  
application nodes.

1. Using the java API only, no source file, how do you initialize your  
mappings? What I want to do is change the "default" mapping so that  
all indexing and query parsing is done with this configuration.

2. When I index and build my JSon entity representation, I want to  
specify the tokenize and store attribute for each field individually.

3. Is there some example code I can check somewhere?

Here's what I have right now but I'm getting a 'mapping type is  
missing' exception...

Thanks for helping...

```
    // Initialize the ES embedded client.

    // Set path.data from the app.lucene.dir setting in

```

application.properties.

```
    Properties esProperties = new Properties();
    esProperties.putAll(configuration);
    esProperties.setProperty("path.data",

```

configuration.getProperty(ConfigurationConstants.PROP\_APP\_LUCENE\_DIR));

```
    Settings settings =

```

ImmutableSettings.settingsBuilder().put(esProperties).build();  
elasticSearchNode =  
NodeBuilder.nodeBuilder().settings(settings).node();  
elasticSearchClient = elasticSearchNode.client();

```
    // Default configuration.

    XContentBuilder mappings = null;
    try
    {
        mappings = XContentFactory.jsonBuilder();

```

mappings.startObject().startObject("analysis").startObject("analyzer").startObject("default")  
.field("type", "custom").field("tokenizer",  
"whitespace")  
.field("filter", new String[] { "snowball",  
"standard", "lowercase" }).endObject().endObject()  
.endObject().endObject();

```
        logger.info(mappings.string());
    }
    catch (IOException e)
    {
        ValidateState.fail();
    }

    PutMappingRequestBuilder mappingRequest =

```

elasticSearchClient.admin().indices().preparePutMapping("default");  
mappingRequest.setSource(mappings);  
PutMappingResponse mappingResponse =  
mappingRequest.execute().actionGet();

---

<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:** [June 29, 2011, 12:08pm UTC](https://discuss.elastic.co/t/using-the-java-api-to-create-update-mappings/4727/2 "2011-06-29T12:08:04Z")

</div>

You put the index configuration as a mapping for a type. Mappings are for document types, index settings are for things like custom analyzers.

On Tuesday, June 28, 2011 at 11:23 PM, Spring Ninja wrote:

> Hi all,
> 
> A couple questions on mapping definition. I run ES embedded in my  
> application nodes.
> 
> 1. Using the java API only, no source file, how do you initialize your  
> mappings? What I want to do is change the "default" mapping so that  
> all indexing and query parsing is done with this configuration.
> 
> 2. When I index and build my JSon entity representation, I want to  
> specify the tokenize and store attribute for each field individually.
> 
> 3. Is there some example code I can check somewhere?
> 
> Here's what I have right now but I'm getting a 'mapping type is  
> missing' exception...
> 
> Thanks for helping...
> 
> // Initialize the ES embedded client.
> 
> // Set path.data from the app.lucene.dir setting in  
> application.properties.
> 
> Properties esProperties = new Properties();  
> esProperties.putAll(configuration);  
> esProperties.setProperty("path.data",  
> configuration.getProperty(ConfigurationConstants.PROP\_APP\_LUCENE\_DIR));
> 
> Settings settings =  
> ImmutableSettings.settingsBuilder().put(esProperties).build();  
> elasticSearchNode =  
> NodeBuilder.nodeBuilder().settings(settings).node();  
> elasticSearchClient = elasticSearchNode.client();
> 
> // Default configuration.
> 
> XContentBuilder mappings = null;  
> try  
> {  
> mappings = XContentFactory.jsonBuilder();
> 
> mappings.startObject().startObject("analysis").startObject("analyzer").startObject("default")  
> .field("type", "custom").field("tokenizer",  
> "whitespace")  
> .field("filter", new String { "snowball",  
> "standard", "lowercase" }).endObject().endObject()  
> .endObject().endObject();
> 
> logger.info ([http://logger.info](http://logger.info))(mappings.string());  
> }  
> catch (IOException e)  
> {  
> ValidateState.fail();  
> }
> 
> PutMappingRequestBuilder mappingRequest =  
> elasticSearchClient.admin().indices().preparePutMapping("default");  
> mappingRequest.setSource(mappings);  
> PutMappingResponse mappingResponse =  
> mappingRequest.execute().actionGet();

---

<div class="post-metadata">

**Author:** ![Spring\_Ninja](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spring_ninja/32/2719_2.png) [@Spring\_Ninja](https://discuss.elastic.co/u/Spring_Ninja)\
**Post date:** [June 29, 2011, 1:47pm UTC](https://discuss.elastic.co/t/using-the-java-api-to-create-update-mappings/4727/3 "2011-06-29T13:47:16Z")

</div>

Yeah, that makes sense and... it works!!! 😉

Thanks!

Here's how I redefined the default analyser for those that may have  
the same need:

```
    XContentBuilder indexSettings = null;
    try
    {
        indexSettings = XContentFactory.jsonBuilder();

```

indexSettings.startObject().startObject("index").startObject("analysis").startObject("analyzer")  
.startObject("default").field("type",  
"custom").field("tokenizer", "whitespace")  
.field("filter", new String[] { "asciifolding",  
"lowercase" }).endObject().endObject().endObject()  
.endObject().endObject();

```
        logger.info(indexSettings.string());

        UpdateSettingsRequestBuilder settingsRequest =

```

elasticSearchClient.admin().indices()  
.prepareUpdateSettings();  
settingsRequest.setSettings(indexSettings.string());  
UpdateSettingsResponse settingsResponse =  
settingsRequest.execute().actionGet();  
}  
catch (IOException e)  
{  
ValidateState.fail();  
}

---

<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, 4:02am UTC](https://discuss.elastic.co/t/using-the-java-api-to-create-update-mappings/4727/4 "2017-07-06T04:02:15Z")

</div>


