How to find index exists in elasticsearch 6.2.1?

烈酒焚心 提交于 2019-12-21 23:03:01

问题


I had trying to check whether a index exists in the RestHighLevelClient of elasticsearch 6.2.1

presently I am using using following code

    try {

        OpenIndexRequest openIndexRequest = new OpenIndexRequest(indexName);
        client.indices().open(openIndexRequest, header).isAcknowledged();

    } catch (ElasticsearchStatusException ex) {
        String m = "Elasticsearch exception [type=index_not_found_exception, reason=no such index]";

        if (m.equals(ex.getMessage())) {
            //TODO In case index does not exists
        }
    }

it works fine but I want to find some relevant methods like

client.indices().exists(indexname);

elastic search 6.2.1

Any help is really appreciated.


回答1:


Until this is supported by the high-level REST client (probably as of 6.3), you can achieve this by using the low-level REST client and issuing a HEAD HTTP request to your index name

Response response = restClient.performRequest("HEAD", "/" + indexname); 
int statusCode = response.getStatusLine().getStatusCode(); 
if (statusCode == 404) {
   // index does not exist
} else {
   // index exists
}


来源:https://stackoverflow.com/questions/48927002/how-to-find-index-exists-in-elasticsearch-6-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!