High-level Python library for Elasticsearch providing an idiomatic way to write and manipulate queries.
—
A high-level Python library for Elasticsearch that provides an idiomatic way to write and manipulate queries. Built on top of the official low-level elasticsearch-py client, it stays close to the Elasticsearch JSON DSL while exposing the whole range of functionality through Python classes and expressions. This package also provides optional document persistence with Python object mapping and index management capabilities.
Note: As of version 8.18.0, this package serves as a compatibility layer that redirects imports to elasticsearch.dsl in the main elasticsearch package.
pip install elasticsearch-dslimport elasticsearch_dslCommon imports for document and search operations:
from elasticsearch_dsl import Document, Search, Q, A, connectionsField types for document mapping:
from elasticsearch_dsl import Text, Keyword, Date, Integer, Boolean, Object, Nestedfrom elasticsearch_dsl import Document, Search, Text, Keyword, Date, Integer, connections
# Configure connection
connections.create_connection(hosts=['localhost:9200'])
# Define a document
class Article(Document):
title = Text(analyzer='snowball')
body = Text(analyzer='snowball')
tags = Keyword()
published_date = Date()
view_count = Integer()
class Index:
name = 'blog_articles'
# Create index and mappings
Article.init()
# Create and save a document
article = Article(
title='Getting Started with Elasticsearch DSL',
body='This article explains how to use elasticsearch-dsl...',
tags=['elasticsearch', 'python', 'tutorial'],
published_date='2023-10-01',
view_count=150
)
article.save()
# Search for documents
search = Search(index='blog_articles')
search = search.filter('term', tags='elasticsearch')
search = search.query('match', title='elasticsearch')
response = search.execute()
for hit in response:
print(f"{hit.title} - Views: {hit.view_count}")Elasticsearch DSL organizes functionality into several key components:
This architecture enables both simple document operations and complex analytical queries while maintaining type safety and providing a Pythonic interface to Elasticsearch's full feature set.
Object-relational mapping for Elasticsearch documents with automatic index management, CRUD operations, bulk processing, and lifecycle hooks. Supports both synchronous and asynchronous operations.
class Document:
def __init__(self, **kwargs): ...
def save(self, **kwargs): ...
def delete(self, **kwargs): ...
@classmethod
def get(cls, id, **kwargs): ...
@classmethod
def init(cls, **kwargs): ...
class AsyncDocument:
async def save(self, **kwargs): ...
async def delete(self, **kwargs): ...
@classmethod
async def get(cls, id, **kwargs): ...Fluent API for building and executing Elasticsearch queries with support for filtering, sorting, pagination, and result processing. Includes both basic and advanced query types.
class Search:
def __init__(self, **kwargs): ...
def query(self, query, **kwargs): ...
def filter(self, query, **kwargs): ...
def exclude(self, query, **kwargs): ...
def sort(self, *keys): ...
def execute(self): ...
def Q(name, **params): ...Comprehensive field type system for defining document structure and Elasticsearch mappings with support for all Elasticsearch field types including text, numeric, date, geographic, and specialized fields.
class Text:
def __init__(self, analyzer=None, **kwargs): ...
class Keyword:
def __init__(self, **kwargs): ...
class Date:
def __init__(self, format=None, **kwargs): ...
class Integer:
def __init__(self, **kwargs): ...
class Object:
def __init__(self, properties=None, **kwargs): ...
class Nested:
def __init__(self, **kwargs): ...Statistical analysis and data grouping with support for metric aggregations, bucket aggregations, and pipeline aggregations. Enables complex analytical queries and data summarization.
def A(name, **params): ...
class Aggs:
def bucket(self, name, agg_type, **params): ...
def metric(self, name, agg_type, **params): ...Custom analyzer creation, tokenizer configuration, and text processing setup for multilingual and domain-specific search requirements.
def analyzer(name, **kwargs): ...
def tokenizer(name, **kwargs): ...
def char_filter(name, **kwargs): ...
def token_filter(name, **kwargs): ...Connection configuration and management for single and multiple Elasticsearch clusters with support for authentication, SSL, connection pooling, and both synchronous and asynchronous clients.
def create_connection(alias='default', **kwargs): ...
def get_connection(alias='default'): ...
def configure(**kwargs): ...
# Async variants
def create_async_connection(alias='default', **kwargs): ...
def get_async_connection(alias='default'): ...Index creation, configuration, and lifecycle management with support for settings, mappings, aliases, and templates. Enables comprehensive control over Elasticsearch indices and their behavior.
class Index:
def __init__(self, name, using=None): ...
def settings(self, **kwargs): ...
def create(self, ignore=400, **kwargs): ...
def delete(self, ignore=404, **kwargs): ...
class IndexTemplate:
def __init__(self, name, using=None): ...
def body(self, **kwargs): ...
def create(self, **kwargs): ...The library raises specific exceptions for different error conditions:
class ElasticsearchDslException(Exception): ...
class UnknownDslObject(ElasticsearchDslException): ...
class ValidationException(ElasticsearchDslException): ...
class IllegalOperation(ElasticsearchDslException): ...Common exceptions from the underlying elasticsearch package should also be handled in application code.
Install with Tessl CLI
npx tessl i tessl/pypi-elasticsearch-dsl