简介

​elasticsearch-py​​是官方的底层ES客户端,目标是用Python支持ES相关接口,尽量做到易用和高可扩展。

安装

兼容性

# 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
pip install elasticsearch5 # 安装对应版本的模块

python -m pip install elasticsearch

使用

from elasticsearch6 import Elasticsearch

es = Elasticsearch(
"https://127.0.0.1:9200",
http_auth=('user','passwd'),
verify_certs=False
)

query = {
'query': {
"range": {
"id": {
"gt": 100
}
}
},
"sort": {"id": {"order": "asc"}},
"size": 3
}

ret = es.search(index="index-test", doc_type='doc', body=query)

for hit in ret['hits']['hits']:
print(hit['_source']['title'])

参考

源码:
​​​https://github.com/elastic/elasticsearch-py​​​ 文档:
​https://elasticsearch-py.readthedocs.io/en/master/index.html#​