# Would like to programmatically create indices with mappings in java: 6.1.3 HighLevelRestClient

**URL:** <https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144>\
**Category:** Elasticsearch\
**Created:** [February 2, 2018, 2:42am UTC](https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144 "2018-02-02T02:42:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackpark](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jackpark](https://discuss.elastic.co/u/jackpark)\
**Post date:** [February 2, 2018, 2:42am UTC](https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144/1 "2018-02-02T02:42:53Z")

</div>

In the past (ES 2+) I could assign mappings to index names programmatically. In 6.3.1, there is a CreateIndexRequest/Response pair, but the documentation online says to use it with an API which does not exist. Most ES docs talk about using CURL; that's not an option. Can someone point me to a Gist or documentation that doesn't say "this is a preliminary document"?

Similarly, I need to test if the index already exists to avoid re-creating it.

Many thanks.

---

<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:** [February 2, 2018, 7:11am UTC](https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144/2 "2018-02-02T07:11:18Z")

</div>

This is how I'm doing it:

> <https://github.com/dadoonet/fscrawler/blob/master/elasticsearch-client/src/main/java/fr/pilato/elasticsearch/crawler/fs/client/ElasticsearchClient.java#L76-L106>

I'll remove that piece of code as soon as the High Level Rest Client will offer it OOTB (6.2.0):

> <https://github.com/elastic/elasticsearch/pull/27351>

---

<div class="post-metadata">

**Author:** ![jackpark](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jackpark](https://discuss.elastic.co/u/jackpark)\
**Post date:** [February 2, 2018, 6:49pm UTC](https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144/3 "2018-02-02T18:49:39Z")

</div>

I applied that code in an Eclipse IDE and get this error message:

> The method performRequest(String, String, Map\<String,String\>, Header...) in the type RestClient is not applicable for the arguments (String, String, Map\<Object,Object\>, StringEntity)

I switched your emptyCollection to new HashMap\<String, String\>() and the error went away. Still have to test this now. Thanks!

---

<div class="post-metadata">

**Author:** ![jackpark](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jackpark](https://discuss.elastic.co/u/jackpark)\
**Post date:** [February 2, 2018, 6:58pm UTC](https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144/4 "2018-02-02T18:58:43Z")

</div>

@dadoonet I guess I need to revisit this: It appears to be the case that I can create the index; index settings were about numshards and numreplicas. What is missing for me at the moment is declaring the mappings on that index. The "putMappings" method is missing from the 6+ series. Thoughts?

---

<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:** [February 2, 2018, 7:39pm UTC](https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144/5 "2018-02-02T19:39:35Z")

</div>

Just give the mapping at the same time you are creating the index. [https://www.elastic.co/guide/en/elasticsearch/reference/6.1/indices-create-index.html#mappings](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/indices-create-index.html#mappings)

---

<div class="post-metadata">

**Author:** ![jackpark](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jackpark](https://discuss.elastic.co/u/jackpark)\
**Post date:** [February 4, 2018, 7:43pm UTC](https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144/6 "2018-02-04T19:43:11Z")

</div>

It appears that worked. Thanks!

---

<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:** [March 4, 2018, 7:43pm UTC](https://discuss.elastic.co/t/would-like-to-programmatically-create-indices-with-mappings-in-java-6-1-3-highlevelrestclient/118144/7 "2018-03-04T19:43:13Z")

</div>

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