# Configure an index using a low level rest Client

**URL:** <https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389>\
**Category:** Elasticsearch\
**Created:** [October 18, 2017, 12:26pm UTC](https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389 "2017-10-18T12:26:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gringogordo](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@gringogordo](https://discuss.elastic.co/u/gringogordo)\
**Post date:** [October 18, 2017, 12:26pm UTC](https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389/1 "2017-10-18T12:26:24Z")

</div>

I am (re)writing an index creation app in java and struggling a little bit.

The existing app uses the TransportClient and I want to move to the High Level Rest Client (using the low level Rest Client to set it up). I want to create a new index and to do this I am following the [migration documentation](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/_changing_the_application_8217_s_code.html).

Starting with the sample code along these lines

```
			Settings indexSettings = Settings.builder() 
			        .put("index.number_of_shards", 1)
			        .put("index.number_of_replicas", 0)
			       .put("path.data", "NewDevCluster") //THIS SETTING FAILS!
		               .build();

			String payload = XContentFactory.jsonBuilder() 
			        .startObject()
			            .startObject("settings") 
			                .value(indexSettings)
			            .endObject()
			            .startObject("mappings")  
			                .startObject("doc")
			                    .startObject("properties")
			                        .startObject("time")
			                            .field("type", "date")
			                        .endObject()
			                    .endObject()
			                .endObject()
			            .endObject()
			        .endObject().string();

			
			HttpEntity entityCreate = new NStringEntity(payload, ContentType.APPLICATION_JSON); 
							//CREATE INDEX
			Response response = restClient.performRequest("PUT", "newIndex", emptyMap(), entityCreate); 

```

So...

This does the basics that I want (although I haven't figured out how to point it to a new cluster yet - in the long term I'm not sure whether I want to do this anyway maybe a new index or an alias will suffice ...)

However I have a general settings file (along these lines)

> {  
> "index" : {  
> "analysis" : {  
> "char\_filter" : {  
> "gref\_mapping" : {  
> "type" : "mapping",  
> "mappings" : ["/=\>E"]  
> }  
> },  
> "analyzer" : {  
> "gref\_char\_filter" : {  
> "tokenizer" : "standard",  
> "char\_filter" : ["gref\_mapping"]  
> }  
> }  
> }  
> }  
> }

which are being loaded into the transport client like

```
> String settingsSource=getSourceFromMappingFile("settings.json");

```

> ```
> CreateIndexResponse resp = client.admin().indices().create(new CreateIndexRequest(INDEX_NAME).settings(settingsSource)).actionGet();
> 
> ```

and a set of large mapping files which are loaded like this

> ```
> client.admin().
> indices().
> preparePutMapping(indexName).
> setSource(getSourceFromMappingFile(mappingFile)).
> setType(indexType).
> execute().
> actionGet();
> 
> ```

I'm struggling to move towards loading these using the restclients. Tbh I haven't tried very hard with the mapping files yet, and I can see how I could load them in a very long winded way but presumably there is a better way than creating an enourmous mapping section by loading 30 - 40 json documents into a XContentFactory.jsonBuilder ?) and I'm assuming there must be a way to load from file ?

Anyway what I'm asking is

1. Is it definitely possible to load the settings and mapping files ?
2. Can anyone point me in the right direction as to how I should do this

---

<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:** [October 18, 2017, 1:06pm UTC](https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389/2 "2017-10-18T13:06:00Z")

</div>

Here is what I'm doing to create an index with settings (that I read previously from a file):

> <https://github.com/dadoonet/elasticsearch-beyonder/blob/master/src/main/java/fr/pilato/elasticsearch/tools/index/IndexElasticsearchUpdater.java#L278-L299>

And same for add a mapping:

> <https://github.com/dadoonet/elasticsearch-beyonder/blob/master/src/main/java/fr/pilato/elasticsearch/tools/type/TypeElasticsearchUpdater.java#L228-L249>

Though I think the later is useless as it's probably better to provide the mapping in the index creation call as at some point you will have only one type per index.

---

<div class="post-metadata">

**Author:** ![gringogordo](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@gringogordo](https://discuss.elastic.co/u/gringogordo)\
**Post date:** [October 18, 2017, 1:17pm UTC](https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389/3 "2017-10-18T13:17:29Z")

</div>

That's great thanks I've been struggling with that for a day or more!

This is really helpful so no worries if you are too busy to answer but I'm interested by " at some point you will have only one type per index". In my current design I have 6 types in the index, is this a flawed design ?

( I need to revisit why I also have 20 odd mappings which are just sub-sets of one of the Types -I'm sure this is an inherited flaw which has just never been looked at since).

---

<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:** [October 18, 2017, 2:33pm UTC](https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389/4 "2017-10-18T14:33:48Z")

</div>

> In my current design I have 6 types in the index, is this a flawed design ?

Yes. From 6.0 you won't be able to create multiple type indices.

You can read that blog: [Indices, types, and parent / child: current status and upcoming changes in Elasticsearch | Elastic Blog](https://www.elastic.co/blog/index-type-parent-child-join-now-future-in-elasticsearch)

---

<div class="post-metadata">

**Author:** ![gringogordo](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@gringogordo](https://discuss.elastic.co/u/gringogordo)\
**Post date:** [October 18, 2017, 2:37pm UTC](https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389/5 "2017-10-18T14:37:26Z")

</div>

Wow, that passed me by completely (tbh I only started looking at upgrading elasticSearch as an afterthought so I didn't do my research!). Many thanks, my side project has just increased in complexity by several magnitudes!

---

<div class="post-metadata">

**Author:** ![gringogordo](https://avatars.discourse-cdn.com/v4/letter/g/a87d85/32.png) [@gringogordo](https://discuss.elastic.co/u/gringogordo)\
**Post date:** [October 19, 2017, 12:26pm UTC](https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389/6 "2017-10-19T12:26:05Z")

</div>

For future reference if anyone finds it useful I bumped into this just now which adds a lot of flesh to the core documentation

[http://elasticsearch-cheatsheet.jolicode.com/](http://elasticsearch-cheatsheet.jolicode.com/)

---

<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:** [November 16, 2017, 12:26pm UTC](https://discuss.elastic.co/t/configure-an-index-using-a-low-level-rest-client/104389/7 "2017-11-16T12:26:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
