es查询限制10000,使用scroll

报错:

TransportError(500, 'search_phase_execution_exception', 'Result window is too large, from + size must be less than or equal to: 10000 but was 10010. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the index.max_result_window index level setting.')

原因:

这个错误提示意味着你正在尝试从Elasticsearch中检索大量的数据,但是你的请求超过了Elasticsearch的默认结果窗口大小限制。默认情况下,Elasticsearch的结果窗口大小限制为10000,这意味着你一次最多只能检索10000个文档。

为了解决这个问题,你可以使用Elasticsearch的scroll API来检索大量的数据。scroll

API允许你在多个请求之间保持搜索上下文,以便你可以逐步检索大量的数据,而不会超出默认结果窗口大小的限制。

另外,你也可以通过更改Elasticsearch的索引级别设置来增加结果窗口大小限制。你可以通过设置"index.max_result_window"参数来调整结果窗口大小限制。但是,这种方法并不推荐,因为它可能会导致Elasticsearch性能下降,特别是在处理大量数据时。

总之,建议你使用scroll API来检索大量的数据,以避免超出默认结果窗口大小限制。

原代码:

python 复制代码
def main():
for i in range(0, int(allapge(index) / 10) + 2):
	try:
        res = es_yt.search(index=index, body=search_body(i))
        list_es = res['hits']['hits']
        s = helpers.bulk(es_ebscn, list_es)
        print('已完成', i)
        print(s)
    except Exception as e:
        print(e)

使用scroll 新代码:

python 复制代码
def main():
    scroll_time = '2m'  # 滚动查询的保持时间
    page_size = 1000  # 每个滚动请求返回的文档数

    res = es_yt.search(index=index, body=search_body(0), scroll=scroll_time, size=page_size)
    scroll_id = res['_scroll_id']
    scroll_size = res['hits']['total']
    print('scroll_size', scroll_size)

    while (scroll_size > 0):

        list_es = res['hits']['hits']
        print(list_es[0:1])
        if list_es:
            s = helpers.bulk(es_ebscn, list_es)
            print('scroll_size', scroll_size)
            print(s)
            res = es_yt.scroll(scroll_id=scroll_id, scroll=scroll_time)
        else:
            print('结束')
            break

    es_yt.clear_scroll(scroll_id=scroll_id)
相关推荐
Elastic 中国社区官方博客几秒前
6个资源,1条命令:使用 Terraform 全自动化实现 Elastic 异常检测
大数据·人工智能·elasticsearch·搜索引擎·云原生·自动化·terraform
captain_AIouo5 分钟前
深耕跨境赛道!autoAGC跨境AI,挖掘海外百亿增量红利
大数据·人工智能·经验分享·aigc
曾阿伦5 分钟前
Elasticsearch Query DSL 叶子查询+复合查询指南
大数据·elasticsearch
Sharewinfo_BJ5 分钟前
当 AWS 遇上智信 BI:全球化底座 + 本土化智慧
大数据·智信bi·aws亚马逊云
Stick_ZYZ8 分钟前
从 Prompt 到 Context Engineering:Agent 真正稳定的关键
大数据·人工智能·算法·ai·prompt
数学建模导师11 分钟前
2026第八届中青杯ABC题赛题分析【配套解题思路+代码】
大数据·人工智能·数学建模
陆水A18 分钟前
【实时数仓·2】CDC到Doris数据对不上——Sequence Column解了吗?
大数据·数据仓库·数据库开发·etl工程师·bigdata
黎阳之光23 分钟前
数字孪生赋能智慧油站建设|黎阳之光全场景可视化安防管控平台落地应用
大数据·物联网·算法·安全·数字孪生
真上帝的左手23 分钟前
19. 大数据- BI 入门-数据集成全维度详解
大数据·bi
十六年开源服务商24 分钟前
2026外贸WordPress社交媒体营销运营指南
大数据·人工智能·媒体