Python elasticsearch.helpers.scan 例子
Python elasticsearch.helpers.scan example
有人可以提供 python elasticsearch helpers 客户端的扫描 API 示例吗?
res = elasticsearch.helpers.scan(....)
如何使用 res 对象从 elasticsearch 中获取所有结果?
documentation includes an example, although if I'm reading it right, helpers.scan
by default sets search_type=scan
, which was removed in ES 5.1。这会导致示例代码失败,ES 返回 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'])
此助手 returns 一个对象,您可以迭代该对象以从查询中获取实际结果。
item
的形式是
{'_index': <str>, '_type': <str>, '_id': <str>, '_score': <float or None>, '_source': {'key': val}, 'sort': [<int>]}
有人可以提供 python elasticsearch helpers 客户端的扫描 API 示例吗?
res = elasticsearch.helpers.scan(....)
如何使用 res 对象从 elasticsearch 中获取所有结果?
documentation includes an example, although if I'm reading it right, helpers.scan
by default sets search_type=scan
, which was removed in ES 5.1。这会导致示例代码失败,ES 返回 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'])
此助手 returns 一个对象,您可以迭代该对象以从查询中获取实际结果。
item
的形式是
{'_index': <str>, '_type': <str>, '_id': <str>, '_score': <float or None>, '_source': {'key': val}, 'sort': [<int>]}