# Check if index exists

**URL:** <https://discuss.elastic.co/t/check-if-index-exists/3818>\
**Category:** Elasticsearch\
**Created:** [January 22, 2011, 2:41pm UTC](https://discuss.elastic.co/t/check-if-index-exists/3818 "2011-01-22T14:41:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [January 22, 2011, 2:41pm UTC](https://discuss.elastic.co/t/check-if-index-exists/3818/1 "2011-01-22T14:41:08Z")

</div>

Hi,

I would like to create a new index if not already there with Java API.  
The appended way does not work because the map doesn't contain the  
index even if it exists!?

Also the following index map is empty:  
map = client.admin().cluster().state(new  
ClusterStateRequest()).actionGet().getState().getMetaData().getIndices();

What am I doing wrong?? I need this for unit tests where automatic  
index creation does not work in some cases, where I am querying before  
indexing ...

Regards,  
Peter.

Map map = client.admin().cluster().health(new  
ClusterHealthRequest(indexName)).actionGet().getIndices();  
// map is always empty!!??  
boolean exists = map.containsKey(indexName))  
if(!exists) {  
client.admin().indices().create(new  
CreateIndexRequest(indexName)).actionGet();  
// waitForYellow  
client.admin().cluster().health(new  
ClusterHealthRequest(getIndexName()).waitForYellowStatus()).actionGet();  
}

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [January 22, 2011, 2:55pm UTC](https://discuss.elastic.co/t/check-if-index-exists/3818/2 "2011-01-22T14:55:43Z")

</div>

Putting the waitForYellowStatus before the ClusterHealthRequest seems  
to solve this.

Is this the correct way to do it?

On 22 Jan., 15:41, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> Hi,
> 
> I would like to create a new index if not already there with Java API.  
> The appended way does not work because the map doesn't contain the  
> index even if it exists!?
> 
> Also the following index map is empty:  
> map = client.admin().cluster().state(new  
> ClusterStateRequest()).actionGet().getState().getMetaData().getIndices();
> 
> What am I doing wrong?? I need this for unit tests where automatic  
> index creation does not work in some cases, where I am querying before  
> indexing ...
> 
> Regards,  
> Peter.
> 
> Map map = client.admin().cluster().health(new  
> ClusterHealthRequest(indexName)).actionGet().getIndices();  
> // map is always empty!!??  
> boolean exists = map.containsKey(indexName))  
> if(!exists) {  
> client.admin().indices().create(new  
> CreateIndexRequest(indexName)).actionGet();  
> // waitForYellow  
> client.admin().cluster().health(new  
> ClusterHealthRequest(getIndexName()).waitForYellowStatus()).actionGet();
> 
> }

---

<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:** [January 23, 2011, 4:43pm UTC](https://discuss.elastic.co/t/check-if-index-exists/3818/3 "2011-01-23T16:43:58Z")

</div>

Yea, you should wait for the state to be recovered from the gateway (at least the simple one where indices are recreated).

If you try and create an index, it will block until the cluster is in such a state where the metadata has been recovered, and only then try and create that index. Then, you will either get an exception (that the index already exists) or success.  
On Saturday, January 22, 2011 at 4:55 PM, Karussell wrote:

> Putting the waitForYellowStatus before the ClusterHealthRequest seems  
> to solve this.
> 
> Is this the correct way to do it?
> 
> On 22 Jan., 15:41, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:
> 
> > Hi,
> > 
> > I would like to create a new index if not already there with Java API.  
> > The appended way does not work because the map doesn't contain the  
> > index even if it exists!?
> > 
> > Also the following index map is empty:  
> > map = client.admin().cluster().state(new  
> > ClusterStateRequest()).actionGet().getState().getMetaData().getIndices();
> > 
> > What am I doing wrong?? I need this for unit tests where automatic  
> > index creation does not work in some cases, where I am querying before  
> > indexing ...
> > 
> > Regards,  
> > Peter.
> > 
> > Map map = client.admin().cluster().health(new  
> > ClusterHealthRequest(indexName)).actionGet().getIndices();  
> > // map is always empty!!??  
> > boolean exists = map.containsKey(indexName))  
> > if(!exists) {  
> > client.admin().indices().create(new  
> > CreateIndexRequest(indexName)).actionGet();  
> > // waitForYellow  
> > client.admin().cluster().health(new  
> > ClusterHealthRequest(getIndexName()).waitForYellowStatus()).actionGet();
> > 
> > }

---

<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, 4:13am UTC](https://discuss.elastic.co/t/check-if-index-exists/3818/4 "2017-07-06T04:13:22Z")

</div>


