Skip to main content

es_odm

ODM (Object Document Mapper) for Elasticsearch based on Elasticsearch_dsl and Pydantic. It's also a bridge connecting Mysql and Elasticsearch when using Sqlmodel, which also based on Pydantic.

Installation

Install using pip install -U es_odm.

A Simple Example

import typing

from es_odm import InnerESModel, ESModel, Field, ObjectField


class UserProfileODM(InnerESModel):
    """user profile document"""
    user_id: int = Field(None, description="user id")
    nickname: str = Field(None, description="user nickname", keyword=True)
    avatar_url: str = Field(None, description="user avatar url", keyword=True)
    gender: int = Field(None, description="gender")
    address: str = Field(None, description="address", keyword=True)


class UserODM(ESModel):
    """user document"""
    id: int = Field(None, primary_key=True, description="ID")
    username: str = Field(None, description="login name", keyword=True)

    profile: typing.Union[ObjectField[UserProfileODM], dict] = Field(
        None,
        description="user profile",
    )

    class Config:
        """pydantic BaseModel Config"""
        arbitrary_types_allowed = True

    class Index:
        """elasticsearch_dsl Document Index config"""
        name = 'test-index-name'
        settings = {
            "number_of_shards": 1,
        }


def test_odm_doc():
    doc_example = {
        "id": 1,
        "username": "test_username",
        "profile": {
            "user_id": 100,
            "nickname": "test_nickname",
            "gender": 1,
            "address": "test address"
        }
    }
    doc = UserODM(**doc_example)
    assert doc_example == doc.to_dict()


def test_odm_mapping():
    cls = UserODM
    cls_mapping = cls._index.to_dict()

    es_mapping = {
        "settings": {
            "number_of_shards": 1
        },
        "mappings": {
            "properties": {
                "id": {
                    "type": "integer"
                },
                "username": {
                    "fields": {"keyword": {"type": "keyword"}},
                    "type": "text"
                },
                "last_login": {
                    "type": "date"
                },
                "profile": {
                    "properties": {
                        "user_id": {
                            "type": "integer"
                        },
                        "nickname": {
                            "fields": {
                                "keyword": {
                                    "type": "keyword"
                                }
                            },
                            "type": "text"
                        },
                        "avatar_url": {
                            "fields": {
                                "keyword": {
                                    "type": "keyword"
                                }
                            },
                            "type": "text"
                        },
                        "gender": {
                            "type": "integer"
                        },
                        "address": {
                            "fields": {
                                "keyword": {
                                    "type": "keyword"
                                }
                            },
                            "type": "text"
                        }
                    },
                    "type": "object"
                }
            }
        }
    }
    assert es_mapping == cls_mapping

Release files for es-odm 0.0.6

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

Source distribution (sdist)

Source distribution for es-odm 0.0.6
File Size Uploaded
es_odm-0.0.6.tar.gz 12.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for es-odm 0.0.6
File Interpreter ABI Platform
es_odm-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 23.8 kB

Release files / es_odm-0.0.6.tar.gz

Download URL es_odm-0.0.6.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
83337988c9c0ac08ee009217a5186d306308e94d17472fb33ee97ebbb4d1fcd6
BLAKE2b-256 checksum
How to use checksums
5f68e6912fbc32f8ab56da79f3834ac799347b76abbefcf6cb1eaa6ced895278
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.9

Release files / es_odm-0.0.6-py3-none-any.whl

Download URL es_odm-0.0.6-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
241ccd761536dfcceaac5c75afa48840659a0120bd2ab9d275aeb8f854e3799c
BLAKE2b-256 checksum
How to use checksums
994c44b8050308c6d478cdd799a3e38c6123e050a35c4d8d7b3623cb7ca9453b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.9

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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