# Create-or-replace index programmatically

**URL:** <https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344>\
**Category:** Elasticsearch\
**Created:** [May 26, 2020, 1:15pm UTC](https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344 "2020-05-26T13:15:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmu](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@mmu](https://discuss.elastic.co/u/mmu)\
**Post date:** [May 26, 2020, 1:15pm UTC](https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344/1 "2020-05-26T13:15:33Z")

</div>

Hi,

I have a JAVA web service that uses an ES index for caching. Sometimes the field/index definitions change so the ES index must be re-created.

What I am looking for is a solution pattern (or API function) that:

1. Creates index and mapping if it is missing
2. Deletes and re-creates index and mapping if the index definition is not in sync with an expected definition

---

<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 26, 2020, 4:26pm UTC](https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344/2 "2020-05-26T16:26:48Z")

</div>

> Creates index and mapping if it is missing

I wrote a project which does something like this:

> **[GitHub - dadoonet/elasticsearch-beyonder: Elasticsearch Beyonder project...](https://github.com/dadoonet/elasticsearch-beyonder)**
>
> Elasticsearch Beyonder project gives some features beyond elasticsearch itself. - GitHub - dadoonet/elasticsearch-beyonder: Elasticsearch Beyonder project gives some features beyond elasticsearch ...

You can see it in [this demo project](https://github.com/dadoonet/legacy-search/tree/03-mapping). [Index settings](https://github.com/dadoonet/legacy-search/blob/03-mapping/src/main/resources/elasticsearch/person/_settings.json).

> Deletes and re-creates index and mapping if the index definition is not in sync with an expected definition

That's super dangerous because it will delete all the existing data.  
But there is an option in Beyonder here:

> <https://github.com/dadoonet/elasticsearch-beyonder/blob/main/src/main/java/fr/pilato/elasticsearch/tools/ElasticsearchBeyonder.java#L103>

Using `force` will remove any existing index if exists. Not that it will not compare the existing schema with the one that you want to apply. That could be a good option to add to this project though. But it is still super dangerous to me and I'd only use that for integration testing not for production code.

Note that the `merge` option can try to update an existing mapping but it will work only in a few circonstances (like adding a new field).

---

<div class="post-metadata">

**Author:** ![mmu](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@mmu](https://discuss.elastic.co/u/mmu)\
**Post date:** [May 26, 2020, 9:40pm UTC](https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344/3 "2020-05-26T21:40:13Z")

</div>

Thanks for the hint! - But I am really looking for something robust that detects index definition changes and performs a complete re-creation if definitions have changed. Data loss is not an issue - ES is not the primary data store and can be rebuilt within minutes. It is just not convenient to force that kind of re-creation on every service restart.

---

<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 27, 2020, 3:53am UTC](https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344/4 "2020-05-27T03:53:29Z")

</div>

You need to write yourself this logic then.  
I'm not sure if spring data elasticsearch or hibernate search support that but this is something you might want to check.

If you do your own implementation, I'd be happy to welcome it as a PR in the beyonder project. 😉

---

<div class="post-metadata">

**Author:** ![mmu](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@mmu](https://discuss.elastic.co/u/mmu)\
**Post date:** [May 29, 2020, 7:13am UTC](https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344/5 "2020-05-29T07:13:41Z")

</div>

I see three options how information about the last "valid" index definition could be persisted:

1. Hash the original index definition and store hash it in some metadata field in ES (I am not sure there is an appropriate field to do this - is there?)
2. Hash the original index definition and store hash it in a persistent location (usually File System)
3. Keep the original index definition and store it in a persistent location (also FS)

On startup, the current \_settings.json could be checked against the persisted hash/definition and force re-creation if they don't match.

My preference would be (1) but I could not yet find an appropriate customizable metadata field at the index level. Options (2,3) require configuration of some persistent file location, which would work for me but is probably less desirable for this kind of library.

---

<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 29, 2020, 8:01am UTC](https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344/6 "2020-05-29T08:01:25Z")

</div>

Good idea. I'd use [https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-meta-field.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-meta-field.html)

---

<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:** [June 26, 2020, 8:01am UTC](https://discuss.elastic.co/t/create-or-replace-index-programmatically/234344/7 "2020-06-26T08:01:29Z")

</div>

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