This documentation contains work-in-progress information for future Elastic Stack and Cloud releases. Use the version selector to view supported release docs. It also contains some Elastic Cloud serverless information. Check out our serverless docs for more details.
Elasticsearch Python DSL
edit
IMPORTANT: This documentation is no longer updated. Refer to Elastic's version policy and the latest documentation.
Elasticsearch Python DSL
editElasticsearch DSL is a module of the official Python client that aims to help with writing and running queries against Elasticsearch in a more convenient and idiomatic way. It stays close to the Elasticsearch JSON DSL, mirroring its terminology and structure. It exposes the whole range of the DSL from Python either directly using defined classes or a queryset-like expressions. Here is an example:
from elasticsearch.dsl import Search s = Search(index="my-index") \ .filter("term", category="search") \ .query("match", title="python") \ .exclude("match", description="beta") for hit in s: print(hit.title)
Or with asynchronous Python:
from elasticsearch.dsl import AsyncSearch async def run_query(): s = AsyncSearch(index="my-index") \ .filter("term", category="search") \ .query("match", title="python") \ .exclude("match", description="beta") async for hit in s: print(hit.title)
It also provides an optional wrapper for working with documents as Python objects: defining mappings, retrieving and saving documents, wrapping the document data in user-defined classes.
To use the other Elasticsearch APIs (eg. cluster health) just use the regular client.
Was this helpful?
Thank you for your feedback.