python - Elasticsearch 滚动结束不返回任何内容

标签 python api elasticsearch scroll elasticsearch-api

我正在使用适用于 Python 的 Elasticsearch 6.1 API,并尝试从数据库中的每个文档(303 958 个文档)中读取特定值。

doc = {
    'size' : 1000,
    'query' : {
        'match_all' : {}
    }
}

samplesCount = 0

res = es.search(index="index", doc_type='data', body=doc, scroll='1m')
scrollId = res['_scroll_id']

scrollSize = res['hits']['total']

while scrollSize > 0 :
    for x in range (0, len(res['hits']['hits']) - 1) :
        name = res['hits']['hits'][x]['_source']['name']
        samplesCount += 1
        print(str(samplesCount) + '. ' + name)
        scrollSize -= 1

    res = es.scroll(scroll_id=scrollId, scroll='1m')

索引 (samplesCount) 以 303 654 结束,并且 es.scroll 似乎没有返回剩余文档的结果(大约 300,小于滚动大小)。

同样让我好奇的是它以 303 654 结尾......我希望是一个整数(1000 的倍数)。

有什么想法吗?

非常感谢您提供任何有用的提示。

最佳答案

尝试更换

range (0, len(res['hits']['hits']) - 1) 

range(0, len(res['hits']['hits']))

或(等效)

range(len(res['hits']['hits']))

从语法和引用的数字来看,while 循环的每次迭代似乎都跳过了 1 条记录。

关于python - Elasticsearch 滚动结束不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48389719/

相关文章:

python-3.x - ElasticSearch查询传递python动态变量

python - 将参数传递给 side_effect 函数以在 unittest.mock 中打补丁

python - 创建临时 keras session ?

c# - Windows Azure 服务管理 API 交换问题

go - 如何使用 `go-elasticsearch` 库在 Elasticsearch 中创建索引?

elasticsearch - 更新 elasticsearch 时返回文档

python - 如何将 Python Pandas Dataframe 转换为 R data.frame

python - 在 Celery 中如何更新主任务的状态直到他的所有子任务完成?

python - 使用 CSV 中的数据迭代 URL 以进行 API 数据拉取 - Python

ruby-on-rails - 如何在我的 Rails 应用程序中与 Amazon Product Advertising API 交互?