Python elasticsearch.helpers.scan示例 [英] Python elasticsearch.helpers.scan example

查看:527
本文介绍了Python elasticsearch.helpers.scan示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供python elasticsearch helpers客户端的扫描API示例吗?

Can someone provide scan API example of python elasticsearch helpers client?

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

我如何从带有res对象的elasticsearch中获得所有结果?

How can i get all results from elasticsearch with res object?

推荐答案

文档包含一个示例,尽管如果我没看错,默认情况下 helpers.scan 会设置 search_type = scan ,已在ES中 5.1 。这将导致示例代码失败,ES返回 [scan] 没有搜索类型。我们可以使用 preserve_order = True 对此进行修改(但是我不确定此处的性能含义):

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. This causes the example code to fail with ES returning No search type for [scan]. We can amend this with preserve_order=True (I am however not sure about the performance implications here):

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 的格式为

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

这篇关于Python elasticsearch.helpers.scan示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆