Elasticsearch Index is exist or not

How can I know if an index is exist or not?
I am using co.elastic.clients 8.6.2 in Java
below is my code

String index = "test"+ date;
		try {
			ElasticsearchClient client = elasticsearchConfig.getClient();
			SearchResponse<Object> searchResponse = null;
			searchResponse = client.search(s -> s.index(index)
					.sort(sort -> sort.field(f -> f.field("creationDateTime").order(SortOrder.Asc)))
					.size(DEFAULT_SEARCH_RESPONSE_SIZE), Object.class);
			for (Hit<Object> hit : searchResponse.hits().hits()) {
				Object data = hit.source();
				Map<?, ?> map = (Map<?, ?>) data;
				JSONObject jsonObject = new JSONObject(map);
		} catch (Exception e) {

Basically I want to check the index is exist or not before I start searching
I found this way might be work


But i dont know how to use ExistsRequest,can someone tell me how to use it?

I found how to know the index is exist or not !

ExistsRequest existRequest = ExistsRequest.of(e -> e.index("your_index"));
boolean indexExists = client.indices().exists(existRequest).value();
if(indexExists) {
    System.out.println("Index exist !");
}else {
	System.out.println("Index not exist !");

I think you can do this as well (no import required):

if (client.indices().exists(er -> er.index("your_index")).value()) {
    System.out.println("Index exist !");
} else {
    System.out.println("Index not exist !");

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