背景

之前分页查询有接ES,随着系统使用时间数据量不断增加,会有如下报错。关键信息就是 Result window is too large, from + size must be less than or equal to: [10000] but was [100001]

其实就是,查询的数量超过了index.max_result_window的限制值10000,也就是说只能查询前10000条数据。

org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]

Caused by: org.elasticsearch.client.ResponseException: method [POST], host [http://**.***.**.***:****], URI [/****/****/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 500 Internal Server Error]
{"error":{"root_cause":[{"type":"query_phase_execution_exception","reason":"Result window is too large, from + size must be less than or equal to: [10000] but was [100001]. 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."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"dms-apply-info","node":"IZhu6TvHSh-jm0rinE0f2A","reason":{"type":"query_phase_execution_exception","reason":"Result window is too large, from + size must be less than or equal to: [10000] but was [100001]. 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."}}]},"status":500}

解决

得知这个情况,我是很震惊的,因为对ES还没有全面学习(也可以说一点都没有 bushi)。于是和一位大佬发生了如下对话(自古红蓝出CP😂

富贵儿:Result window is too large, from + size must be less than or equal to: [10000] but was [100001]

       这个是指总数据量超过10000吗?

大佬:分页只能10000

富贵儿:我看pageSize是10 耶

大佬:pageIndex * pageSize <=10000

富贵儿:那这个也查不了多少数据呀

大佬:是啊,只能查前10000条

富贵儿:认真的?

大佬:我们就这么干的

大佬:

富贵儿:真秀啊

大佬:没人反馈不够用啊

富贵儿:你们没有超过10000的分页需求?

富贵儿:这很魔幻

大佬:超过10000需要用scroll 滚动查询
大佬:不能用 from + size

富贵儿:拼装查询参数这里,要改一下是吧

大佬:emmmm完全不一样的

富贵儿:emmmm因为我不了解哦,先确认下,就是我们这里调用要改。但是ES是支持的,这个意思?

大佬:不支持,只能通过一页一页滚动去看

富贵儿:也就是说,目前使用ES查询,没办法查10000以后的数据,我可以这么理解不?分页查询的话,不支持查超过10000的

大佬:嗯

富贵儿:感谢解答,这个分页查询,好鸡肋的感觉

大佬:合理 没必要查10000以后的

富贵儿:为啥,我这边现在就遇到了

大佬:没人会去翻 10000以后啊

富贵儿:刚刚就有人查了,线上报错了,才发现了这个问题

大佬:没事的,做一个提示吧

富贵儿:真的秀

过了半个小时.......(我在疯狂百度

富贵儿:看起来好像可以单独设置某个索引的最大数量限制

大佬:这个操作比较吃性能

富贵儿:哦酱造,那不乱搞了

大佬:嗯

富贵儿:给大佬递茶

屈服于大佬,增加了限制,over...... 

        Integer queryTotal = pageQuery.getPageIndex() * pageQuery.getPageSize();
        if (queryTotal > 10000) {
            throw new BusinessException(String.format("只能查询前[%s]条数据, 建议缩小查询范围", 10000));
        }

参考

解决 Elasticsearch 超过 10000 条无法查询的问题

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐