3

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.

Sandeep Singh
  • 745
  • 4
  • 20
Raviteja Gannoju
  • 117
  • 3
  • 16

1 Answers1

6

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
}
Val
  • 207,596
  • 13
  • 358
  • 360