Community Blog Practical Tips: Mastering Elasticsearch with Python

Practical Tips: Mastering Elasticsearch with Python

This article will delve into combining Elasticsearch with Python to enhance our projects, specifically utilizing the environment on Alibaba Cloud Elasticsearch.

Elasticsearch has become a popular choice for its powerful full-text search capabilities as a robust, open-source, distributed search and data analytics engine. Despite Elasticsearch's Java core, its RESTful API invites developers from various programming languages, including Python, to interact seamlessly. This article will delve into combining Elasticsearch with Python to enhance our projects, specifically utilizing the environment on Alibaba Cloud Elasticsearch.

Getting Started

Before diving into the technicalities, ensure you have a running Elasticsearch 8.X instance and a Kibana instance.

For deployment details, I recommend reading Chapter 3 of "Elasticsearch Deep Dive," focusing on Elasticsearch cluster deployment.

Introducing Elasticsearch Python Clients

Several Python libraries are available for interacting with Elasticsearch:

  • elasticsearch-py: The official low-level client offers direct and flexible access.
  • elasticsearch-dsl: A higher-level wrapper around elasticsearch-py that simplifies operations, recommended for daily use.
  • django-elasticsearch-dsl: Designed for Django users, integrates deeply with the Django framework using elasticsearch-dsl.

Use Cases and Pros/Cons of Various Elasticsearch Python Clients

Client Use Cases Pros Cons
elasticsearch-py Direct low-level operations with Elasticsearch Full access to Elasticsearch APIs, high flexibility Complex code, steep learning curve
elasticsearch-dsl Complex search queries Simplifies query construction, Pythonic interface, less syntax error risk Steeper learning curve
django-elasticsearch-dsl Using Elasticsearch in Django projects Seamless integration with Django, auto-syncing of models and documents Limited to Django, more abstraction layers

Basic CRUD Operations with elasticsearch-py

The official low-level Python client elasticsearch-py allows performing all basic and advanced Elasticsearch operations. Let's start with some basic operation examples:

Import Dependencies and Initialize Client

from elasticsearch import Elasticsearch
import configparser
import warnings


def init_es_client(config_path='./conf/config.ini'):
    # Initialize parser
    config = configparser.ConfigParser()
    # Read config
    # Retrieve Elasticsearch configuration
    es_host = config.get('elasticsearch', 'ES_HOST')
    es_user = config.get('elasticsearch', 'ES_USER')
    es_password = config.get('elasticsearch', 'ES_PASSWORD')
    es = Elasticsearch(
        basic_auth=(es_user, es_password),
    return es

Create Index

def create_index(es, index_name="test-index"):
    if not es.indices.exists(index=index_name):

Define Mapping

def define_mapping(es, index_name="test-index"):
    mapping = {
        "mappings": {
            "properties": {
                "name": {"type": "text"},
                "age": {"type": "integer"},                "email": {"type": "keyword"}
    es.indices.create(index=index_name, body=mapping, ignore=400)

Insert, Update, and Delete Documents

def insert_document(es, index_name="test-index", doc_id=None, document=None):
    es.index(index=index_name, id=doc_id, document=document)

def update_document(es, index_name="test-index", doc_id=None, updated_doc=None):
    es.update(index=index_name, id=doc_id, body={"doc": updated_doc})

def delete_document(es, index_name="test-index", doc_id=None):
    es.delete(index=index_name, id=doc_id)

Search Documents

def search_documents(es, index_name="test-index", query=None):
    return es.search(index=index_name, body=query)

Full Example in the main Function

def main():
    # Initialization
    es = init_es_client()
    # Index creation and mapping
    # Document operations
    doc = {"name": "John Doe", "age": 30, "email": "john.doe@example.com
    insert_document(es, "test-index", doc_id="1", document=doc)
    # Update document
    update_document(es, "test-index", "1", {"age": 31})
    # Search document
    query = {"query": {"match": {"name": "John Doe"}}}
    results = search_documents(es, "test-index", query)
    # Delete document
    delete_document(es, "test-index", "1")
if __name__ == "__main__":

This code demonstrates the process of interacting with Elasticsearch using the elasticsearch-py library. From creating and deleting indices, defining mappings, and performing CRUD operations on documents to executing simple searches, it encapsulates a wide range of fundamental Elasticsearch operations.

Transitioning to elasticsearch-dsl for Simplified Operations

While elasticsearch-py provides comprehensive access to Elasticsearch capabilities, elasticsearch-dsl simplifies complex search query construction, showcasing a more Pythonic approach. The following snippets illustrate how to utilize elasticsearch-dsl effectively:

Initialize Elasticsearch DSL Client

from elasticsearch_dsl import Document, Text, Integer, connections, Search

def init_es_client_dsl(config_path='./conf/config.ini'):
    config = configparser.ConfigParser()
    es_host = config.get('elasticsearch', 'ES_HOST')
    es_user = config.get('elasticsearch', 'ES_USER')
    es_password = config.get('elasticsearch', 'ES_PASSWORD')
        http_auth=(es_user, es_password),

Define a Document and Create an Index

class MyDocument(Document):
    name = Text()
    age = Integer()
    email = Text()
    class Index:
        name = 'test-index-dsl'
def create_index_dsl():

Insert and Update Documents

def insert_document_dsl(document):
    doc = MyDocument(meta={'id': document.get('id', None)}, **document)

def update_document_dsl(doc_id, updated_doc):
    doc = MyDocument.get(id=doc_id)
    for key, value in updated_doc.items():
        setattr(doc, key, value)

Delete and Search Documents

def delete_document_dsl(doc_id):
    doc = MyDocument.get(id=doc_id)

def search_documents_dsl(query):
    s = Search(index="test-index-dsl").query("match", name=query)
    response = s.execute()
    return response


Whether you choose elasticsearch-py for its direct API access and flexibility or prefer the more abstracted and Pythonic elasticsearch-dsl for simplified query construction, both clients offer powerful ways to interact with Elasticsearch in Python projects. Leveraging these in conjunction with Alibaba Cloud Elasticsearch can significantly enhance your search and data analytics capabilities.

Ready to start your journey with Elasticsearch on Alibaba Cloud? Explore our tailored Cloud solutions and services to take the first step towards transforming your data into a visual masterpiece. Click here, Embark on Your 30-Day Free Trial

0 1 0
Share on

Data Geek

86 posts | 4 followers

You may also like


Data Geek

86 posts | 4 followers

Related Products