调用ElasticSearch做分页查询时报错:

QueryPhaseExecutionException[Result window is too large, from + size must be less than or equal to: [10000] but was [666000]. 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.]; }

提示用from+size方式有1万条数据查询的限制,需要更改index.max_result_window参数的值。

翻了下elasticsearch官网的文档:

index.max_result_window
The maximum value of from + size for searches to this index.Defaults to 10000.
Search requests take heap memory and time proportional to from + size and this limits that memory.
See Scroll or Search After for a more efficient alternative to raising this.

说是用传统方式(from + size)查询占用内存空间且比较消耗时间,所以做了限制。

问题是用scroll方式做后台分页根本行不通。

不说用scroll方式只能一页页的翻这种不人性化的操作。页码一多,scrollId也很难管理啊。

所以继续鼓捣传统方式的分页。

上网查了下设置max_result_window的方法,全都是用crul或者http方式改的。

后来无意间看到了一篇文档: https://blog.csdn.net/tzconn/article/details/83309516

结合之前逛elastic中文社区的时候知道这个参数是索引级别的。于是小试了一下,结果竟然可以了。

java代码如下:

public SearchResponse search(String logIndex, String logType, QueryBuilder query, 
List<AggregationBuilder> agg, int page, int size) {
page = page > 0 ? page - 1 : page;
TransportClient client = getClient();
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(logIndex.split(","))
.setTypes(logType.split(","))
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.addSort("createTime", SortOrder.DESC); if (agg != null && !agg.isEmpty()) {
for (int i = 0; i < agg.size(); i++) {
searchRequestBuilder.addAggregation(agg.get(i));
}
}
updateIndexs(client, logIndex, page, size); SearchResponse searchResponse = searchRequestBuilder
.setQuery(query)
.setFrom(page * size)
.setSize(size)
.get();
return searchResponse;
} //更新索引的max_result_window参数
private boolean updateIndexs(TransportClient client, String indices, int from, int size) {
int records = from * size + size;
if (records <= 10000) return true;
UpdateSettingsResponse indexResponse = client.admin().indices()
.prepareUpdateSettings(indices)
.setSettings(Settings.builder()
.put("index.max_result_window", records)
.build()
).get();
return indexResponse.isAcknowledged();
}

搞定。

当然这段代码不好的地方在于:

每次查询超过10000万条记录的时候,都会去更新一次index。

这对原本就偏慢的from+size查询来说,更是雪上加霜了。

Java代码解决ElasticSearch的Result window is too large问题的更多相关文章

  1. elastic query match_all 数据目标超过10000条出错 Result window is too large

    起因 elastic做文本索引,match_all目标索引超过10000条时,出错 { "error": { "root_cause": [ { "t ...

  2. Result window is too large, from + size must be less than or equal to [10000]

    使用sql插件执行如下语句的时候报错http://10.127.0.1:9200/_sql?sql=select * from test limit 1000000 错误信息:{"error ...

  3. Result window is too large, from + size must be less than or equal to: [10000] but was [78440]. See the scroll api for a more efficient way to request large data sets

    {"error":{"root_cause":[{"type":"query_phase_execution_exception& ...

  4. 用java代码解决10元喝多少瓶汽水的问题

    问题:汽水2元一瓶,四个盖子换一瓶,两个空瓶一瓶,问10元可以喝几瓶?(不许借别人空瓶或瓶盖,但可以先喝汽水再付空酒瓶或瓶盖) 最近同事让笔者看了一道脑筋急转弯的数学题,当然不是很难,只要会加减法应该 ...

  5. 用java代码解决excel打开csv文件乱码问题

      Java 读取csv文件后,再保存到磁盘上,然后直接用Excel打开,你会发现里面都是乱码. 贴上代码: public class Test { public static void main(S ...

  6. Java代码操作Elasticsearch

    创建maven项目,导入依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</a ...

  7. Elasticsearch 的分页报错 result window is too large

    检查自己分页查询的代码 Pageable pageable = new PageRequest(0, 10000); searchQuery.setPageable(pageable); // 分页效 ...

  8. 【max_result_window大小】 Result window is too large的问题

    方法一: 如果需要搜索分页,可以通过from size组合来进行.from表示从第几行开始,size表示查询多少条文档.from默认为0,size默认为10, 如果搜索size大于10000,需要设置 ...

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

    解决 Elasticsearch 超过 10000 条无法查询的问题 问题描述 分页查询场景,当查询记录数超过 10000 条时,会报错. 使用 Kibana 的 Dev Tools 工具查询 从第 ...

随机推荐

  1. [LeetCode 题解]: Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  2. Postgresql 用户管理

    一, 设置超级用户密码 1  修改 pg_hba.conf 使超级用户postgres 可以登录到数据库中 host all all 127.0.0.1/32 trust 2 修改 postgres ...

  3. 打造自己的Sublime使用环境

    1.破解LICENSE(如果不是特穷还是买一个吧,不是特别贵,支持正版)----- BEGIN LICENSE -----Andrew WeberSingle User LicenseEA7E-855 ...

  4. DataTables使用总结

    一.使用方法     1.引入JS文件 <script src="js/plugin/datatables/jquery.dataTables.min.js">< ...

  5. .netcore Swagger 生成 api接口文档

    1, 引用第三方包, Swashbuckle.AspNetCore Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerUI 最简 ...

  6. Cookie客户端缓存.Session.Application

    Cookie客户端缓存. 1.引言 随着浏览器的处理能力不断增强,越来越多的网站开始考虑将数据存储在「客户端」,那么久不得不谈本地存储了. 本地存储的好处: 一是避免取回数据前页面一片空白,如果不需要 ...

  7. TestNG学习笔记目录

    学习TestNG主要用于GUI自动化测试使用,学习目录随进度不断更新.文档内容主要是翻译官方doc,同时加入自己的理解和案例.如有理解偏差欢迎指正 一.TestNG Eclipse plug-in 安 ...

  8. RabbitMq初探——消息持久化

    消息持久化 前言 通过上一节,我们知道,有消息确认机制,保证了当消费者进程挂掉后,消息的不丢失. 但是如果rabbitmq挂掉呢?它的队列和消息都会丢失的.为了保证消息在rabbitmq挂掉重启后不丢 ...

  9. 激活xmind的方法

    https://blog.csdn.net/qq_38238041/article/details/81107127 这里以windows为例来演示,其它操作系统需根据情况修改相应步骤. 下载安装包 ...

  10. 【12c OCP】CUUG OCP认证071考试原题解析(36)

    36.choose the best answer View the Exhibits and examine the structures of the PRODUCTS, SALES, and C ...