# Elasticsearch Index is exist or not

**URL:** <https://discuss.elastic.co/t/elasticsearch-index-is-exist-or-not/342224>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 4, 2023, 7:24am UTC](https://discuss.elastic.co/t/elasticsearch-index-is-exist-or-not/342224 "2023-09-04T07:24:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hld942614](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hld942614/32/124643_2.png) [@hld942614](https://discuss.elastic.co/u/hld942614)\
**Post date:** [September 4, 2023, 7:24am UTC](https://discuss.elastic.co/t/elasticsearch-index-is-exist-or-not/342224/1 "2023-09-04T07:24:31Z")

</div>

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

```auto
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);
				array.put(jsonObject);
			}
			client._transport().close();
		} catch (Exception e) {
			e.printStackTrace();
		}

```

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

```auto
client.indices().exists(ExistsRequest);

```

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

---

<div class="post-metadata">

**Author:** ![hld942614](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hld942614/32/124643_2.png) [@hld942614](https://discuss.elastic.co/u/hld942614)\
**Post date:** [September 5, 2023, 8:44am UTC](https://discuss.elastic.co/t/elasticsearch-index-is-exist-or-not/342224/2 "2023-09-05T08:44:37Z")

</div>

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

```auto
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 !");
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 5, 2023, 1:28pm UTC](https://discuss.elastic.co/t/elasticsearch-index-is-exist-or-not/342224/3 "2023-09-05T13:28:23Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 3, 2023, 1:29pm UTC](https://discuss.elastic.co/t/elasticsearch-index-is-exist-or-not/342224/4 "2023-10-03T13:29:02Z")

</div>

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