Connecting to Elasticsearch with Python: Essential Factors for a Successful Integration

Rishab Batra
3 min readMay 20


Image Source


Python provides an official Elasticsearch client library called Elasticsearch-py, which allows you to interact with Elasticsearch using Python. With Elasticsearch-py, you can perform various operations such as indexing, searching, updating, and deleting documents in an Elasticsearch cluster.

Usage of this doc:

To interact with the ELK (Elasticsearch, Logstash, and Kibana) stack using Python, you need to consider a few points mentioned below to establish a successful connection with Elasticsearch.


To work with Elasticsearch in Python, you’ll need to install Elasticsearch and Kibana and ensure their servers are running, so install them from their official links.
Install the Elasticsearch-Py library using pip:

Issues I encountered and how overcame them one by one

I have faced a few issues while connecting to Elasticsearch. I want to mention them here so that you don’t get stuck in endless loops of tests and trials and you don't end up wasting time.

When connecting to Elasticsearch, there are several essential considerations to remember to ensure a successful and secure connection.

Number 1 is when I initially connected, I used the below snippet:

client = Elasticsearch(hosts=[‘https://localhost:9200’])

Now, I got one error

TypeError: __init__() missing 1 required positional argument: ‘scheme’ in Elastic Search

Then, I researched it and found that I needed to add one more argument, as the error suggested.

Add "scheme": "https" to the client

Now after adding the above argument, this is how your connection string looks like:

client = Elasticsearch(hosts=[‘https://localhost:9200’],scheme="https")

Now I tried connecting and pinging it.

Still, I was not able to connect.


Number two is I haven’t provided the credential here,
So after adding my credential, this is how my client looks like:

client = Elasticsearch(hosts=[‘https://localhost:9200’],scheme="https"
basic_auth=[“my_admin”, “yourpassword”])

Now Number three, I got issues with SSL handshake failure:

Elasticsearch throws SSL error for certification verification

I was like, what is happening? Let’s turn off this machine for today...

Then I added and experimented with use_ssl=True, and verify_certs=False after my research.

And at number four got one more error due to use_ssl=True

Later found, that the Python Elasticsearch client does not allow function parameters. use_ssl=True in its latest releases

So, I removed that parameter and

“Now, my client looks like this.”

client = Elasticsearch(hosts=[‘https://localhost:9200’],scheme="https",verify_certs = False
basic_auth=[“my_admin”, “yourpassword”])

And when I ping my connection,

You know what happen??

You know what happen??

You know what happen??


It returned true

Voila it worked!

Here are some other vital things to take care of:

Network Connectivity: Ensure that your Elasticsearch cluster is accessible from the machine where you are running your application. Verify that no network restrictions or firewalls are blocking the connection between your application and Elasticsearch.

Version Compatibility: Make sure that the version of Elasticsearch-py library you are using is compatible with the version of Elasticsearch you have deployed. Different versions may have different API syntax and features, so matching them correctly is essential.

Authentication and Security: If your Elasticsearch cluster is secured with authentication or encryption, you must provide the appropriate credentials and ensure your connection is secure. Elasticsearch supports security mechanisms like basic authentication, SSL/TLS encryption, and integration with external authentication providers.

Error Handling: Handling exceptions properly is crucial to gracefully handle connection failures, timeouts, and other error scenarios. Catch and handle exceptions raised by Elasticsearch-py operations to provide users with meaningful feedback and ensure your application's stability.


Elasticsearch provides near-real-time indexing, meaning that once you index a document, it becomes quickly searchable and available for analysis. This is crucial for applications that require real-time data updates and fast search responses. By considering the above aspects and best practices, you can establish a reliable connection to Elasticsearch.”

Let me know in the comments if you encounter any such issues from above.And please refer to the official docs of Elasticsearch for security purposes.

Thank you!



Rishab Batra

Here to share life lessons. We are from the other side of the universe!