简体   繁体   English

弹性搜索以找到索引存在或不使用Java API

[英]Elastic search to find index existis or not using Java api

Before am indexing the documents i want to check whether my index name is already existis in the ElasticSearcch or not. 在对文档建立索引之前,我想检查一下我的索引名称是否已经存在于ElasticSearcch中。

Please find the below piece of code which am using RestLowLevelClient to find my index existis. 请找到下面的代码,这些代码正在使用RestLowLevelClient查找我的索引存在。

public boolean checkIfIndexExists(String indexName) throws IOException {
        Response response = client.getLowLevelClient().performRequest("HEAD", "/" + indexName);
        int statusCode = response.getStatusLine().getStatusCode(); 
        return (statusCode != 404);
    }

But i want to using RestHighLevelClient and how i can modify the same code. 但是我想使用RestHighLevelClient以及如何修改相同的代码。

You can simply use the Indices Exists API : 您可以简单地使用Indices Exists API

public boolean checkIfIndexExists(String indexName) throws IOException {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName); 
    return client.indices().exists(request, RequestOptions.DEFAULT);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM