Skip to content

RinseIO/asahi

Repository files navigation

#asahi circle-ci

A database library for ElasticSearch.

##Installation

$ git submodule add git@github.com:RinseIO/asahi.git $ git submodule add https://github.com/RinseIO/elasticsearch-py.git




##Document
>
```python
# example:
from asahi import db
# define your data model
class SampleModel(db.Document):
    _index = 'samples'  # You can set index name by this attribute.
    name = db.StringProperty()
    email = db.StringProperty(required=True)
    is_vip = db.BooleanProperty(default=False)
    quota = db.FloatProperty(default=0.0)
    created_at = db.DateTimeProperty(auto_now=True)

Properties

_id: {string} _version: {int}


**Methods**
>```python
def get(cls, ids, rev=None, db=None, dynamic_properties=True):
    """
    Get documents by ids.
    :param ids: {list or string} The documents' id.
    :return: {list or Document}
    """
# example:
#    Get the document by the id.
#    The result document is SampleModel's instance.
    document = SampleModel.get('byMQ-ULRSJ291RG_eEwSfQ')
#    Get the documents by ids.
#    The result documents is the list. There are SampleModels' instance in the list.
    documents = SampleModel.get([
        'byMQ-ULRSJ291RG_eEwSfQ',
        'byMQ-ULRSJ291RG_eEwSfc',
    ])
def where(cls, *args, **kwargs):
    """
    Intersect the query.
    :param args:
        The member's name of the document or
            the sub queries' lambda function.
    :param kwargs: [
        unequal,
        equal,
        less,
        less_equal,
        greater,
        greater_equal,
        like,
        unlike,
        contains,
    ]
    :return: {asahi.query.Query}
    """
def all(cls):
    """
    The query for all documents.
    :return: {asahi.query.Query}
    """
def refresh(cls):
    """
    Explicitly refresh the index, making all operations performed
    """
def save(self, synchronized=False):
    """
    Save the document.
    """
def delete(self, synchronized=False):
    """
    Delete the document.
    """

##Query

The asahi query.

Methods

def where(self, *args, **kwargs): """ Intersect the query. :param args: The member's name of the document or the sub queries' lambda function. :param kwargs: [ unequal, equal, less, less_equal, greater, greater_equal, like, unlike, contains, ] :return: {asahi.query.Query} """

```python
def order_by(self, member, descending=False):
    """
    Append the order query.
    :param member: {string} The property name of the document.
    :param descending: {bool} Is sorted by descending.
    :return: {asahi.query.Query}
    """
def fetch(self, limit=1000, skip=0, fetch_reference=True):
    """
    Fetch documents by the query.
    :param limit: {int} The size of the pagination. (The limit of the result items.)
    :param skip: {int} The offset of the pagination. (Skip x items.)
    :returns: {tuple}
        ({list}[{Document}], {int}total)
        The documents.
        The total items.
    """
def first(self, fetch_reference=True):
    """
    Fetch the first document.
    :return: {asahi.document.Document or None}
    """
def count(self):
    """
    Count documents by the query.
    :return: {int}
    """
def group_by(self, member, limit=10, descending=True):
    """
    Aggregations
    http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html
    :param member: {string} The property name of the document.
    :param limit: {int} The number of returns.
    :param descending: {bool} Is sorted by descending?
    :returns: {list}
        {list}[{dict}]
        {
            doc_count: {int},
            key: 'term'
        }
    """

##Examples

select * from "ExampleModel" where "name" = "asahi"

```python
models, total = ExampleModel.where('name', equal='asahi').fetch()

select * from "ExampleModel" where "name" = "asahi" and "email" = "asahi@rinse.io"

```python
models, total = ExampleModel.where('name', equal='asahi')\
        .where('email', equal='asahi@rinse.io')\
        .fetch()

select * from "ExampleModel" where "name" like "%asahi%" or "email" like "%asahi%"

```python
models, total = ExampleModel.where(lambda x:
    x.where('name', like='asahi')
    .union('email', like='asahi')
).fetch()

select * from "ExampleModel" where "category" = 1 or "category" = 3 order by "created_at" limit 20 offset 20

```python
models, total = ExampleModel.where('category', contains=[1, 3])\
        .order_by('created_at').fetch(20, 20)

Fetch the first item.

select * from "ExampleModel" where "age" >= 10
         order by "created_at" desc limit 1
model = ExampleModel.where('age', greater_equal=10)\
        .order_by('created_at', descending=True).first()

Count items.

select count(*) from "ExampleModel" where "age" < 10
count = ExampleModel.where('age', less=10).count()

##Properties

https://github.com/RinseIO/asahi/blob/master/asahi/properties.py

  • Property
  • StringProperty
  • IntegerProperty
  • BooleanProperty
  • FloatProperty
  • DateTimeProperty
  • ListProperty
  • DictProperty
  • ReferenceProperty

##Requirement

$ git submodule update --init $ pip3 install -r pip_requirements.txt




##unit-test
>```bash
$ python3 test.py

##Note

There are issues about ElasticSearch.
If your OS X is 10.9.3, your default Java is 1.6. ElasticSearch 1.2.0 required Java 1.7. Run ElasticSearch 1.2.0 on Java 1.6 will pop the message like this:

 Exception in thread "main" java.lang.UnsupportedClassVersionError: org/elasticsearch/bootstrap/Elasticsearch : Unsupported major.minor version 51.0
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
 at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
 at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: org.elasticsearch.bootstrap.Elasticsearch.  Program will exit.

##References