Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find index exists in elasticsearch 6.2.1?

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.

like image 889
Raviteja Gannoju Avatar asked Jan 20 '26 04:01

Raviteja Gannoju


1 Answers

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
}
like image 105
Val Avatar answered Jan 21 '26 18:01

Val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!