有人可以提供 python elasticsearch helpers 客户端的扫描 API 示例吗?
res = elasticsearch.helpers.scan(....)
如何使用 res 对象获取 elasticsearch 的所有结果?
The 文档 https://elasticsearch-py.readthedocs.io/en/6.0.0/helpers.html#scan包括一个例子,但如果我没读错的话,helpers.scan
默认设置search_type=scan
,这是ES 5.1 中删除 https://www.elastic.co/guide/en/elasticsearch/reference/5.1/breaking_50_search_changes.html#_literal_search_type_scan_literal_removed。这会导致示例代码失败并返回 ESNo search type for [scan]
。我们可以修改这个preserve_order=True
(但是我不确定这里的性能影响):
import elasticsearch
import elasticsearch.helpers
es = elasticsearch.Elasticsearch()
results = elasticsearch.helpers.scan(es,
index="test_index",
doc_type="my_document",
preserve_order=True,
query={"query": {"match_all": {}}},
)
for item in results:
print(item['_id'], item['_source']['name'])
该帮助器返回一个对象,您可以迭代该对象以从查询中获取实际结果。
item
是有形式的
{'_index': <str>, '_type': <str>, '_id': <str>, '_score': <float or None>, '_source': {'key': val}, 'sort': [<int>]}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)