Skip to main content

No project description provided

Project description

OpenSearch ORM

opensearch-orm is a high-level OpenSearch ORM for Python. The query syntax is similar to django-orm.

May be compatible with Elasticsearch, depending on opensearch-py.

Installation

pip install opensearch-orm

Getting Started

First, define your document model with indexing pattern.

from opensearchorm import SearchSession, BaseModel


class UserLog(BaseModel):
    __index__ = 'user_access_log-*'

    method: str
    path: str
    remote_ip: str
    created: datetime

You can use django-like syntax or typed query expressions together.

filter

# {'bool': {'must_not': [], 'should': [], 'filter': [{'range': {'created': {'gte': '2022-09-01'}}}, {'match_phrase': {'remote_ip': '127.0.0.1'}}]}}        
with SearchSession() as session:
    result = (
        session.select(UserLog)
        .filter(created__gte='2022-09-01', remote_ip='127.0.0.1')
        .fetch()
    )
    print(result)

    # equals to
    result = (
        session.select(UserLog)
        .filter(Range('created', date(2022, 9, 1)), remote_ip='127.0.0.1')
        .fetch()
    )

contains

# {'bool': {'must_not': [], 'should': [], 'filter': [{'bool': {'should': [{'match_phrase': {'method': 'GET'}}, {'match_phrase': {'method': 'POST'}}], 'minimum_should_match': 1}}]}}      
with SearchSession() as session:
    result = (
        session.select(UserLog)
        .filter(method__contains=['GET', 'POST'])
        .fetch()
    )
    print(result)

    # equals to
    result = (
        session.select(UserLog)
        .filter(Contains('method', ['GET', 'POST']))
        .fetch()
    )

    # single value
    result = (
        session.select(UserLog)
        .filter(method__contains='GET')
        .fetch()
    )
    print(result)

exclude

# {'bool': {'must_not': [{'match_phrase': {'method': 'get'}}, {'match_phrase': {'path': '/login'}}], 'should': [], 'filter': []}}
with SearchSession() as session:
    result = (
        session.select(UserLog)
        .exclude(method='get', path='/login')
        .fetch()
    )
    print(result)

paginate

with SearchSession() as session:
    result = (
        session.select(UserLog)
        .filter(method='get')
        .limit(100)
        .offset(100)
        .fetch()
    )
    print(result)

aggregations

group by path and count unique remote_ip.

with SearchSession() as session:
    # aggregate text field need use a keyword field instead
    # request_timeout argument will be passed on to the opensearch-py
    result = (
        session.select(UserLog)
        .aggregate(Terms('path.keyword').nested(Cardinality('remote_ip,keyword')), request_timeout=300)
    )
    print(result)
    # result -> {'path': 1, 'path2': 2}

scroll

with SearchSession() as session:
    start = datetime(2023, 4, 15, 19, tzinfo=TZ)
    end = datetime(2023, 4, 15, 21, tzinfo=TZ)

    scroll = (
        session.select(Model)
        .filter(
            created__gte=start,
            created__lte=end,
        )
        .limit(10000)
        .scroll('1m')
    )
    for records in scroll:
        print('-------scroll', len(records))

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

opensearch_orm-0.2.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opensearch_orm-0.2.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file opensearch_orm-0.2.1.tar.gz.

File metadata

  • Download URL: opensearch_orm-0.2.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/22.3.0

File hashes

Hashes for opensearch_orm-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5b27b0a02752b8830303eecb8433af1293fc224092345b085f5e6d01429e888f
MD5 4aa4edc5bfad79d07c5d9a55da957221
BLAKE2b-256 d32e73d8bf667684714fead60154cd7e65c11252e86c017f94d3b03e72d249bd

See more details on using hashes here.

File details

Details for the file opensearch_orm-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: opensearch_orm-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Darwin/22.3.0

File hashes

Hashes for opensearch_orm-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4edaea1b707f8db7937964811acbeb6798858b31b3b6d873216edc71acc9da70
MD5 37f0102ab01928e8c1400f6d18962a18
BLAKE2b-256 5f02ca246cf8d5493ed3689bd9c085f2ea5e84917439d815c52784960187d60d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page