# Testing the index existence

**URL:** <https://discuss.elastic.co/t/testing-the-index-existence/8082>\
**Category:** Elasticsearch\
**Created:** [June 13, 2012, 1:28pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082 "2012-06-13T13:28:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tullio0106](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tullio0106](https://discuss.elastic.co/u/tullio0106)\
**Post date:** [June 13, 2012, 1:28pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/1 "2012-06-13T13:28:09Z")

</div>

I need to test if an index already exists, otherwise I create it by code.  
In order to test if an index already exists I execute :  
private boolean existsIndex() {  
ClusterState cs =  
client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();  
IndexMetaData imd = cs.getMetaData().index(INDICE);  
return imd != null;  
}  
Howerver, somtimes, this test gives a wrong result (the index exists but  
the test return false) because the index was not still recovered.  
How can avoid this ?  
Is there a way to synchronize that operation ?  
Any different solution ?  
Tks  
Tullio

---

<div class="post-metadata">

**Author:** ![AEvar\_Arnfjord\_Bjarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aevar_arnfjord_bjarm/32/2746_2.png) [@AEvar\_Arnfjord\_Bjarm](https://discuss.elastic.co/u/AEvar_Arnfjord_Bjarm)\
**Post date:** [June 13, 2012, 1:39pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/2 "2012-06-13T13:39:04Z")

</div>

Have you just tried not checking and creating the index, then throwing  
an error if you can't create it?

---

<div class="post-metadata">

**Author:** ![tullio0106](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tullio0106](https://discuss.elastic.co/u/tullio0106)\
**Post date:** [June 13, 2012, 1:44pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/3 "2012-06-13T13:44:11Z")

</div>

Yes, I know I can, however I prefer to test before than try/catch.  
Tks  
Tullio

Il giorno mercoledì 13 giugno 2012 15:39:04 UTC+2, Ævar Arnfjörð Bjarmason  
ha scritto:

> Have you just tried not checking and creating the index, then throwing  
> an error if you can't create it?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 13, 2012, 2:03pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/4 "2012-06-13T14:03:38Z")

</div>

On Wed, 2012-06-13 at 06:28 -0700, tullio0106 wrote:

> I need to test if an index already exists, otherwise I create it by  
> code.  
> In order to test if an index already exists I execute :  
> private boolean existsIndex() {  
> ClusterState cs =  
> client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();  
> IndexMetaData imd = cs.getMetaData().index(INDICE);  
> return imd != null;  
> }  
> Howerver, somtimes, this test gives a wrong result (the index exists  
> but the test return false) because the index was not still recovered.  
> How can avoid this ?

Have you tried the index-exists API?

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

> Is there a way to synchronize that operation ?  
> Any different solution ?  
> Tks  
> Tullio

---

<div class="post-metadata">

**Author:** ![tullio0106](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tullio0106](https://discuss.elastic.co/u/tullio0106)\
**Post date:** [June 13, 2012, 2:07pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/5 "2012-06-13T14:07:07Z")

</div>

Is more or less what I did with my code.  
My installation is embedded and I need to use Java API, howewer what I did  
is the same, but the problem is a timing problem.  
When I test index is not yet recovered and return false even if the index  
exists.  
Tks  
Tullio

Il giorno mercoledì 13 giugno 2012 16:03:38 UTC+2, Clinton Gormley ha  
scritto:

> On Wed, 2012-06-13 at 06:28 -0700, tullio0106 wrote:
> 
> > I need to test if an index already exists, otherwise I create it by  
> > code.  
> > In order to test if an index already exists I execute :  
> > private boolean existsIndex() {  
> > ClusterState cs =
> 
> client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();
> 
> > ```
> > IndexMetaData imd = cs.getMetaData().index(INDICE); 
> > return imd != null; 
> > } 
> > 
> > ```
> > 
> > Howerver, somtimes, this test gives a wrong result (the index exists  
> > but the test return false) because the index was not still recovered.  
> > How can avoid this ?
> 
> Have you tried the index-exists API?
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html)
> 
> > Is there a way to synchronize that operation ?  
> > Any different solution ?  
> > Tks  
> > Tullio

---

<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:** [June 13, 2012, 2:58pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/6 "2012-06-13T14:58:06Z")

</div>

Hi Tulio,

Have a look at his method :  
[https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/fr/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L567](https://github.com/dadoonet/spring-elasticsearch/blob/master/src/main/java/fr/pilato/spring/elasticsearch/ElasticsearchAbstractClientFactoryBean.java#L567)

HTH  
David

Le 13 juin 2012 à 16:07, tullio0106 [tbettinazzi@axioma.it](mailto:tbettinazzi@axioma.it) a écrit :

> Is more or less what I did with my code.  
> My installation is embedded and I need to use Java API, howewer what I did is  
> the same, but the problem is a timing problem.  
> When I test index is not yet recovered and return false even if the index  
> exists.  
> Tks  
> Tullio
> 
> Il giorno mercoledì 13 giugno 2012 16:03:38 UTC+2, Clinton Gormley ha  
> scritto:
> 
> > > On Wed, 2012-06-13 at 06:28 -0700, tullio0106 wrote:  
> > > I need to test if an index already exists, otherwise I create it by  
> > > code.  
> > > In order to test if an index already exists I execute :  
> > > private boolean existsIndex() {  
> > > ClusterState cs =  
> > > client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();  
> > > IndexMetaData imd = cs.getMetaData().index(INDICE);  
> > > return imd != null;  
> > > }  
> > > Howerver, somtimes, this test gives a wrong result (the index exists  
> > > but the test return false) because the index was not still recovered.  
> > > How can avoid this ?
> > 
> > Have you tried the index-exists API?
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html)
> > 
> > > Is there a way to synchronize that operation ?  
> > > Any different solution ?  
> > > Tks  
> > > Tullio
> > 
> > [http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html](http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html)
> > 
> > > [http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html](http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html)

--  
David Pilato  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet

---

<div class="post-metadata">

**Author:** ![AEvar\_Arnfjord\_Bjarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aevar_arnfjord_bjarm/32/2746_2.png) [@AEvar\_Arnfjord\_Bjarm](https://discuss.elastic.co/u/AEvar_Arnfjord_Bjarm)\
**Post date:** [June 13, 2012, 3:43pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/7 "2012-06-13T15:43:42Z")

</div>

On Wed, Jun 13, 2012 at 3:44 PM, tullio0106 [tbettinazzi@axioma.it](mailto:tbettinazzi@axioma.it) wrote:

> Yes, I know I can, however I prefer to test before than try/catch.

Why?

If you have an API that can create an index but throw an exception if  
one exists, why wouldn't you use it and instead opt for an API that'll  
_always_ have a race condition, Elasticsearch does _not_ guarantee any  
transactionality between API calls, so you can't call "index\_exists"  
and guarantee that it won't exists 10ms later when you try to create  
your own index.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [June 14, 2012, 9:37pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/8 "2012-06-14T21:37:22Z")

</div>

You can use the cluster health API and wait for yellow status.

On Wed, Jun 13, 2012 at 3:28 PM, tullio0106 [tbettinazzi@axioma.it](mailto:tbettinazzi@axioma.it) wrote:

> I need to test if an index already exists, otherwise I create it by code.  
> In order to test if an index already exists I execute :  
> private boolean existsIndex() {  
> ClusterState cs =  
> client.admin().cluster().prepareState().setFilterIndices(INDICE).execute().actionGet().getState();  
> IndexMetaData imd = cs.getMetaData().index(INDICE);  
> return imd != null;  
> }  
> Howerver, somtimes, this test gives a wrong result (the index exists but  
> the test return false) because the index was not still recovered.  
> How can avoid this ?  
> Is there a way to synchronize that operation ?  
> Any different solution ?  
> Tks  
> Tullio

---

<div class="post-metadata">

**Author:** ![ibai](https://avatars.discourse-cdn.com/v4/letter/i/3d9bf3/32.png) [@ibai](https://discuss.elastic.co/u/ibai)\
**Post date:** [October 14, 2014, 4:33pm UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/9 "2014-10-14T16:33:52Z")

</div>

Hi David!  
I am using your v1.3.0 of the ElasticsearchAbstractClientFactory bean and I have the same problem as explained in the beginning of the post. Since the index is not recovered by the time of trying to create the index, using a prepareExists is also returning false (does not exist) and raising an error. The solution I use is simply to catch the IndexAlreadyExistsException. I am writing this as feedback to your post in case you want to consider it for further versions of your factories.

Ibai

---

<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, 12:56am UTC](https://discuss.elastic.co/t/testing-the-index-existence/8082/10 "2017-07-06T00:56:17Z")

</div>


