# JAVA vs REST index existence check

**URL:** <https://discuss.elastic.co/t/java-vs-rest-index-existence-check/7370>\
**Category:** Elasticsearch\
**Created:** [April 17, 2012, 8:50pm UTC](https://discuss.elastic.co/t/java-vs-rest-index-existence-check/7370 "2012-04-17T20:50:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kosta/32/2915_2.png) [@Kosta](https://discuss.elastic.co/u/Kosta)\
**Post date:** [April 17, 2012, 8:50pm UTC](https://discuss.elastic.co/t/java-vs-rest-index-existence-check/7370/1 "2012-04-17T20:50:37Z")

</div>

I have been reading the group for ways in which to check the existence  
of an index, and using the JAVA API, this seems to be the preferred  
method:

client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet();  
ClusterStateResponse response =  
client.admin().cluster().prepareState().execute().actionGet();  
boolean hasIndex =  
response.getState().metaData().hasIndex(indexName);

To say that this is fairly verbose would be an understatement,  
especially when you consider it with the suggested method in the  
official reference guide:  
[http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html](http://www.elasticsearch.org/guide/reference/api/admin-indices-indices-exists.html)

This seems a lot cleaner and simpler, so I am wondering, are there any  
pitfalls involved with using this way over the JAVA API one? Thanks.

---

<div class="post-metadata">

**Author:** ![Kosta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kosta/32/2915_2.png) [@Kosta](https://discuss.elastic.co/u/Kosta)\
**Post date:** [April 17, 2012, 9:19pm UTC](https://discuss.elastic.co/t/java-vs-rest-index-existence-check/7370/2 "2012-04-17T21:19:04Z")

</div>

Sorry for spamming my own thread, but these are possibly some other  
solutions?

ActionFuture exists =  
client.admin().indices().exists(new  
IndicesExistsRequest("indexName"));  
IndicesExistsResponse actionGet = exists.actionGet();  
boolean hasIndex = actionGet.exists();

com.sun.jersey.api.client.Client jerseyClient =  
com.sun.jersey.api.client.Client.create();  
WebResource webResource = jerseyClient.resource("[http://192.168.1.100/](http://192.168.1.100/)  
indexName");  
ClientResponse head = webResource.head();  
boolean indexExists = head.getStatus() == 200;

It all seems a bit hacky though. If checking for index existence is an  
operation that has caveats, that should probably be outlined in the  
documentation. Anyway, still looking forward to understanding what the  
preferred way is, and what the possible caveats of each method are.

On Apr 17, 9:50 pm, Kosta [kosta.kra...@gmail.com](mailto:kosta.kra...@gmail.com) wrote:

> I have been reading the group for ways in which to check the existence  
> of an index, and using the JAVA API, this seems to be the preferred  
> method:
> 
> client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute() .actionGet();  
> ClusterStateResponse response =  
> client.admin().cluster().prepareState().execute().actionGet();  
> boolean hasIndex =  
> response.getState().metaData().hasIndex(indexName);
> 
> To say that this is fairly verbose would be an understatement,  
> especially when you consider it with the suggested method in the  
> official reference guide:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-indice)...
> 
> This seems a lot cleaner and simpler, so I am wondering, are there any  
> pitfalls involved with using this way over the JAVA API one? Thanks.

---

<div class="post-metadata">

**Author:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [April 17, 2012, 9:35pm UTC](https://discuss.elastic.co/t/java-vs-rest-index-existence-check/7370/3 "2012-04-17T21:35:21Z")

</div>

It is quite obvious that a java call is going to be more verbose than the REST interface. The REST interface does everything behind the scenes before sending the response. If you look in the actual source code for this indices exists handler you will find code similar to what you have found in your 2nd email:

boolean hasIndex = client.admin().indices().exists(new IndicesExistsRequest("indexName")).actionGet().exists();

--  
Matt Weber  
Sent with Sparrow ([http://www.sparrowmailapp.com/?sig](http://www.sparrowmailapp.com/?sig))

On Tuesday, April 17, 2012 at 2:19 PM, Kosta wrote:

> Sorry for spamming my own thread, but these are possibly some other  
> solutions?
> 
> ActionFuture exists =  
> client.admin().indices().exists(new  
> IndicesExistsRequest("indexName"));  
> IndicesExistsResponse actionGet = exists.actionGet();  
> boolean hasIndex = actionGet.exists();
> 
> com.sun.jersey.api.client.Client jerseyClient =  
> com.sun.jersey.api.client.Client.create();  
> WebResource webResource = jerseyClient.resource("[http://192.168.1.100/](http://192.168.1.100/)  
> indexName");  
> ClientResponse head = webResource.head();  
> boolean indexExists = head.getStatus() == 200;
> 
> It all seems a bit hacky though. If checking for index existence is an  
> operation that has caveats, that should probably be outlined in the  
> documentation. Anyway, still looking forward to understanding what the  
> preferred way is, and what the possible caveats of each method are.
> 
> On Apr 17, 9:50 pm, Kosta \<[kosta.kra...@gmail.com](mailto:kosta.kra...@gmail.com) ([http://gmail.com](http://gmail.com))\> wrote:
> 
> > I have been reading the group for ways in which to check the existence  
> > of an index, and using the JAVA API, this seems to be the preferred  
> > method:
> > 
> > client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute() .actionGet();  
> > ClusterStateResponse response =  
> > client.admin().cluster().prepareState().execute().actionGet();  
> > boolean hasIndex =  
> > response.getState().metaData().hasIndex(indexName);
> > 
> > To say that this is fairly verbose would be an understatement,  
> > especially when you consider it with the suggested method in the  
> > official reference guide:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-indice)...
> > 
> > This seems a lot cleaner and simpler, so I am wondering, are there any  
> > pitfalls involved with using this way over the JAVA API one? Thanks.

---

<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:** [April 19, 2012, 1:38pm UTC](https://discuss.elastic.co/t/java-vs-rest-index-existence-check/7370/4 "2012-04-19T13:38:58Z")

</div>

I don't really understand what the caveats you are referring to are? Here  
is a Java API to do it:

client.admin().indices().prepareExists("testindex").execute().actionGet().exists();

On Wed, Apr 18, 2012 at 12:19 AM, Kosta [kosta.krauth@gmail.com](mailto:kosta.krauth@gmail.com) wrote:

> Sorry for spamming my own thread, but these are possibly some other  
> solutions?
> 
> ActionFuture exists =  
> client.admin().indices().exists(new  
> IndicesExistsRequest("indexName"));  
> IndicesExistsResponse actionGet = exists.actionGet();  
> boolean hasIndex = actionGet.exists();
> 
> com.sun.jersey.api.client.Client jerseyClient =  
> com.sun.jersey.api.client.Client.create();  
> WebResource webResource = jerseyClient.resource("[http://192.168.1.100/](http://192.168.1.100/)  
> indexName");  
> ClientResponse head = webResource.head();  
> boolean indexExists = head.getStatus() == 200;
> 
> It all seems a bit hacky though. If checking for index existence is an  
> operation that has caveats, that should probably be outlined in the  
> documentation. Anyway, still looking forward to understanding what the  
> preferred way is, and what the possible caveats of each method are.
> 
> On Apr 17, 9:50 pm, Kosta [kosta.kra...@gmail.com](mailto:kosta.kra...@gmail.com) wrote:
> 
> > I have been reading the group for ways in which to check the existence  
> > of an index, and using the JAVA API, this seems to be the preferred  
> > method:
> 
> client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute()  
> .actionGet();
> 
> > ClusterStateResponse response =  
> > client.admin().cluster().prepareState().execute().actionGet();  
> > boolean hasIndex =  
> > response.getState().metaData().hasIndex(indexName);
> > 
> > To say that this is fairly verbose would be an understatement,  
> > especially when you consider it with the suggested method in the  
> > official reference guide:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/admin-indices-indice)...
> > 
> > This seems a lot cleaner and simpler, so I am wondering, are there any  
> > pitfalls involved with using this way over the JAVA API one? Thanks.

---

<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, 3:31am UTC](https://discuss.elastic.co/t/java-vs-rest-index-existence-check/7370/5 "2017-07-06T03:31:58Z")

</div>


