Skip to main content

A simple, expressive elasticsearch orm

Project description


A simple and expressive (like peewee) sql-like orm for elasticsearch.


  • Defining index model in a Django way:
from sqlorm4es import *

class LogCenter(BaseModel):
    __index__ = 'lala'
    __database__ = {
        "host": ""

    ok = Boolean(default=True)
    lineno = Integer(required=True)
    message = Text(default='xixi')
    timestamp = Date(name="@timestamp", timezone="+8")
    bigbrother = Object(
  • Search elasticsearch in a expressive way:
res =, LogCenter.lineno, 'max(lineno)') \
    .where(((LogCenter.ok == False) & (LogCenter.message != 'error')) |
           (LogCenter.timestamp >= '2017-10-12') |
           (LogCenter.bigbrother.head >> ' ( tough; soft, medium soft ) '))\
  • Note that and(&)/or(|) expression in where can be nested
  • Also support group by aggregation: MAX, MIN, AVG, SUM, COUNT...
  • Also support order by, LIMIT, OFFSET:
res ='max(lineno)')\
    .group_by(LogCenter.ok, LogCenter.timestamp)\
    .order_by((LogCenter.timestamp, 'asc'))\
  • Manipulate document like a normal python object, value will be validated when assigning it (eg. value in Date Field all stored as UTC):
new_log = LogCenter(ok=False, message='oops', timestamp='2019-10-10')
new_log.bigbrother = {
    "head": 'tough',
    "hand": {
        "finger": "long",
        "nail": "clean"
  • Create a single document:
  • Insert, Delete, Update and Index operation: Coming soon...


Sqlorm4es has been packaged to pypi, so just need one simple command to install it:

pip install sqlorm4es

Elasticsearch driver

Sqlorm4es implemented a simple almost lock-free connection pool based on official Elasticsearch client. As it is lock-free, so i am not sure whether it is thread-safe, but it works fine under my own multi-thread tests :)

  • Note that if you did not set config of your model, it will use the default config as below:
config = {
    'hosts': [''],
    'maxsize': 20,
    'sniff_on_start': False,
    'sniff_on_connection_fail': False,
    'sniff_timeout': .1,
    'sniffer_timeout': None,
    'retry_on_timeout': False,
    'timeout': 60,

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

sqlorm4es-1.1.5.tar.gz (19.8 kB view hashes)

Uploaded source

Supported by

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