# Need mapping help with unit test in java api

**URL:** <https://discuss.elastic.co/t/need-mapping-help-with-unit-test-in-java-api/7851>\
**Category:** Elasticsearch\
**Created:** [May 24, 2012, 9:36pm UTC](https://discuss.elastic.co/t/need-mapping-help-with-unit-test-in-java-api/7851 "2012-05-24T21:36:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Craig\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Craig\_Brown](https://discuss.elastic.co/u/Craig_Brown)\
**Post date:** [May 24, 2012, 9:36pm UTC](https://discuss.elastic.co/t/need-mapping-help-with-unit-test-in-java-api/7851/1 "2012-05-24T21:36:29Z")

</div>

I have a json mapping file that I user for unit testing that is based  
on the one I use with CURL. It looks like the java api only accepts  
the "mapping: {}" section that starts with the type like this:

```
    client.admin()
            .indices()
            .create(createIndexRequest("myIndex")
                .mapping("records", "records:{}")
            )
            .actionGet();

```

I want to add an edgeNGram tokenizer to a field and it looks like it  
needs to be specified as follows:  
{  
"settings" : {  
"analysis": {  
"analyzer": {  
"containsText" : {  
"tokenizer": "whitespace",  
"filter": ["lowercase", "autocomplete"]  
}  
},  
"filter": {  
"autocomplete": {"type": "edgeNGram", "min\_gram": "1",  
"max\_gram": "50", "side": "front"}  
}  
}  
},  
"mappings" : {  
"records" : {  
"properties" : {  
"name":{"type":"string", "analyzer":"containsText"}  
}  
}  
}  
}

This seems to work from the REST api, but the java mapping api gives  
the following exception.  
org.elasticsearch.index.mapper.MapperParsingException: mapping  
[records]  
at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService  
$1.execute(MetaDataCreateIndexService.java:244)  
at org.elasticsearch.cluster.service.InternalClusterService  
$2.run(InternalClusterService.java:211)  
at java.util.concurrent.ThreadPoolExecutor  
$Worker.runTask(ThreadPoolExecutor.java:886)  
at java.util.concurrent.ThreadPoolExecutor  
$Worker.run(ThreadPoolExecutor.java:908)  
at java.lang.Thread.run(Thread.java:662)  
Caused by: org.elasticsearch.index.mapper.MapperParsingException:  
Mapping must have the `type` as the root object  
at  
org.elasticsearch.index.mapper.DocumentMapperParser.extractMapping(DocumentMapperParser.java:  
231)  
at  
org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:  
141)  
at  
org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:  
269)  
at  
org.elasticsearch.index.mapper.MapperService.add(MapperService.java:  
172)  
at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService  
$1.execute(MetaDataCreateIndexService.java:241)

How can I set this mapping up using the java api so that I can perform  
some unit testing with ngrams to make sure they are working  
correctly?

Thanks!

- Craig

---

<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:** [May 24, 2012, 9:47pm UTC](https://discuss.elastic.co/t/need-mapping-help-with-unit-test-in-java-api/7851/2 "2012-05-24T21:47:19Z")

</div>

Hi Craig,

Not sure it will fit to your needs but I wrote something here that create an  
index using settings.  
[https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f)  
r/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L6  
69

Also, I define new mappings as follow :  
[https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f)  
r/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L6  
09

```
			PutMappingResponse response =

```

client.admin().indices()  
.preparePutMapping(index)  
.setType(type)  
.setSource(source)  
.execute().actionGet();

Source is a String containing:  
{  
"tweet" : {  
"properties" : {  
"message" : {"type" : "string", "store" : "yes"}  
}  
}  
}  
(see  
[https://github.com/dadoonet/spring-elasticsearch/blob/master/src/test/resour](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/test/resour)  
ces/es1/twitter/tweet.json )

Hope this could help you to solve your issue.  
David.

> -----Message d'origine-----  
> De : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
> [[mailto:elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)] De la part de Craig Brown  
> Envoyé : jeudi 24 mai 2012 23:36  
> À : elasticsearch  
> Objet : Need mapping help with unit test in java api.
> 
> I have a json mapping file that I user for unit testing that is based  
> on the one I use with CURL. It looks like the java api only accepts the  
> "mapping: {}" section that starts with the type like this:
> 
> ```
> client.admin()
> .indices()
> .create(createIndexRequest("myIndex")
> .mapping("records", "records:{}")
> )
> .actionGet();
> 
> ```
> 
> I want to add an edgeNGram tokenizer to a field and it looks like it  
> needs to be specified as follows:  
> {  
> "settings" : {  
> "analysis": {  
> "analyzer": {  
> "containsText" : {  
> "tokenizer": "whitespace",  
> "filter": ["lowercase", "autocomplete"]  
> }  
> },  
> "filter": {  
> "autocomplete": {"type": "edgeNGram", "min\_gram": "1",  
> "max\_gram": "50", "side": "front"}  
> }  
> }  
> },  
> "mappings" : {  
> "records" : {  
> "properties" : {  
> "name":{"type":"string", "analyzer":"containsText"}  
> }  
> }  
> }  
> }
> 
> This seems to work from the REST api, but the java mapping api gives  
> the following exception.  
> org.elasticsearch.index.mapper.MapperParsingException: mapping  
> [records]  
> at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService  
> $1.execute(MetaDataCreateIndexService.java:244)  
> at org.elasticsearch.cluster.service.InternalClusterService  
> $2.run(InternalClusterService.java:211)  
> at java.util.concurrent.ThreadPoolExecutor  
> $Worker.runTask(ThreadPoolExecutor.java:886)  
> at java.util.concurrent.ThreadPoolExecutor  
> $Worker.run(ThreadPoolExecutor.java:908)  
> at java.lang.Thread.run(Thread.java:662)  
> Caused by: org.elasticsearch.index.mapper.MapperParsingException:  
> Mapping must have the `type` as the root object  
> at  
> org.elasticsearch.index.mapper.DocumentMapperParser.extractMapping(Docu  
> mentMapperParser.java:  
> 231)  
> at  
> org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMappe  
> rParser.java:  
> 141)  
> at  
> org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:  
> 269)  
> at  
> org.elasticsearch.index.mapper.MapperService.add(MapperService.java:  
> 172)  
> at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService  
> $1.execute(MetaDataCreateIndexService.java:241)
> 
> How can I set this mapping up using the java api so that I can perform  
> some unit testing with ngrams to make sure they are working correctly?
> 
> Thanks!
> 
> - Craig

---

<div class="post-metadata">

**Author:** ![Craig\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Craig\_Brown](https://discuss.elastic.co/u/Craig_Brown)\
**Post date:** [May 24, 2012, 10:11pm UTC](https://discuss.elastic.co/t/need-mapping-help-with-unit-test-in-java-api/7851/3 "2012-05-24T22:11:23Z")

</div>

Interesting. You're using a different api than I am to create the  
index mapping. I'll need to try that. Thanks for the code resource,  
very neat and understandable.

- Craig

On Thu, May 24, 2012 at 3:47 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> Hi Craig,
> 
> Not sure it will fit to your needs but I wrote something here that create an  
> index using settings.  
> [https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f)  
> r/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L6  
> 69
> 
> Also, I define new mappings as follow :  
> [https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f)  
> r/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L6  
> 09
> 
> ```
> PutMappingResponse response =
> 
> ```
> 
> client.admin().indices()  
> .preparePutMapping(index)  
> .setType(type)  
> .setSource(source)  
> .execute().actionGet();
> 
> Source is a String containing:  
> {  
> "tweet" : {  
> "properties" : {  
> "message" : {"type" : "string", "store" : "yes"}  
> }  
> }  
> }  
> (see  
> [https://github.com/dadoonet/spring-elasticsearch/blob/master/src/test/resour](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/test/resour)  
> ces/es1/twitter/tweet.json )
> 
> Hope this could help you to solve your issue.  
> David.
> 
> > -----Message d'origine-----  
> > De : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
> > [[mailto:elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)] De la part de Craig Brown  
> > Envoyé : jeudi 24 mai 2012 23:36  
> > À : elasticsearch  
> > Objet : Need mapping help with unit test in java api.
> > 
> > I have a json mapping file that I user for unit testing that is based  
> > on the one I use with CURL. It looks like the java api only accepts the  
> > "mapping: {}" section that starts with the type like this:
> > 
> > ```
> > client.admin()
> > .indices()
> > .create(createIndexRequest("myIndex")
> > .mapping("records", "records:{}")
> > )
> > .actionGet();
> > 
> > ```
> > 
> > I want to add an edgeNGram tokenizer to a field and it looks like it  
> > needs to be specified as follows:  
> > {  
> > "settings" : {  
> > "analysis": {  
> > "analyzer": {  
> > "containsText" : {  
> > "tokenizer": "whitespace",  
> > "filter": ["lowercase", "autocomplete"]  
> > }  
> > },  
> > "filter": {  
> > "autocomplete": {"type": "edgeNGram", "min\_gram": "1",  
> > "max\_gram": "50", "side": "front"}  
> > }  
> > }  
> > },  
> > "mappings" : {  
> > "records" : {  
> > "properties" : {  
> > "name":{"type":"string", "analyzer":"containsText"}  
> > }  
> > }  
> > }  
> > }
> > 
> > This seems to work from the REST api, but the java mapping api gives  
> > the following exception.  
> > org.elasticsearch.index.mapper.MapperParsingException: mapping  
> > [records]  
> > at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService  
> > $1.execute(MetaDataCreateIndexService.java:244)  
> > at org.elasticsearch.cluster.service.InternalClusterService  
> > $2.run(InternalClusterService.java:211)  
> > at java.util.concurrent.ThreadPoolExecutor  
> > $Worker.runTask(ThreadPoolExecutor.java:886)  
> > at java.util.concurrent.ThreadPoolExecutor  
> > $Worker.run(ThreadPoolExecutor.java:908)  
> > at java.lang.Thread.run(Thread.java:662)  
> > Caused by: org.elasticsearch.index.mapper.MapperParsingException:  
> > Mapping must have the `type` as the root object  
> > at  
> > org.elasticsearch.index.mapper.DocumentMapperParser.extractMapping(Docu  
> > mentMapperParser.java:  
> > 231)  
> > at  
> > org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMappe  
> > rParser.java:  
> > 141)  
> > at  
> > org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:  
> > 269)  
> > at  
> > org.elasticsearch.index.mapper.MapperService.add(MapperService.java:  
> > 172)  
> > at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService  
> > $1.execute(MetaDataCreateIndexService.java:241)
> > 
> > How can I set this mapping up using the java api so that I can perform  
> > some unit testing with ngrams to make sure they are working correctly?
> > 
> > Thanks!
> > 
> > - Craig

--  
…  
CRAIG BROWN  
chief architect  
youwho, Inc.

[www.youwho.com](http://www.youwho.com)

T: 801.855. 0921  
M: 801.913. 0939

---

<div class="post-metadata">

**Author:** ![Craig\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/ce7236/32.png) [@Craig\_Brown](https://discuss.elastic.co/u/Craig_Brown)\
**Post date:** [May 25, 2012, 8:50pm UTC](https://discuss.elastic.co/t/need-mapping-help-with-unit-test-in-java-api/7851/4 "2012-05-25T20:50:46Z")

</div>

Ok, figured it out. I tried this one, but must have had the parameters  
wrong. So it looks like there is a section for settings and another  
for mappings. The REST call must break those apart automatically.

```
        client.admin()
                .indices()
                .create(createIndexRequest(index)
                    .settings(settings)
                    .mapping(type, mapping)
                )
                .actionGet();

```

- Craig

On Thu, May 24, 2012 at 4:11 PM, Craig Brown [cbrown@youwho.com](mailto:cbrown@youwho.com) wrote:

> Interesting. You're using a different api than I am to create the  
> index mapping. I'll need to try that. Thanks for the code resource,  
> very neat and understandable.
> 
> - Craig
> 
> On Thu, May 24, 2012 at 3:47 PM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:
> 
> > Hi Craig,
> > 
> > Not sure it will fit to your needs but I wrote something here that create an  
> > index using settings.  
> > [https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f)  
> > r/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L6  
> > 69
> > 
> > Also, I define new mappings as follow :  
> > [https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/f)  
> > r/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L6  
> > 09
> > 
> > ```
> > PutMappingResponse response =
> > 
> > ```
> > 
> > client.admin().indices()  
> > .preparePutMapping(index)  
> > .setType(type)  
> > .setSource(source)  
> > .execute().actionGet();
> > 
> > Source is a String containing:  
> > {  
> > "tweet" : {  
> > "properties" : {  
> > "message" : {"type" : "string", "store" : "yes"}  
> > }  
> > }  
> > }  
> > (see  
> > [https://github.com/dadoonet/spring-elasticsearch/blob/master/src/test/resour](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/test/resour)  
> > ces/es1/twitter/tweet.json )
> > 
> > Hope this could help you to solve your issue.  
> > David.
> > 
> > > -----Message d'origine-----  
> > > De : [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)  
> > > [[mailto:elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com)] De la part de Craig Brown  
> > > Envoyé : jeudi 24 mai 2012 23:36  
> > > À : elasticsearch  
> > > Objet : Need mapping help with unit test in java api.
> > > 
> > > I have a json mapping file that I user for unit testing that is based  
> > > on the one I use with CURL. It looks like the java api only accepts the  
> > > "mapping: {}" section that starts with the type like this:
> > > 
> > > ```
> > > client.admin()
> > > .indices()
> > > .create(createIndexRequest("myIndex")
> > > .mapping("records", "records:{}")
> > > )
> > > .actionGet();
> > > 
> > > ```
> > > 
> > > I want to add an edgeNGram tokenizer to a field and it looks like it  
> > > needs to be specified as follows:  
> > > {  
> > > "settings" : {  
> > > "analysis": {  
> > > "analyzer": {  
> > > "containsText" : {  
> > > "tokenizer": "whitespace",  
> > > "filter": ["lowercase", "autocomplete"]  
> > > }  
> > > },  
> > > "filter": {  
> > > "autocomplete": {"type": "edgeNGram", "min\_gram": "1",  
> > > "max\_gram": "50", "side": "front"}  
> > > }  
> > > }  
> > > },  
> > > "mappings" : {  
> > > "records" : {  
> > > "properties" : {  
> > > "name":{"type":"string", "analyzer":"containsText"}  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > This seems to work from the REST api, but the java mapping api gives  
> > > the following exception.  
> > > org.elasticsearch.index.mapper.MapperParsingException: mapping  
> > > [records]  
> > > at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService  
> > > $1.execute(MetaDataCreateIndexService.java:244)  
> > > at org.elasticsearch.cluster.service.InternalClusterService  
> > > $2.run(InternalClusterService.java:211)  
> > > at java.util.concurrent.ThreadPoolExecutor  
> > > $Worker.runTask(ThreadPoolExecutor.java:886)  
> > > at java.util.concurrent.ThreadPoolExecutor  
> > > $Worker.run(ThreadPoolExecutor.java:908)  
> > > at java.lang.Thread.run(Thread.java:662)  
> > > Caused by: org.elasticsearch.index.mapper.MapperParsingException:  
> > > Mapping must have the `type` as the root object  
> > > at  
> > > org.elasticsearch.index.mapper.DocumentMapperParser.extractMapping(Docu  
> > > mentMapperParser.java:  
> > > 231)  
> > > at  
> > > org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMappe  
> > > rParser.java:  
> > > 141)  
> > > at  
> > > org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:  
> > > 269)  
> > > at  
> > > org.elasticsearch.index.mapper.MapperService.add(MapperService.java:  
> > > 172)  
> > > at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService  
> > > $1.execute(MetaDataCreateIndexService.java:241)
> > > 
> > > How can I set this mapping up using the java api so that I can perform  
> > > some unit testing with ngrams to make sure they are working correctly?
> > > 
> > > Thanks!
> > > 
> > > - Craig
> 
> --  
> …  
> CRAIG BROWN  
> chief architect  
> youwho, Inc.
> 
> [www.youwho.com](http://www.youwho.com)
> 
> T: 801.855. 0921  
> M: 801.913. 0939

--  
…  
CRAIG BROWN  
chief architect  
youwho, Inc.

[www.youwho.com](http://www.youwho.com)

T: 801.855. 0921  
M: 801.913. 0939

---

<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/need-mapping-help-with-unit-test-in-java-api/7851/5 "2017-07-06T03:26:59Z")

</div>


