【ElasticSearch】ES分页查询超过10000限制解决

背景

之前分页查询有接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 条无法查询的问题

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Elasticsearch 中使用深度分页功能需要注意以下几点: 1. 尽量避免使用深度分页功能,因为它会增加网络和计算开销,可能导致性能问题。 2. 深度分页功能是通过设置 from 和 size 参数来实现的。from 参数表示从哪个位置开始查询,size 参数表示每页返回的文档数量。 3. Elasticsearch 默认最多只能返回 10000 条记录,如果需要查询更多的记录,需要设置 index.max_result_window 参数。但是设置太大会占用过多的内存,影响性能。 下面是一个 Java 实现 Elasticsearch 分页查询的示例代码: ``` import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Client; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; public class ESQuery { private Client client; public ESQuery(Client client) { this.client = client; } public void search(String index, String type, int from, int size) { SearchResponse response = client.prepareSearch(index) .setTypes(type) .setQuery(QueryBuilders.matchAllQuery()) .addSort(SortBuilders.fieldSort("_id").order(SortOrder.DESC)) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setFrom(from) .setSize(size) .execute() .actionGet(); SearchHits hits = response.getHits(); for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); } } } ``` 调用示例: ``` ESQuery esQuery = new ESQuery(client); esQuery.search("my_index", "my_type", 0, 10); // 查询第一页,每页10条记录 esQuery.search("my_index", "my_type", 10, 10); // 查询第二页,每页10条记录,从第11条记录开始 ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值