博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ESEngine –适用于Python的Elasticsearch对象文档类型映射器
阅读量:2518 次
发布时间:2019-05-11

本文共 4050 字,大约阅读时间需要 13 分钟。

什么是ESEngine (What is ESEngine)

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。

这个怎么运作? (How it works?)

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/

你可能感兴趣的文章
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
学习进度
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>