Skip to main content

PyPi CI Code coverage Python Issues Commit activity Downloads License

🔍 elasticsearch-pydantic

Use the Elasticsearch DSL with Pydantic models.

This minimal library is for those who...

To interconnect the Elasticsearch DSL and Pydantic, we override a limited set of methods from the Elasticsearch Document and InnerDoc base classes with Pydantic's BaseModel functionality. Elasticsearch field types are inferred from the model's type annotations and can be overridden by using Annotated type hints.

Installation

Install the package from PyPI:

pip install elasticsearch-pydantic

Usage

To migrate from Elasticsearch DSL to elasticsearch-pydantic, just change your ORM classes to inherit from elasticsearch_pydantic.BaseDocument instead of elasticsearch_dsl.Document. Then, gradually replace your field definitions with Pydantic type annotations.

For example, in Elasticsearch DSL, you would typically define a document like this:

from elasticsearch_dsl import Document, Text, Date

class BlogPost(Document):
    title = Text()
    content = Text()
    published_at = Date()

With elasticsearch-pydantic, you can define the same document using Pydantic models:

from elasticsearch_pydantic import BaseDocument

class BlogPost(BaseDocument):
    title: str
    content: str
    published_at: datetime

And that's about it! You now get all the type-safety and validation benefits of Pydantic, while still being able to use the powerful features of Elasticsearch DSL.

Most Pydantic types are naturally mapped to Elasticsearch field types. To learn more about the field type mappings, see the mapping code.

Annotated types

You can use Annotated type hints to customize the Elasticsearch field types:

from typing import Annotated
from elasticsearch_dsl import Text, Keyword
from elasticsearch_pydantic import BaseDocument

class BlogPost(BaseDocument):
    title: Annotated[str, Text(analyzer="standard")]
    tags: Annotated[list[str], Keyword]

Field type aliases

For convenience, elasticsearch-pydantic provides type aliases for all standard Elasticsearch field types:

from elasticsearch_pydantic import BaseDocument, TextField, KeywordField

class BlogPost(BaseDocument):
    title: TextField
    tags: list[KeywordField]

Accessing meta fields

For convenience, the Elasticsearch meta fields (like _id, _index, _score, etc.) are directly accessible as attributes on your document models:

post = BlogPost(id="1", title="My first post")
print(post.id)        # Access the document ID
print(post.meta.index)  # Access the index name

The meta attribute is kept for compatibility with Elasticsearch DSL and contains all meta fields.

Compatibility

This library works fine with any of the following Pip packages installed:

The elasticsearch-pydantic library will automatically detect which Elasticsearch DSL is installed.

Development

To build this package and contribute to its development you need to install the build, setuptools and wheel packages:

pip install build setuptools wheel

(On most systems, these packages are already pre-installed.)

Development installation

Install package and test dependencies:

pip install -e .[tests,tests-es6]       # For elasticsearch-dsl~=6.0
pip install -e .[tests,tests-es6-major] # For elasticsearch6-dsl
pip install -e .[tests,tests-es7]       # For elasticsearch-dsl~=7.0
pip install -e .[tests,tests-es7-major] # For elasticsearch7-dsl
pip install -e .[tests,tests-es8]       # For elasticsearch-dsl~=8.0
pip install -e .[tests,tests-es8-major] # For elasticsearch8-dsl

Testing

Verify your changes against the test suite to verify.

ruff check .  # Code format and LINT
mypy .        # Static typing
pytest .      # Unit tests

Please also add tests for your newly developed code.

Build wheels

Wheels for this package can be built with:

python -m build

Support

If you have any problems using this package, please file an issue. We're happy to help!

License

This repository is released under the MIT license.

Metadata

Release files for elasticsearch-pydantic 1.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elasticsearch-pydantic 1.2.1
File Size Uploaded Provenance Actions
elasticsearch_pydantic-1.2.1.tar.gz 21.6 kB With provenance Details for elasticsearch_pydantic-1.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for elasticsearch-pydantic 1.2.1
File Interpreter ABI Platform Actions
elasticsearch_pydantic-1.2.1-py3-none-any.whl Python 3 none any Details for elasticsearch_pydantic-1.2.1-py3-none-any.whl

Total release size: 40.1 kB

Release files / elasticsearch_pydantic-1.2.1.tar.gz

Download URL elasticsearch_pydantic-1.2.1.tar.gz
Size 21.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b1743029c1f8b0ada96fd6a60c31f152ac22f0341165afa27539ef4e7c816cf7
BLAKE2b-256 checksum
How to use checksums
ce5b1d69d6231c95389f46c1abc24d50a653ea6cf8d81c174830adeafd8ff76e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / elasticsearch_pydantic-1.2.1-py3-none-any.whl

Download URL elasticsearch_pydantic-1.2.1-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e30e16c0b2be22c48e72139727d963595bc625ca654f56c765c3fe528c639fff
BLAKE2b-256 checksum
How to use checksums
61bb9a1bb61562c04788f31735b9449f309f3ef0e986d18a330a4581b77c2e21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for elasticsearch-pydantic 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for elasticsearch_pydantic-1.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page