Elasticsearch 的 Python 客戶端:tina

jopen 9年前發布 | 12K 次閱讀 ElasticSearch 搜索引擎

Elasticsearch 的 Python client,提供較為方便的查詢語法。

Installation

$ git submodule add https://github.com/kelp404/tina.git
$ git submodule add https://github.com/kelp404/elasticsearch-py.git
$ pip3 install urllib3
$ pip3 install certifi
$ pip3 install ujson
</blockquote>

Django settings

# settings.py TINA_ELASTICSEARCH_URL = 'https://username:password@domain.com:9200' TINA_INDEX_PREFIX = 'prefix_' # The prefix of the index name.
</blockquote>

Document

# example:
from tina 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)</pre>

Properties

_id: {string}
_version: {int}

Methods

     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 exists(cls, id):
        """
        Is the document exists?
        :param id: {string} The documents' id.
        :return: {bool}
        """
        is_exist = SampleModel.exists('byMQ-ULRSJ291RG_eEwSfQ')

    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,
            exclude,
        ]
        :return: {tina.query.Query}
        """

    def all(cls):
        """
        The query for all documents.
        :return: {tina.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.
        """

項目主頁:http://www.baiduhome.net/lib/view/home/1441012024721

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!