pydantic-to-elastic
A CLI and Python library that converts Pydantic 2 models into Elasticsearch mappings.
Installation
pip install pydantic-to-elastic
From source:
git clone https://github.com/malinkinsa/pydantic-to-elastic.git
cd pydantic-to-elastic
pip install .
For development:
pip install -e '.[test]'
pytest
Usage
Given user_models.py:
from datetime import datetime
from pydantic import BaseModel, Field
class Address(BaseModel):
city: str
zip_code: str
class User(BaseModel):
name: str = Field(alias="displayName")
address: Address
previous_addresses: list[Address]
created_at: datetime
Run:
pydantic2es --input ./user_models.py --output_format pretty
The single root model is detected automatically. Singular submodels become
object fields and collections of submodels become nested fields:
{
"mappings": {
"properties": {
"displayName": {"type": "keyword"},
"address": {
"type": "object",
"properties": {
"city": {"type": "keyword"},
"zip_code": {"type": "keyword"}
}
},
"previous_addresses": {
"type": "nested",
"properties": {
"city": {"type": "keyword"},
"zip_code": {"type": "keyword"}
}
},
"created_at": {"type": "date", "ignore_malformed": true}
}
}
}
CLI options
| Option | Description | Default |
|---|---|---|
--input PATH |
Python file containing Pydantic models; required | — |
--model NAME |
Root model to convert; repeat or comma-separate for several | auto-detect |
--output console|file |
Output destination | console |
--output_path PATH |
Destination filename; required for file output | — |
--output_format json|pretty |
Compact or indented JSON | json |
--submodel_type auto|nested|object |
Submodel mapping policy | auto |
--text_fields FIELD |
Force names or dotted paths to text |
— |
--flattened_fields FIELD |
Force names or dotted paths to flattened |
— |
Field options may be repeated or comma-separated:
pydantic2es \
--input ./user_models.py \
--text_fields displayName,address.city \
--flattened_fields metadata
A bare field name applies at every nesting level. A dotted path such as
address.city applies only at that path. Assigning the same selector to both
text and flattened is an error.
When a file contains several independent root models, select one explicitly:
pydantic2es --input ./models.py --model User
If --model is repeated, the output is keyed by model name.
Python API
from pydantic2es import load_model_classes, model_to_mapping
models = load_model_classes("user_models.py")
mapping = model_to_mapping(models["User"])
Supported annotations include Pydantic submodels, optional fields, lists and
sets, dictionaries, Literal, string/numeric enums, datetime, date, UUID,
Decimal, bytes, and standard scalar types. Elasticsearch has no direct mapping
for every possible Python union or custom class; unsupported or ambiguous types
produce a descriptive error instead of emitting "type": null.
Recursive models are rejected because an Elasticsearch mapping must be finite.
Security
The input Python file is imported and therefore executes its top-level code. Only convert model files you trust.
Metadata
Release files for pydantic-to-elastic 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydantic_to_elastic-0.1.0.tar.gz | 16.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_to_elastic-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.4 kB
Release files / pydantic_to_elastic-0.1.0.tar.gz
| Download URL | pydantic_to_elastic-0.1.0.tar.gz |
|---|---|
| Size | 16.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
68a2583cfde6b188c48d029688900d1b554aad2fbdcdb786eac9e5e60c1d2aea
|
|
BLAKE2b-256 checksum How to use checksums |
29c56fba686a99e7186b42b885eacc14aac40da2894e607b7077efa506b0bd6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / pydantic_to_elastic-0.1.0-py3-none-any.whl
| Download URL | pydantic_to_elastic-0.1.0-py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ee88c127a72ef444ed0f2a29c733670e0bfaaa90bc5bddf738e25ad829a446de
|
|
BLAKE2b-256 checksum How to use checksums |
2a732ec03f2840c1ae343e642c9e1d7bc41d804a0f0282cbdb6ac2b39efa0522
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|