Use the Elasticsearch DSL with Pydantic models.
Project description
🔍 elasticsearch-pydantic
Use the Elasticsearch DSL with Pydantic models.
This minimal library is for those who...
- ... want to benefit from Pydantic's extensive validation and type checking ecosystem,
- ... want the convenient and idiomatic persistence layer and query DSL of Elasticsearch DSL,
- ... and do not want to reimplement everything from scratch.
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file elasticsearch_pydantic-1.1.tar.gz.
File metadata
- Download URL: elasticsearch_pydantic-1.1.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6272cf785d4cb918223ad769e9910076e8b4314aa37c038417aa02ea8c29a15
|
|
| MD5 |
2ca2ace60dac1c439de03ea24abc74ae
|
|
| BLAKE2b-256 |
2e21f1013eb5d4e14d6619762f0c0718c151c9e3ac2ba1d2a51f5d727a6ea875
|
Provenance
The following attestation bundles were made for elasticsearch_pydantic-1.1.tar.gz:
Publisher:
ci.yml on janheinrichmerker/elasticsearch-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
elasticsearch_pydantic-1.1.tar.gz -
Subject digest:
f6272cf785d4cb918223ad769e9910076e8b4314aa37c038417aa02ea8c29a15 - Sigstore transparency entry: 535672818
- Sigstore integration time:
-
Permalink:
janheinrichmerker/elasticsearch-pydantic@21ba3c2a35743e4cb546a9de5598f40fc2089c82 -
Branch / Tag:
refs/tags/1.1 - Owner: https://github.com/janheinrichmerker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@21ba3c2a35743e4cb546a9de5598f40fc2089c82 -
Trigger Event:
push
-
Statement type:
File details
Details for the file elasticsearch_pydantic-1.1-py3-none-any.whl.
File metadata
- Download URL: elasticsearch_pydantic-1.1-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80ce2e4f180a1c7eaa47e2e2ef3e4b2e27c184c49d57d74447bf27413b605765
|
|
| MD5 |
b60b850933dbe99dd219a5d60e020192
|
|
| BLAKE2b-256 |
58f4c620c857dc8c3f42c7c80686a6d6b7dea0b2c874ac783e18f09411c16503
|
Provenance
The following attestation bundles were made for elasticsearch_pydantic-1.1-py3-none-any.whl:
Publisher:
ci.yml on janheinrichmerker/elasticsearch-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
elasticsearch_pydantic-1.1-py3-none-any.whl -
Subject digest:
80ce2e4f180a1c7eaa47e2e2ef3e4b2e27c184c49d57d74447bf27413b605765 - Sigstore transparency entry: 535672829
- Sigstore integration time:
-
Permalink:
janheinrichmerker/elasticsearch-pydantic@21ba3c2a35743e4cb546a9de5598f40fc2089c82 -
Branch / Tag:
refs/tags/1.1 - Owner: https://github.com/janheinrichmerker
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@21ba3c2a35743e4cb546a9de5598f40fc2089c82 -
Trigger Event:
push
-
Statement type: