判断Index是否存在版本以6.4.2为准,6.2.1api不一样
public boolean indexExists(String indexName) { GetIndexRequest request = new GetIndexRequest(); request.indices(indexName); try { return rhlClient.indices().exists(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); return false; } }
6.2.1api使用以下方法,通过异常捕获判断es_not_found_exception
public boolean isExistIndex(String indexName){ OpenIndexRequest openIndexRequest = new OpenIndexRequest(indexName); /*IndicesExistsRequest request = new IndicesExistsRequest(indexName); GetIndexRequest request = new GetIndexRequest(); org.elasticsearch.client.Client Client=client; IndicesExistsResponse response = Client.admin().indices().exists(request).actionGet(); if (response.isExists()) { return true; } return false;*/ try { client.indices().open(openIndexRequest); return true; }catch (ElasticsearchException e) { return false; }catch (Exception e) { return false; } }