python helpers,Python elasticsearch.helpers.scan示例

本文提供了一个使用Python Elasticsearch helpers客户端进行批量扫描的例子,并解释了如何通过设置preserve_order=True来获取所有结果。代码示例展示了如何打印出每条记录的_id和_name字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

解决方案

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 is of form

{'_index': , '_type': , '_id': , '_score': , '_source': {'key': val}, 'sort': []}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值