Elasticsearch使用Rest高级客户端捕获异常

时间:2018-09-15 15:08:23

标签: rest elasticsearch exception

使用其余的高级客户端。尝试创建已经存在的索引后,我们将收到一个“ ElasticsearchStatusException”,如下所示:

[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)

现在我们需要知道异常的类型,在这种情况下为'resource_already_exists_exception'。我们正在这样做:

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

}

这是使用Rest High-Level Client获取异常类型的正确方法吗?只是对消息异常执行indexOf吗?还是有另一种“清洁”的方式来做到这一点?

谢谢。

0 个答案:

没有答案