用于Elasticsearch的Python客户端
elasticsearch7的Python项目详细描述
Elasticsearch的官方低级别客户端。它的目标是提供 Python中所有与Elasticsearch相关的代码的基础;因此它尝试 没有意见,而且很容易扩展。在
安装
使用pip安装elasticsearch包:
$ python -m pip install elasticsearch
如果应用程序在Python中使用async/await,则可以使用 额外的async:
^{pr2}$阅读有关how to use asyncio with this project的更多信息。在
兼容性
该库与0.90.x以来的所有Elasticsearch版本兼容,但您 必须使用匹配的主版本:
对于Elasticsearch 7.0及更高版本,请使用 图书馆。在
对于Elasticsearch 6.0及更高版本,请使用 图书馆。在
对于Elasticsearch 5.0及更高版本,请使用 图书馆。在
对于Elasticsearch 2.0及更高版本,请使用 图书馆等等。在
在您的设置.py或 <引用>要求.txt是:
# Elasticsearch 7.x elasticsearch>=7.0.0,<8.0.0 # Elasticsearch 6.x elasticsearch>=6.0.0,<7.0.0 # Elasticsearch 5.x elasticsearch>=5.0.0,<6.0.0 # Elasticsearch 2.x elasticsearch>=2.0.0,<3.0.0
如果您需要同时安装多个版本 版本也发布为elasticsearch2和elasticsearch5。在
示例使用
简单用例:
>>> from datetime import datetime >>> from elasticsearch import Elasticsearch # by default we connect to localhost:9200 >>> es = Elasticsearch() # create an index in elasticsearch, ignore status code 400 (index already exists) >>> es.indices.create(index='my-index', ignore=400) {'acknowledged': True, 'shards_acknowledged': True, 'index': 'my-index'} # datetimes will be serialized >>> es.index(index="my-index", id=42, body={"any": "data", "timestamp": datetime.now()}) {'_index': 'my-index', '_type': '_doc', '_id': '42', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1} # but not deserialized >>> es.get(index="my-index", id=42)['_source'] {'any': 'data', 'timestamp': '2019-05-17T17:28:10.329598'}
弹性云(和SSL)用例:
>>> from elasticsearch import Elasticsearch >>> es = Elasticsearch(cloud_id="<some_long_cloud_id>", http_auth=('elastic','yourpassword')) >>> es.info()
在自签名证书用例中使用SSL上下文:
>>> from elasticsearch import Elasticsearch >>> from ssl import create_default_context >>> context = create_default_context(cafile="path/to/cafile.pem") >>> es = Elasticsearch("https://elasticsearch.url:port", ssl_context=context, http_auth=('elastic','yourpassword')) >>> es.info()
特点
客户的特点包括:
- translating basic Python data types to and from json (datetimes are not decoded for performance reasons)
- configurable automatic discovery of cluster nodes
- persistent connections
- load balancing (with pluggable selection strategy) across all available nodes
- failed connection penalization (time based - failed connections won’t be retried until a timeout is reached)
- support for ssl and http authentication
- thread safety
- pluggable architecture
Elasticsearch DSL
对于范围更有限的更高级别客户端库,请查看 elasticsearch-dsl-一个更像Python的图书馆 elasticsearch-py。在
elasticsearch-dsl提供了一种更方便和惯用的编写和操作方法 queries通过镜像Elasticsearch JSON DSL的术语和结构 同时从Python公开DSL的整个范围 直接使用定义的类或类似queryset的表达式。在
它还提供了一个可选的persistence layer,用于将文档作为 类似ORM的Python对象:定义映射、检索和保存 文档,将文档数据包装在用户定义的类中。在
许可证
版权所有2020 Elasticsearch B.V
根据Apache许可证2.0版(“许可证”)授权; 除非符合许可证,否则您不能使用此文件。 您可以在
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,软件 根据许可证分发的是“按原样”分发的, 没有任何明示或暗示的保证或条件。 有关管理权限和的特定语言,请参阅许可证 根据许可证的限制。在
- 项目
标签: