Karthikeyan
Karthikeyan

Reputation: 2001

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.

Please find the below piece of code which am using RestLowLevelClient to find my index existis.

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.

Upvotes: 0

Views: 1943

Answers (1)

Val
Val

Reputation: 217344

You can simply use the Indices Exists API:

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

Upvotes: 3

Related Questions