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 correct working.
You need an up-and-running Elasticsearch installation, as used in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
To execute curl
via the command line, you need to install curl
for your operative system.
To correctly execute the following commands, use the index created in the Creating an index recipe.
The HTTP method to check 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 steps given as follows:
curl -i -XHEAD 'http://localhost:9200/myindex/'
-i curl options
allows dumping the server headers.200
is returned, if missing, a 404
. For checking if a type exists, we will perform the steps given as follows:curl -i -XHEAD 'http://localhost:9200/myindex/order/'
200
is returned, if missing, a 404
.This is a typical HEAD REST
call to check existence. It doesn't return body response, but only the status code, which is the result status of the operation.
The most common status codes are:
3.138.34.80