本文共 4050 字,大约阅读时间需要 13 分钟。
esengine – The Elasticsearch Object Doctype Mapper
esengine - 电子拉斯蒂克小号 Ø操作搜索bject d octype 中号冲击片雷管
ESEngine is an ODM (Object Doctype Mapper) heavily inspired by MongoEngine, developed with the idea that you have to “Know well your Elastic queries and then write them as Python objects“
ESEngine是深受MongoEngine启发的ODM(对象文档类型映射器),其开发思想是:“必须熟悉弹性查询,然后将其编写为Python对象 ”
You extend the esengine.Document class defining a bunch of fields and meta-attributes and you can use that model to instantiate documents and perform queries on ElasticSearch.
您可以扩展esengine.Document类,以定义一堆字段和元属性,然后可以使用该模型实例化文档并在ElasticSearch上执行查询 。
ESEngine is MIT licensed and is open source available at
ESEngine已获得MIT许可,可从获得开放源代码
The documentation is currently only a README full of examples in and also the DocString that can be read using Epydoc in
该文档目前仅是包含示例的自述文件,也是可以在使用Epydoc读取的DocString。
Firstly you need an Elasticsearch Python Client, we recommend using the official one pip install elasticsearch
, and then you can define your models using ESEngine objects.
首先,您需要一个Elasticsearch Python客户端,我们建议使用官方的一个pip install elasticsearch
,然后您可以使用ESEngine对象定义模型 。
# myproject/models.pyfrom elasticsearch import Elasticsearchfrom esengine import Document, StringField, BooleanFieldclass Person(Document): # meta attributes _index = 'myproject' _doctype = 'person' # default client instance _es = Elasticsearch() # optional, can be passed lazily or can be a callable # field definitions name = StringField() active = BooleanField()Person.init()# myproject/models.pyfrom elasticsearch import Elasticsearchfrom esengine import Document, StringField, BooleanFieldclass Person(Document): # meta attributes _index = 'myproject' _doctype = 'person' # default client instance _es = Elasticsearch() # optional, can be passed lazily or can be a callable # field definitions name = StringField() active = BooleanField()Person.init()
NOTE: The init() calling will initialize the index/doctype mappings and settings, this part can be omitted and then Elastic Search will try to create this by introspection when the first document is indexed.
注意: init()调用将初始化索引/文档类型的映射和设置,可以省略此部分,然后在对第一个文档建立索引时,Elastic Search将尝试通过自省来创建此部分。
With the model definition in a file like myproject/models.py
we can now use the model class Person to Index(insert), edit, delete and of course search documents.
有了myproject/models.py
这样的文件中的模型定义,我们现在可以使用模型类Person进行索引(插入),编辑,删除以及搜索文档。
In a Python console:
在Python控制台中:
Indexing:
索引:
>>> user = Person(name=”Bruno”, active=True)>>> user.save()# or simply>>> user = Person.create(name=”Bruno”, active=True)>>> user = Person(name=”Bruno”, active=True)>>> user.save()# or simply>>> user = Person.create(name=”Bruno”, active=True)
Updating
更新中
Filtering multiple documents
过滤多个文件
>>> users = Person.filter(active=True)[ ResultSet generator… a list of active users ]>>> users = Person.filter(active=True)[ ResultSet generator… a list of active users ]
Bulk update
批量更新
Performing raw queries (recommended)
执行原始查询(推荐)
>>> query = {“query”: {“match_all”: {}}, “sort”: “name”} >>> Person.search(query=query, size=10)>>> query = {“query”: {“match_all”: {}}, “sort”: “name”} >>> Person.search(query=query, size=10)
Querying using Payload helpers (better to create dynamic queries)
使用有效载荷助手进行查询(最好创建动态查询)
Deleting documents
删除文件
>>> user = Person.get(id=123)>>> user.delete()# or simply>>> Person.delete_by_id(123)# or in bulk>>> users = Person.filter(active=False)>>> Person.delete_all(users)# ou simply>>> Person.delete_by_query({“query”: …. })>>> user = Person.get(id=123)>>> user.delete()# or simply>>> Person.delete_by_id(123)# or in bulk>>> users = Person.filter(active=False)>>> Person.delete_all(users)# ou simply>>> Person.delete_by_query({“query”: …. })
You can find more examples in
您可以在找到更多示例
Currently ESEngine is being used in 3 of production projects and is reaching a nice level of performance and abstraction.
目前,ESEngine正在Catholabs的3 生产项目中使用,并且达到了不错的性能和抽象水平。
翻译自:
转载地址:http://bgqwd.baihongyu.com/