All you need to know about using Elasticsearch in Python

Learn to build a search engine in Python

Lynn Kwong
8 min readApr 2, 2021


Elasticsearch and Python

In this article, we are going to talk about how to use Elasticsearch in Python. As a data engineer, you may need to create Elasticsearch documents in Python with some scripts. As a software engineer, when you design your API in Python, you would need to make REST API calls to Elasticsearch to fetch the data. Therefore, if you are using Elasticsearch in your work or plan to learn it, this article can be useful for you.

If you haven’t installed Elasticsearch yet, you can go to the official website of Elasticsearch and download and install Elasticsearch and Kibana on your computer.

Alternatively, if you don’t want to install Elasticsearch and Kibana on your computer, you can choose to use Docker containers for local development. If you haven’t installed Docker and docker-compose yet, you can download them from the official Docker website and install them accordingly. I highly recommend using Docker containers for local development because they are platform-independent and do not have library dependency issues on your computer and thus are easy to maintain. You can use this docker-compose file to start Elasticsearch and Kibana directly.

This docker-compose file specifies the docker images to use and other basic configurations. Importantly, it creates a persistent volume for Elasticsearch so that your data can persist when you restart the containers.

To start the Elasticsearch and Kibana services, which you can understand as docker containers running in the background, you can copy the content in a file named docker-compose.yml, and run this command:

docker-compose up -d

Note that you need to use the -f option to specify the name of the docker-compose file if it is not named as docker-compose.yml.

You can check the status of the services with this command:

docker-compose ps



Lynn Kwong

I’m a Software Developer ( keen on sharing thoughts, tutorials, and solutions for the best practice of software development.