Checking if an index or type exists
A common pitfall error is to query for indices and types that don't exist. To prevent this issue, ElasticSearch gives the user the ability to check the index and type existence.
This check is often used during an application startup to create indices and types that are required for it to work correctly.
Getting ready
You will need a working ElasticSearch cluster and the mapping available in the index, as described in the previous recipes.
How to do it...
The HTTP method to check the existence is HEAD.
The URL format for checking an index is:
http://<server>/<index_name>/
The URL format for checking a type is:
http://<server>/<index_name>/<type>/
To check if an index exists, we will perform the following steps:
If we consider the index created in the Creating an index recipe in this chapter, the call will be:
curl –i -XHEAD 'http://localhost:9200/myindex/'
The
–i curl options
allows dumping the server headers.If the index exists, an HTTP...