简体   繁体   English

Python elasticsearch.helpers.scan示例

[英]Python elasticsearch.helpers.scan example

Can someone provide scan API example of python elasticsearch helpers client? 有人可以提供python elasticsearch助手客户端的扫描API示例吗?

res = elasticsearch.helpers.scan(....)

How can i get all results from elasticsearch with res object? 如何从res对象获得elasticsearch的所有结果?

The 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 . 文档包含一个示例,但如果我正确读取它, helpers.scan默认情况下helpers.scan设置search_type=scan ,这已在ES 5.1删除 This causes the example code to fail with ES returning No search type for [scan] . 这会导致示例代码失败,ES返回No search type for [scan] We can amend this with preserve_order=True (I am however not sure about the performance implications here): 我们可以使用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'])

This helper returns an object which you can iterate to obtain the actual results from the query. 此帮助程序返回一个对象,您可以迭代该对象以从查询中获取实际结果。

item is of form item是形式的

{'_index': <str>, '_type': <str>, '_id': <str>, '_score': <float or None>, '_source': {'key': val}, 'sort': [<int>]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM