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:

  1. If we consider ...

Get ElasticSearch Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.