Python Elasticsearch Client
Official low-level client for Elasticsearch. Its goal is to provide common ground for all Elasticsearch-related code in Python; because of this it tries to be opinion-free and very extendable.
Installation
Install the elasticsearch
package with pip:
$ python -m pip install elasticsearch
If your application uses async/await in Python you can install with
the async
extra:
$ python -m pip install elasticsearch[async]
Read more about how to use asyncio with this project.
Compatibility
Language clients are forward compatible; meaning that clients support communicating with greater or equal minor versions of Elasticsearch. Elasticsearch language clients are only backwards compatible with default distributions and without guarantees made.
If you have a need to have multiple versions installed at the same time older
versions are also released as elasticsearch2
, elasticsearch5
and elasticsearch6
.
Example Usage
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
'timestamp': datetime.now(),
}
resp = es.index(index="test-index", id=1, document=doc)
print(resp['result'])
resp = es.get(index="test-index", id=1)
print(resp['_source'])
es.indices.refresh(index="test-index")
resp = es.search(index="test-index", query={"match_all": {}})
print("Got %d Hits:" % resp['hits']['total']['value'])
for hit in resp['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
Interactive examples
The elasticsearch-labs repo contains interactive and executable Python notebooks, sample apps, and resources for testing out Elasticsearch, using the Python client. These examples are mainly focused on vector search, hybrid search and generative AI use cases, but you’ll also find examples of basic operations like creating index mappings and performing lexical search.
Features
This client was designed as very thin wrapper around Elasticsearch’s REST API to allow for maximum flexibility. This means that there are no opinions in this client; it also means that some of the APIs are a little cumbersome to use from Python. We have created some Helpers to help with this issue as well as a more high level library (elasticsearch-dsl) on top of this one to provide a more convenient way of working with Elasticsearch.
Elasticsearch-DSL
For a more high level client library with more limited scope, have a look at
elasticsearch-dsl - a more pythonic library sitting on top of
elasticsearch-py
.
elasticsearch-dsl provides a more convenient and idiomatic way to write and manipulate queries by mirroring the terminology and structure of Elasticsearch JSON DSL while exposing the whole range of the DSL from Python either directly using defined classes or a queryset-like expressions.
It also provides an optional persistence layer for working with documents as Python objects in an ORM-like fashion: defining mappings, retrieving and saving documents, wrapping the document data in user-defined classes.
Contents
- Elasticsearch API Reference
- Elasticsearch
- Async Search
- Autoscaling
- Cat
- Cross-Cluster Replication (CCR)
- Cluster
- Dangling Indices
- Enrich Policies
- Event Query Language (EQL)
- Snapshottable Features
- Fleet
- Graph Explore
- Index Lifecycle Management (ILM)
- Indices
- Ingest Pipelines
- License
- Logstash
- Migration
- Machine Learning (ML)
- Monitoring
- Nodes
- Query rules
- Rollup Indices
- Search Applications
- Searchable Snapshots
- Security
- Shutdown
- Snapshot Lifecycle Management (SLM)
- Snapshots
- SQL
- Synonyms
- TLS/SSL
- Tasks
- Text Structure
- Transforms
- Watcher
- X-Pack
- Exceptions & Warnings
- Using Asyncio with Elasticsearch
- Helpers
- Release Notes
License
Copyright 2023 Elasticsearch B.V. Licensed under the Apache License, Version 2.0.