Get An Elasticsearch Cluster's Indices Using Python


If you’re attempting to integrate Elasticsearch with Python one of the first challenges you’ll encounter will be how to get a list of your Elasticsearch Clusters indices through Python. It’s a small task but is the foundation that leads to much more advanced interactions. In this tutorial we’ll detail exactly how to get a list of your Elasticsearch Cluster’s indices using Python.


There are a few prerequisites we recommend before we get started: You should have an Elasticsearch cluster running with at least one index. You should have Python installed on the same server that’s running Elasticsearch. While not required we recommend you have a basic understanding of the Python syntax. You should have the elasticsearch Python library for the low-level Elasticsearch client installed. * If you don’t have the library installed you can use the pip (Python’s package manager) to install the library. Let’s take a look at how to install the library. We’ll show the commands for both Python 2 and Python 3. Execute the command that matches your version of Python. (NOTE: We recommend using Python 3 as Python 2 is being deprecated)

# Python 2 uses 'pip':
pip install elasticsearch

# Python 3 uses 'pip3':
pip3 install elasticsearch

Import Elasticsearch and create an instance of the client

With the elasticsearch library installed our next step will be to import the Elasticsearch module and make sure that the 'host' key is set to localhost and port 9200, the default port for Elasticsearch. Let’s take a look at the Python code to do that.

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# import the Elasticsearch low-level client
from elasticsearch import Elasticsearch

# create a client instance from the Elasticsearch module
elastic_client = Elasticsearch([{'host': 'localhost', 'port': 9200}])

Now we have an elastic_client variable that we’ll use to interact with Elasticsearch with.

Attributes of the Elasticsearch client’s Indices class

Our connection with Elasticsearch exists in the elastic_client variable, and there is a tremendous amount of utility methods on this object like flush() or refresh() that will help maintain your indices. We’ll be using the dir() function which will retrieve all of the attributes of the Indices class. We use indices on the elastic_client variable to get our indices into a variable called indexes. Let’s take a look at the code:

indexes = elastic_client.indices
print (dir(indexes))

To check if a specific index exists we can pass a index name (string) into the exists() method to check if it exists on the cluster. The function will return a True or False Python boolean depending on whether the index exists in the cluster. Let’s take a look at a code sample for checking if an index exists:

indexes = elastic_client.indices
print ("Does 'some_index' exist:", indexes.exists('some_index'))

Get all of the indices on an Elasticsearch cluster using the ‘get_alias()’ method:

The client library’s Indices class has another utility method .get_alias() which returns a Python dictionary of all the indices with the index name as the key. We’ll execute this method and then print the result:

index_aliases = elastic_client.indices.get_alias()
print ("index_aliases:",index_aliases)

Our next step is to enumerate through the dictionary of indexes from the previous step and print them out using a for loop. Let’s take a look at the Python code snippet:

for num, index in enumerate(index_aliases):
    print (num, '--', index)

Getting all of the Elasticsearch indices on a cluster using the Python client library in an IDLE shell:

Screenshot of Python's IDLE getting the indices in an Elasticsearch cluster


In this tutorial we used Python to retrieve a list of indices from Elasticsearch. We showed you how to install the elasticsearch library needed through pip and demonstrated the Python code you’d need to retrieve the indices into a dictionary and how to loop through them. Specifically we utilized the get_alias() method from the Elasticsearch client which returns all the indices in a cluster. We hope you found this tutorial helpful and if you have any questions don’t hesitate to reach out to us.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.