# Find index exists in elasticsearch 5.6

**URL:** <https://discuss.elastic.co/t/find-index-exists-in-elasticsearch-5-6/133420>\
**Category:** Elasticsearch\
**Created:** [May 27, 2018, 10:51am UTC](https://discuss.elastic.co/t/find-index-exists-in-elasticsearch-5-6/133420 "2018-05-27T10:51:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aymenkn](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@aymenkn](https://discuss.elastic.co/u/aymenkn)\
**Post date:** [May 27, 2018, 10:51am UTC](https://discuss.elastic.co/t/find-index-exists-in-elasticsearch-5-6/133420/1 "2018-05-27T10:51:44Z")

</div>

i tried to check if an index exists or note before i do a search, i found a solution by performing requests, but it's not working, it results on a NullPointerException

```
   @Autowired
private RestHighLevelClient restHighLevelClient;

private List<E> convertToObjectList(SearchRequest searchRequest) {
	SearchResponse searchResponse = null;
	try {
		if (searchRequest != null) {
			RestClient restClient = null;
			Response response = restClient.performRequest("HEAD", "/" + index()); //java.lang.NullPointerException: null
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != 404) {
				searchResponse = restHighLevelClient.search(searchRequest);
			}
		}
	} catch (IOException e) {
		logger.error("Error" + e);
	}
	....
}

@Configuration
public class ElasticsearchConfiguration extends AbstractFactoryBean<RestHighLevelClient> {

	private RestHighLevelClient restHighLevelClient;

	@Override
	public RestHighLevelClient createInstance() {
		return buildClient();
	}

	@Override
	public Class<RestHighLevelClient> getObjectType() {
		return RestHighLevelClient.class;
	}

	@Override
	public void destroy() {
		try {
			if (restClient() != null) {
				restClient().close();
			}
		} catch (final Exception e) {
			logger.error("Error closing ElasticSearch client: ", e);
		}
	}

	@Override
	public boolean isSingleton() {
		return false;
	}

	public RestClient restClient() {
		return RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
	}

	private RestHighLevelClient buildClient() {
		try {
			restHighLevelClient = new RestHighLevelClient(restClient());
		} catch (Exception e) {
			logger.error("Error" + e);
		}
		return restHighLevelClient;
	}

}
```

---

<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:** [May 29, 2018, 7:04am UTC](https://discuss.elastic.co/t/find-index-exists-in-elasticsearch-5-6/133420/2 "2018-05-29T07:04:44Z")

</div>

Well...

```auto
RestClient restClient = null;
Response response = restClient.performRequest("HEAD", "/" + index()); 

```

Don't you see anything obvious?

---

<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:** [June 26, 2018, 7:04am UTC](https://discuss.elastic.co/t/find-index-exists-in-elasticsearch-5-6/133420/3 "2018-06-26T07:04:53Z")

</div>

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