# Create index at startup

**URL:** <https://discuss.elastic.co/t/create-index-at-startup/152570>\
**Category:** Elasticsearch\
**Created:** [October 15, 2018, 11:31pm UTC](https://discuss.elastic.co/t/create-index-at-startup/152570 "2018-10-15T23:31:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiboxiv](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jiboxiv](https://discuss.elastic.co/u/jiboxiv)\
**Post date:** [October 15, 2018, 11:31pm UTC](https://discuss.elastic.co/t/create-index-at-startup/152570/1 "2018-10-15T23:31:40Z")

</div>

Hi,

I wish to create an Elasticsearch plugin, this plugin requires some information to be stored. Is there any way to create an index & its mappings at startup time?

I found this: [Index Creation on Startup?](https://discuss.elastic.co/t/index-creation-on-startup/3837)  
But it does not answer the question.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 16, 2018, 7:00am UTC](https://discuss.elastic.co/t/create-index-at-startup/152570/2 "2018-10-16T07:00:27Z")

</div>

you could use a cluster state listener and wait until the cluster is ready and then check for the existence of that index.

Note that a one time check is not enough, as you may accidentally delete that index - thus the cluster state listener makes sense.

---

<div class="post-metadata">

**Author:** ![jiboxiv](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jiboxiv](https://discuss.elastic.co/u/jiboxiv)\
**Post date:** [October 16, 2018, 3:53pm UTC](https://discuss.elastic.co/t/create-index-at-startup/152570/3 "2018-10-16T15:53:11Z")

</div>

Thank you Spinscale,

By state listener I assume you mean either the afterStart() or clusterChanged()?

The issue with this is that the requests are blocking requests. Do you recommend running these on a generic thread or as async requests?

With async requests it would require two one to check its existence and once to actually create the index.

Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 17, 2018, 7:04am UTC](https://discuss.elastic.co/t/create-index-at-startup/152570/4 "2018-10-17T07:04:19Z")

</div>

the `clusterChanged` event may not be blocked, as it is called by an update thread for all registered listeners sequentially - thus you need to make sure in your own code that you a) run async, b) do not trigger a new async check for each cluster state update.

---

<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 14, 2018, 7:04am UTC](https://discuss.elastic.co/t/create-index-at-startup/152570/5 "2018-11-14T07:04:19Z")

</div>

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