# Setting up indices (mappings, settings etc.)

**URL:** <https://discuss.elastic.co/t/setting-up-indices-mappings-settings-etc/17570>\
**Category:** Elasticsearch\
**Created:** [May 17, 2014, 8:37am UTC](https://discuss.elastic.co/t/setting-up-indices-mappings-settings-etc/17570 "2014-05-17T08:37:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Webber](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Peter\_Webber](https://discuss.elastic.co/u/Peter_Webber)\
**Post date:** [May 17, 2014, 8:37am UTC](https://discuss.elastic.co/t/setting-up-indices-mappings-settings-etc/17570/1 "2014-05-17T08:37:09Z")

</div>

Hello,

I am struggeling to set up my indices correctly. I have a mappings file and  
a settings file that I would like to apply to my indices. Through trial and  
error I figured out that the below code works for me.

However I am really not sure whether all steps that I take are necessary.  
Also it seems that are various points it is advisable to givbe ES some time  
to perform the last action (that is where I have the "Wait.forSeconds(5);"  
line, which just waits for 5 seconds before proceeding.)

Could someone please comment on the below code, or point out to me how it  
is done right?

Many thanks!  
Peter

public void setUpIndex(String indexName){  
try{  
if  
(CLIENT.admin().indices().exists(indicesExistsRequest(indexName)).actionGet().isExists()){

CLIENT.admin().indices().delete(deleteIndexRequest(indexName)).actionGet();  
Wait.forSeconds(5);  
System.out.println("Index '"+indexName+"' deleted.");  
}

CLIENT.admin().indices().create(createIndexRequest(indexName)).actionGet();  
Wait.forSeconds(5);  
System.out.println("Index '"+indexName+"' (re)created.");

```
        Path settingPath = Paths.get(SETTINGS_FILE);
        System.out.println("Loading settings from "+settingPath);
        String settingString = new 

```

String(Files.readAllBytes(settingPath), StandardCharsets.UTF\_8);

CLIENT.admin().indices().prepareClose(indexName).execute().actionGet();

CLIENT.admin().indices().prepareUpdateSettings(indexName).setSettings(settingString).execute().actionGet();  
CLIENT.admin().indices().prepareOpen(indexName).execute().get();  
System.out.println("Settings loaded from file and applied.");

```
        Path mappingPath = Paths.get(MAPPINGS_FILE);
        System.out.println("Loading mappings from "+mappingPath);
        String mappingString = new 

```

String(Files.readAllBytes(mappingPath), StandardCharsets.UTF\_8);

CLIENT.admin().indices().preparePutMapping(indexName).setType("_default_").setSource(mappingString).execute().actionGet();  
System.out.println("Post mapping set.");  
}catch(Exception e){  
System.out.println("Error! Cannot set up index  
'"+indexName+"'.");  
e.printStackTrace();  
}  
}

--  
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/87bfe975-593d-4180-a77c-21165308f039%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/87bfe975-593d-4180-a77c-21165308f039%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Peter\_Webber](https://avatars.discourse-cdn.com/v4/letter/p/c77e96/32.png) [@Peter\_Webber](https://discuss.elastic.co/u/Peter_Webber)\
**Post date:** [May 22, 2014, 1:57pm UTC](https://discuss.elastic.co/t/setting-up-indices-mappings-settings-etc/17570/2 "2014-05-22T13:57:31Z")

</div>

Hi,

if anyone could comment on my code I would be very greatful. I'd like to  
know whether my way to set up the index is as it is intended to be.

Thanks!

--  
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/9891deb3-aa0b-4efe-8838-ed8eed5fe354%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9891deb3-aa0b-4efe-8838-ed8eed5fe354%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:27am UTC](https://discuss.elastic.co/t/setting-up-indices-mappings-settings-etc/17570/3 "2017-07-06T01:27:37Z")

</div>

Closed on July 6, 2017, 1:27am UTC


