有没有人能提供Python Elasticsearch helpers客户端的扫描API示例?
res = elasticsearch.helpers.scan(....)
如何使用 res 对象从 Elasticsearch 获取所有结果?
有没有人能提供Python Elasticsearch helpers客户端的扫描API示例?
res = elasticsearch.helpers.scan(....)
如何使用 res 对象从 Elasticsearch 获取所有结果?
文档中包含一个示例,不过如果我理解正确的话,默认情况下helpers.scan
会设置search_type=scan
,这在ES 5.1中被移除了。这会导致示例代码失败,并返回No 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>]}