当前位置: 代码迷 >> 综合 >> RestHighLevelClient判断index是否存在
  详细解决方案

RestHighLevelClient判断index是否存在

热度:22   发布时间:2023-09-20 01:06:24.0

判断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;
    }
 }

 

  相关解决方案