Elasticsearch Python client
This documentation covers the official Python client for Elasticsearch. The goal of the Python client is to provide common ground for all Elasticsearch-related code in Python. The client is designed to be unopinionated and extendable.
API reference documentation is provided on Read the Docs.
The following example shows a simple Python client use case:
>>> from datetime import datetime
>>> from elasticsearch import Elasticsearch
# Connect to 'http://localhost:9200'
>>> client = Elasticsearch("http://localhost:9200")
# Datetimes will be serialized:
>>> client.index(index="my-index-000001", id=42, document={"any": "data", "timestamp": datetime.now()})
{'_id': '42', '_index': 'my-index-000001', '_type': 'test-type', '_version': 1, 'ok': True}
# ...but not deserialized
>>> client.get(index="my-index-000001", id=42)['_source']
{'any': 'data', 'timestamp': '2013-05-12T19:45:31.804229'}
The client's features include:
- Translating basic Python data types to and from JSON
- Configurable automatic discovery of cluster nodes
- Persistent connections
- Load balancing (with pluggable selection strategy) across all available nodes
- Node timeouts on transient errors
- Thread safety
- Pluggable architecture
The client also provides a convenient set of helpers for tasks like bulk indexing and reindexing.
To get started, try this walkthrough: Ingest data with Python
The Python DSL module offers a convenient and idiomatic way to write and manipulate queries.
Language clients are forward compatible: each client version works with equivalent and later minor versions of the same or next major version of Elasticsearch. For full compatibility, the client and Elasticsearch minor versions should match.
| Client version | Elasticsearch 8.x |
Elasticsearch 9.x |
Elasticsearch 10.x |
|---|---|---|---|
| 9.x client | ❌ Not compatible with Elasticsearch 8.x | ✅ Compatible with Elasticsearch 9.x | ✅ Compatible with Elasticsearch 10.x |
| 8.x client | ✅ Compatible with Elasticsearch 8.x | ✅ Compatible with Elasticsearch 9.x | ❌ Not compatible with Elasticsearch 10.x |
Compatibility does not imply feature parity. New Elasticsearch features are supported only in equivalent client versions. For example, an 8.12 client fully supports Elasticsearch 8.12 features and works with 8.13 without breaking, but it does not support new Elasticsearch 8.13 features. An 8.13 client fully supports Elasticsearch 8.13 features.
Elasticsearch language clients are also backward compatible across minor versions, with default distributions and without guarantees.
To upgrade to a new major version, first upgrade Elasticsearch, then upgrade the Python client.
As of version 8.0, Elasticsearch offers a compatibility mode for smoother upgrades. In compatibility mode, you can upgrade your Elasticsearch cluster to the next major version while continuing to use your existing client during the transition.
For example, if you're upgrading Elasticsearch from 8.x to 9.x, you can continue to use the 8.x Python client during and after the Elasticsearch server upgrade, with the exception of breaking changes.
In the Python client, compatibility mode is always enabled.
To support working with multiple client versions, the Python client is also released under the package names elasticsearch8 and elasticsearch9 (to prevent name collisions).