# What's the best way in java or groovy to determine the existence of an index given it's name

**URL:** <https://discuss.elastic.co/t/whats-the-best-way-in-java-or-groovy-to-determine-the-existence-of-an-index-given-its-name/3543>\
**Category:** Elasticsearch\
**Created:** [November 10, 2010, 1:03am UTC](https://discuss.elastic.co/t/whats-the-best-way-in-java-or-groovy-to-determine-the-existence-of-an-index-given-its-name/3543 "2010-11-10T01:03:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gideon\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Gideon\_Kaplan](https://discuss.elastic.co/u/Gideon_Kaplan)\
**Post date:** [November 10, 2010, 1:03am UTC](https://discuss.elastic.co/t/whats-the-best-way-in-java-or-groovy-to-determine-the-existence-of-an-index-given-its-name/3543/1 "2010-11-10T01:03:59Z")

</div>

Hi Folks,  
There's code in our base (I didnt write, but inherited) like this,  
seems like there has to be a better way?  
Also when instantiating a new IndexStatusRequest, does the resulting  
object contains all the known indexes?  
Is there a true javadoc on this "low level" stuff  
The only think related on the site I saw was this  
[http://www.elasticsearch.com/docs/elasticsearch/java\_api/admin/](http://www.elasticsearch.com/docs/elasticsearch/java_api/admin/)  
I've resorted to looking at method sigs (in my ide) and guessing  
Is there a best way to build the javadoc?

BTW were upgrading from .9 to .12.1  
Thanks  
Gideon

private boolean testIndexExists(indexName) {  
def statusResponse  
boolean retValue = false

```
    try {
        statusResponse = client.admin().indices().status(new

```

IndicesStatusRequest()).actionGet() // check first to see if it  
doesn't exist or get an exception if trying to create existing  
if (statusResponse.index(indexName) != null) retValue =  
true //statusRespopnse if of type IndexStatusResponse -  
public org.elasticsearch.action.admin.indices.status.IndexStatus  
index(java.lang.String index) { /\* compiled code \*/ }  
} catch(IndexMissingException ime) { // I used to be able to  
check the statusresponse now I will get an IME so treat both the same  
logger.debug "Hit an indexedmissingexception in index  
test"  
retValue = false  
}

---

<div class="post-metadata">

**Author:** ![James\_Cook](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@James\_Cook](https://discuss.elastic.co/u/James_Cook)\
**Post date:** [November 10, 2010, 1:47am UTC](https://discuss.elastic.co/t/whats-the-best-way-in-java-or-groovy-to-determine-the-existence-of-an-index-given-its-name/3543/2 "2010-11-10T01:47:04Z")

</div>

[http://elasticsearch-users.115913.n3.nabble.com/Finding-out-if-an-index-exists-td1820921.html#a1820921](http://elasticsearch-users.115913.n3.nabble.com/Finding-out-if-an-index-exists-td1820921.html#a1820921)

On Tue, Nov 9, 2010 at 8:03 PM, Gideon Kaplan [gideonkaplan@gmail.com](mailto:gideonkaplan@gmail.com)wrote:

> Hi Folks,  
> There's code in our base (I didnt write, but inherited) like this,  
> seems like there has to be a better way?  
> Also when instantiating a new IndexStatusRequest, does the resulting  
> object contains all the known indexes?  
> Is there a true javadoc on this "low level" stuff  
> The only think related on the site I saw was this  
> [http://www.elasticsearch.com/docs/elasticsearch/java\_api/admin/](http://www.elasticsearch.com/docs/elasticsearch/java_api/admin/)  
> I've resorted to looking at method sigs (in my ide) and guessing  
> Is there a best way to build the javadoc?
> 
> BTW were upgrading from .9 to .12.1  
> Thanks  
> Gideon
> 
> private boolean testIndexExists(indexName) {  
> def statusResponse  
> boolean retValue = false
> 
> ```
> try {
> statusResponse = client.admin().indices().status(new
> 
> ```
> 
> IndicesStatusRequest()).actionGet() // check first to see if it  
> doesn't exist or get an exception if trying to create existing  
> if (statusResponse.index(indexName) != null) retValue =  
> true //statusRespopnse if of type IndexStatusResponse -  
> public org.elasticsearch.action.admin.indices.status.IndexStatus  
> index(java.lang.String index) { /\* compiled code \*/ }  
> } catch(IndexMissingException ime) { // I used to be able to  
> check the statusresponse now I will get an IME so treat both the same  
> logger.debug "Hit an indexedmissingexception in index  
> test"  
> retValue = false  
> }

---

<div class="post-metadata">

**Author:** ![Gideon\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Gideon\_Kaplan](https://discuss.elastic.co/u/Gideon_Kaplan)\
**Post date:** [November 10, 2010, 6:06pm UTC](https://discuss.elastic.co/t/whats-the-best-way-in-java-or-groovy-to-determine-the-existence-of-an-index-given-its-name/3543/3 "2010-11-10T18:06:35Z")

</div>

Thanks James

On Nov 9, 5:47 pm, James Cook [jc...@tracermedia.com](mailto:jc...@tracermedia.com) wrote:

> [http://elasticsearch-users.115913.n3.nabble.com/Finding-out-if-an-ind](http://elasticsearch-users.115913.n3.nabble.com/Finding-out-if-an-ind)...
> 
> On Tue, Nov 9, 2010 at 8:03 PM, Gideon Kaplan [gideonkap...@gmail.com](mailto:gideonkap...@gmail.com)wrote:
> 
> > Hi Folks,  
> > There's code in our base (I didnt write, but inherited) like this,  
> > seems like there has to be a better way?  
> > Also when instantiating a new IndexStatusRequest, does the resulting  
> > object contains all the known indexes?  
> > Is there a true javadoc on this "low level" stuff  
> > The only think related on the site I saw was this  
> > [http://www.elasticsearch.com/docs/elasticsearch/java\_api/admin/](http://www.elasticsearch.com/docs/elasticsearch/java_api/admin/)  
> > I've resorted to looking at method sigs (in my ide) and guessing  
> > Is there a best way to build the javadoc?
> 
> > BTW were upgrading from .9 to .12.1  
> > Thanks  
> > Gideon
> 
> > private boolean testIndexExists(indexName) {  
> > def statusResponse  
> > boolean retValue = false
> 
> > ```
> > try {
> > statusResponse = client.admin().indices().status(new
> > 
> > ```
> > 
> > IndicesStatusRequest()).actionGet() // check first to see if it  
> > doesn't exist or get an exception if trying to create existing  
> > if (statusResponse.index(indexName) != null) retValue =  
> > true //statusRespopnse if of type IndexStatusResponse -  
> > public org.elasticsearch.action.admin.indices.status.IndexStatus  
> > index(java.lang.String index) { /\* compiled code \*/ }  
> > } catch(IndexMissingException ime) { // I used to be able to  
> > check the statusresponse now I will get an IME so treat both the same  
> > logger.debug "Hit an indexedmissingexception in index  
> > test"  
> > retValue = false  
> > }

---

<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:16am UTC](https://discuss.elastic.co/t/whats-the-best-way-in-java-or-groovy-to-determine-the-existence-of-an-index-given-its-name/3543/4 "2017-07-06T04:16:45Z")

</div>


