# \[Java API\] Cannot create index with meta data

**URL:** <https://discuss.elastic.co/t/java-api-cannot-create-index-with-meta-data/4474>\
**Category:** Elasticsearch\
**Created:** [May 25, 2011, 2:07pm UTC](https://discuss.elastic.co/t/java-api-cannot-create-index-with-meta-data/4474 "2011-05-25T14:07:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ToBe\_HH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tobe_hh/32/3221_2.png) [@ToBe\_HH](https://discuss.elastic.co/u/ToBe_HH)\
**Post date:** [May 25, 2011, 2:07pm UTC](https://discuss.elastic.co/t/java-api-cannot-create-index-with-meta-data/4474/1 "2011-05-25T14:07:17Z")

</div>

Hi there,

For some reason I cannot modify the meta data on my index. I can index  
data, but all data is indexed as type "string". Also it is not case  
sensitive (what I want) and it is not correctly tokenized. So I wanted  
to change my metadata, but the changes are not accepted. Esp. I want  
to use special tokenizer/analyzer for special fields. The data however  
gets indexed.

Here is the output of the status information:  
{  
state: open  
settings: {  
index.number\_of\_shards: 5  
index.number\_of\_replicas: 1  
}  
mappings: {  
datastandards: {  
properties: {  
somenumber: {  
type: string  
}  
search: {  
type: string  
}  
}  
}  
}  
aliases: []  
}

I use the following code to create and fill my index:

```
        XContentBuilder data =

```

XContentFactory.jsonBuilder().startObject().startObject("properties")  
.startObject("search")  
.field("type", "string")  
.field("store", "no")  
.field("analyzer", "whitespace")  
.endObject().startObject("somenumber")  
.field("type", "integer")  
.field("store", "no")  
.endObject()  
.endObject().endObject();

```
		CreateIndexResponse response =

```

ElasticSearchPlugin.client().admin().indices().create(  
new CreateIndexRequest(INDEX\_NAME).  
mapping(CHILD\_TYPE\_NAME, data)  
).actionGet();  
Logger.debug("Response: %s", response);

ElasticSearchPlugin.client().admin().indices().putMapping(new  
PutMappingRequest(INDEX\_NAME).type(CHILD\_TYPE\_NAME).source(data)).actionGet();

```
        BulkRequestBuilder bulkRequest =

```

ElasticSearchPlugin.client().prepareBulk();

```
        int n=0;
        for(MyModel myModel : getModels()) {
            try {

```

bulkRequest.add(ElasticSearchPlugin.client().prepareIndex(INDEX\_NAME,  
CHILD\_TYPE\_NAME, String.valueOf(myModel.getId()))  
.setSource(XContentFactory.jsonBuilder()  
.startObject()  
.field("search", myModel.getHeadline() + "  
" + myModel.getAbstractText())  
.field("somenumber",  
myModel.getSomeNumber())  
.endObject()  
));  
n++;  
} catch (IOException e) {  
Logger.fatal("Cannot index #%d", myModel.getId(),  
e);  
}  
}

```
        BulkResponse bulkResponse =

```

bulkRequest.setRefresh(true).execute().actionGet();  
if (bulkResponse.hasFailures()) {  
Logger.fatal("Cannot update index - %s",  
bulkResponse.buildFailureMessage());  
} else {  
Logger.info("Index created with " + n + " entries");  
}

ElasticSearchPlugin.client().admin().indices().optimize( new  
OptimizeRequest(INDEX\_NAME)).actionGet()

Please help me. I think there is only a small glitch, but cannot find  
the cause.

Also:

- Is it ok to use the bulk request? Or should I index every entry one  
by one?
- I have the feeling, that the optimize does not do anything. Should I  
omit it?

Thank you and kind regards,

Tobias

---

<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:** [May 26, 2011, 11:01am UTC](https://discuss.elastic.co/t/java-api-cannot-create-index-with-meta-data/4474/2 "2011-05-26T11:01:44Z")

</div>

Does the index already exists with the mapping when you try and update it? You can introduce new types of the type has already been derived.

On Wednesday, May 25, 2011 at 5:07 PM, ToBe\_HH wrote:

> Hi there,
> 
> For some reason I cannot modify the meta data on my index. I can index  
> data, but all data is indexed as type "string". Also it is not case  
> sensitive (what I want) and it is not correctly tokenized. So I wanted  
> to change my metadata, but the changes are not accepted. Esp. I want  
> to use special tokenizer/analyzer for special fields. The data however  
> gets indexed.
> 
> Here is the output of the status information:  
> {  
> state: open  
> settings: {  
> index.number\_of\_shards: 5  
> index.number\_\_of\_replicas: 1  
> }  
> mappings: {  
> datastandards: {  
> properties: {  
> somenumber: {{  
> type: string  
> }}  
> search: {  
> type: string  
> }  
> }  
> }  
> }  
> aliases: [  
> }
> 
> I use the following code to create and fill my index:
> 
> XContentBuilder data =  
> XContentFactory.jsonBuilder().startObject().startObject("properties")  
> .startObject(""search")  
> .field("type", "string")  
> .field("store", "no")  
> .field("analyzer", "whitespace")  
> .endObject().startObject("somenumber")  
> .field("type", "integer")  
> .field("store", "no")  
> .endObject()  
> .endObject().endObject();
> 
> CreateIndexResponse response =  
> ElasticSearchPlugin.client().admin().indices().create(  
> new CreateIndexRequest(INDEX\_\_NAME).  
> mapping(CHILD\_\_TYPE\_NAME, data)  
> ).actionGet();  
> Logger.debug("Response: %s", response);
> 
> ElasticSearchPlugin.client().admin().indices().putMapping(new  
> PutMappingRequest(INDEX\_NAME).type(CHILD\_TYPE\_NAME).source(data)).actionGet();
> 
> BulkRequestBuilder bulkRequest =  
> ElasticSearchPlugin.client().prepareBulk();
> 
> int n=0;  
> for(MyModel myModel : getModels()) {  
> try {
> 
> bulkRequest.add(ElasticSearchPlugin.client().prepareIndex(INDEX\_NAME,  
> CHILD\_TYPE\_NAME, String.valueOf(myModel.getId()))  
> .setSource(XContentFFactory.jsonBuilder()  
> .startObject()  
> .field(""search", myModel.getHeadline() + "  
> " + myModel.getAbstractText())  
> .field(""somenumber",  
> myModel.getSomeNumber())  
> .endObject()  
> ));  
> n++;  
> } catch (IOException e) {  
> Logger.fatal("Cannot index #%d", myModel.getId(),  
> e);  
> }  
> }
> 
> BulkResponse bulkResponse D  
> bulkRequest.setRefresh(true).execute().actionGet();  
> if (bulkResponse.hasFailures()) {  
> Logger.fatal(""Cannot update index - %s",  
> bulkResponse.buildFailureMessage());  
> } else {  
> d"http:="" logger.info"=""\>Logger.info("Index created with " + n + " entries");  
> }

ElasticSearchPlugin.client().admin().indices().optimize( new  
OptimizeRequest(INDEX\_NAME)).actionGet()

Please help me. I think there is only a small glitch, but cannot find  
the cause.

Also:

- Is it ok to use the bulk request? Or should I index every entry one  
by one?
- I have the feeling, that the optimize does not do anything. Should I  
omit it?

Thank you and kind regards,

Tobias

---

<div class="post-metadata">

**Author:** ![ToBe\_HH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tobe_hh/32/3221_2.png) [@ToBe\_HH](https://discuss.elastic.co/u/ToBe_HH)\
**Post date:** [May 26, 2011, 5:37pm UTC](https://discuss.elastic.co/t/java-api-cannot-create-index-with-meta-data/4474/3 "2011-05-26T17:37:09Z")

</div>

Hi Shay,

using the code above: no - the index does not exist then.  
However, I tried moving the putMapping-Request at the end just before  
the optimize-Request and had the same result: no effect.

From what you write I would deduct that I am not terribly wrong - it  
is just not working ☹

Kind regards,

Tobias

On 26 Mai, 13:01, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Does the index already exists with the mapping when you try and update it? You can introduce new types of the type has already been derived.
> 
> On Wednesday, May 25, 2011 at 5:07 PM, ToBe\_HH wrote:
> 
> > Hi there,
> 
> > For some reason I cannot modify the meta data on my index. I can index  
> > data, but all data is indexed as type "string". Also it is not case  
> > sensitive (what I want) and it is not correctly tokenized. So I wanted  
> > to change my metadata, but the changes are not accepted. Esp. I want  
> > to use special tokenizer/analyzer for special fields. The data however  
> > gets indexed.
> 
> > Here is the output of the status information:  
> > {  
> > state: open  
> > settings: {  
> > index.number\_of\_shards: 5  
> > index.number\_\_of\_replicas: 1  
> > }  
> > mappings: {  
> > datastandards: {  
> > properties: {  
> > somenumber: {{  
> > type: string  
> > }}  
> > search: {  
> > type: string  
> > }  
> > }  
> > }  
> > }  
> > aliases: [  
> > }
> 
> > I use the following code to create and fill my index:
> 
> > XContentBuilder data =  
> > XContentFactory.jsonBuilder().startObject().startObject("properties")  
> > .startObject(""search")  
> > .field("type", "string")  
> > .field("store", "no")  
> > .field("analyzer", "whitespace")  
> > .endObject().startObject("somenumber")  
> > .field("type", "integer")  
> > .field("store", "no")  
> > .endObject()  
> > .endObject().endObject();
> 
> > CreateIndexResponse response =  
> > ElasticSearchPlugin.client().admin().indices().create(  
> > new CreateIndexRequest(INDEX\_\_NAME).  
> > mapping(CHILD\_\_TYPE\_NAME, data)  
> > ).actionGet();  
> > Logger.debug("Response: %s", response);
> 
> > ElasticSearchPlugin.client().admin().indices().putMapping(new  
> > PutMappingRequest(INDEX\_NAME).type(CHILD\_TYPE\_NAME).source(data)).actionGet ();
> 
> > BulkRequestBuilder bulkRequest =  
> > ElasticSearchPlugin.client().prepareBulk();
> 
> > int n=0;  
> > for(MyModel myModel : getModels()) {  
> > try {
> 
> > bulkRequest.add(ElasticSearchPlugin.client().prepareIndex(INDEX\_NAME,  
> > CHILD\_TYPE\_NAME, String.valueOf(myModel.getId()))  
> > .setSource(XContentFFactory.jsonBuilder()  
> > .startObject()  
> > .field(""search", myModel.getHeadline() + "  
> > " + myModel.getAbstractText())  
> > .field(""somenumber",  
> > myModel.getSomeNumber())  
> > .endObject()  
> > ));  
> > n++;  
> > } catch (IOException e) {  
> > Logger.fatal("Cannot index #%d", myModel.getId(),  
> > e);  
> > }  
> > }
> 
> > BulkResponse bulkResponse D  
> > bulkRequest.setRefresh(true).execute().actionGet();  
> > if (bulkResponse.hasFailures()) {  
> > Logger.fatal(""Cannot update index - %s",  
> > bulkResponse.buildFailureMessage());  
> > } else {
> 
> d"http:="" logger.info"=""\>Logger.info("Index created with " + n + " entries");  
> }
> 
> ElasticSearchPlugin.client().admin().indices().optimize( new  
> OptimizeRequest(INDEX\_NAME)).actionGet()
> 
> Please help me. I think there is only a small glitch, but cannot find  
> the cause.
> 
> Also:
> 
> - Is it ok to use the bulk request? Or should I index every entry one  
> by one?
> - I have the feeling, that the optimize does not do anything. Should I  
> omit it?
> 
> Thank you and kind regards,
> 
> Tobias

---

<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:05am UTC](https://discuss.elastic.co/t/java-api-cannot-create-index-with-meta-data/4474/4 "2017-07-06T04:05:14Z")

</div>


