Elasticsearch catching exceptions with the Rest High-Level Client

Viewed 1105

Using the Rest High-Level Client. After trying to create an index that already exists, we receive an 'ElasticsearchStatusException' like this:

[nscache/qgkpsKp4Sb2nluZTsxac5Q] ElasticsearchStatusException[Elasticsearch exception [type=resource_already_exists_exception, reason=index [nscache/qgkpsKp4Sb2nluZTsxac5Q] already exists]]
    at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
    at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1406)
    at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1382)
    at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1269)
    at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1231)
    at org.elasticsearch.client.IndicesClient.create(IndicesClient.java:151)
    at com.ventusproxy.proxy.cluster.elasticsearch.NonSearchableCacheIndex.<init>(NonSearchableCacheIndex.java:59)
    at com.ventusproxy.proxy.cache.data.impl.NonSearchableCache.<clinit>(NonSearchableCache.java:33)
    at com.ventusproxy.proxy.cache.data.CacheFacade.createCache(CacheFacade.java:73)
    at com.ventusproxy.proxy.cache.data.CacheFacade.initCaches(CacheFacade.java:59)

Now we need to know the type of exception, in this case a 'resource_already_exists_exception'. We are doing this:

catch (ElasticsearchException ee) {
  if (ee.getMessage().contains('resource_already_exists_exception')) {
      do_things();
  }

}

Is this the right way to get the exception type with the Rest High-Level Client? Just performing an indexOf on the message exception? Or is there another "cleaner" way to do this?

thanks.

0 Answers
Related