How To Get An Elasticsearch Cluster's Information In Python

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

If you’re running an Elasticsearch cluster, you’ll occasionally need to get information about the cluster. The Elasticsearch client for Python makes this task quick and simple– all it takes is a few lines of code in Python to connect to Elasticsearch and retrieve the information you need. In this step-by-step tutorial, we’ll explain how to retrieve Elasticsearch cluster information in Python.

Prerequisites

Before we can attempt to get cluster information in Python, it’s important to make sure certain prerequisites are in place. For this task, there are a few key system requirements:

  • The server or machine must have Elasticsearch installed and running. You can see if the Elasticsearch service is running by visiting https://{YOUR_DOMAIN}:9200 or localhost:9200 in your browser.
  • Python (version 2 or 3) must be installed on the server.
  • The Python low-level Elasticsearch client library must be installed. You can install the client library using the PIP package manager for Python:
1
2
3
4
# 'pip3' is for Python 3 packages:
pip3 install elasticsearch
# or use 'pip' to install Python 2 packages:
pip install elasticsearch

NOTE: Python 3 is soon to be the standard for Python, as Python 2 is being depreciated.

Import the Python module for the Elasticsearch client

Once you’ve confirmed all the system requirements, you’re ready to begin your Python script. Use the Python import statement to add the library for Elasticsearch to the script or Python shell environment:

1
from elasticsearch import Elasticsearch

If you encounter an import error, it’s likely that the Elasticsearch client library wasn’t installed properly, or you installed the library for the wrong version of Python.

Create a new client instance for the Elasticsearch Python library

You’ll need to create a client instance in order to make requests and changes to the Elasticsearch cluster and its indexes; however, the client instance has other useful methods as well.

The code shown below creates a client instance that we’ll use to call the info() method:

1
elastic = Elasticsearch()

You may also pass a string into the hosts array as a parameter, specifying the domain name, or IP address, of the server running Elasticsearch; however, this parameter is optional:

1
2
3
4
5
6
7
8
# domain name, or server's IP address, goes in the 'hosts' array
elastic = Elasticsearch(hosts=["localhost"])
"""
OR USE ONE OF THESE:
elastic = Elasticsearch('localhost')
elastic = Elasticsearch(hosts=["http://www.example.com/"])
elastic = Elasticsearch(hosts=["123.456.789"])
"""

Call the client’s info() method to get information on an Elasticsearch cluster

Now that our client instance is set up, we can make our request for Elasticsearch cluster information. The client instance has a built-in method called info() that returns information about the cluster. Calling this method returns a nested Python dictionary– in other words, a dictionary inside of a dictionary:

1
elastic.info()

The outer dictionary contains keys for the cluster name, UUID, logical name, and its version dictionary.

Call the Python client’s info() method, and have it return a nested dictionary with information about the cluster

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

Get details about the Elasticsearch cluster’s version:

The value for the version key itself is a dictionary, nested inside the outer dictionary, that can provide more detailed information about the Elasticsearch cluster. You can access the version key inside the nested dictionary returned by the client’s info() method:

1
2
3
4
5
6
7
8
# store the method call's returned dictionary in a variable
info = cluster.info()

# access the different keys of the nested dictionary
# and print out their respective value:
print ("Cluster Name:", info['cluster_name'])
print ("Cluster version dictionary:", info['version'])
print ("Cluster version number:", info['version']['number'])

Conclusion

It’s important to know how to retrieve basic information about your Elasticsearch cluster, and the Elasticsearch client for Python makes this task a simple one. Just a few lines of code in your Python script allows you to get the essential information you need about your Elasticsearch cluster, such as the cluster name, the UUID and the version of Elasticsearch that’s running. With the instructions provided in this article, you’ll have no trouble using the Elasticsearch client for Python to get information about your cluster.

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.