Raviteja Gannoju
Raviteja Gannoju

Reputation: 117

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.

Upvotes: 3

Views: 5144

Answers (1)

Val
Val

Reputation: 217554

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
}

Upvotes: 6

Related Questions