Check if index exist in elasticsearch 8.2


How do I check if index exist in JAVA API 8.2.

In v.7.x I used:

GetIndexRequest request = new GetIndexRequest("yourindexname");
restHighLevelclient.indices().exists(request, RequestOptions.DEFAULT);


var result = client.indices().exists(ExistsRequest.of(e -> e.index("name_index")));

RestHighLevelClient is deprecated in 8.2 so I cant use client.indices().exists.
RestHighLevelClient is changed to RestClient.

My code is Java API Client

version 8.2?


what is the client object in:

var result = client.indices().exists(ExistsRequest.of(e -> e.index("name_index")));

I just find out that the new way is:

Response response = client.performRequest(new Request("HEAD", "/" + index));
	return response.getStatusLine().getStatusCode() == 200;```

You are using low-level client.

My code:

RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(transport);
BooleanResponse result = client.indices().exists(ExistsRequest.of(e -> e.index("xpto")));
System.out.println("Exists indices: " + result.value());
1 Like

how to pass security parameter in this case ...username and password of Elasticsearch

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.