Elastic Search

Elasticsearch List All Indexes

“In Elasticsearch, an index refers to a logical namespace containing mappings of multiple types. Think of an index as a database in a relational database. An index is essential as it lays out the foundation for organizing and partitioning data.”

In this article, we will show you various methods and techniques you can use to fetch all the indexes within an Elasticsearch cluster.

Thanks for reading!!

Elasticsearch Cat API

Elasticsearch relies heavily on RESTful APIs. Therefore, almost any operation that needs to be performed on Elasticsearch can be accomplished with a specific API endpoint.

To get a list of all indices in an Elasticsearch cluster, we can use the cat API, which provides the functionality to view and gather information about various parts of the cluster.

NOTE: Although cat APIs are very useful when working with command-line and Kibana console consumption, Elasticsearch prohibits using them for application consumption.

That being said, let’s dive in.

Request Syntax

The request to view all the indices in a cluster follows a simple syntax as shown:

GET /_cat/indices

The request will then return information about each index in the Elasticsearch cluster. Such information includes:

  1. Number of shards
  2. Number of documents in the index
  3. Number of deleted documents
  4. Primary store size
  5. The total size of all shards and replicas.

Path Parameters

The request allows you to specify the target information you wish to retrieve by specifying the target parameter. The target parameter is a list of comma-separated values: indices, alias, data streams, etc.

The syntax is as shown:

GET /_cat/indices/<target>

Query Parameters

You can also pass other parameters in the query. The most popular parameter you need to know include:

  1. bytes – specifies the unit used to show byte values.
  2. h – defines the columns to display as comma-separated values.
  3. health – return the indices with the specified health status. Accepted values include:

      a. green
      b. yellow
      c. red

  4. help – display help info.
  5. s – columns used to sort the response.
  6. v – include column headings (default to false).
  7. expand_wildcards – specifies if Elasticsearch should expand the specified wildcard patterns.

Let us look at real-world examples of how to use the cat index API.

Example 1 – Show All Indices

To show all indices in the cluster and their information, we can run the request as shown:

curl -XGET "http://localhost:9200/_cat/indices/?v=true" -H "kbn-xsrf: reporting"

The query above should return all indices and their corresponding information.

Example 2

To show only the index name, you can fetch only the index header using the h parameter. An example query is as shown:

curl -XGET "http://localhost:9200 /_cat/indices?h=index" -H "kbn-xsrf: reporting"

The query above should only the index names as shown:

We can include the health, index, and store to include index health and index store.size columns:

An example request is as shown:

curl -XGET "http://localhost:9200/_cat/indices?h=health,index,store.size&v=true" -H "kbn-xsrf: reporting"

The resulting output is as shown:

Conclusion

In this tutorial, you learned how to use the cat index API to retrieve information about all the indices in your cluster.

Thanks for reading!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list