JAVA vs REST index existence check


(Kosta) #1

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

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.


(Kosta) #2

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/
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 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: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.


(Matt Weber) #3

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)

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/
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 (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: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.


(Shay Banon) #4

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 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/
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 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:
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.


(system) #5