调用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. 全面了解SQL

    很多程序员认为SQL是一头难以驯服的野兽.它是为数不多的声明性语言之一,也因为这样,其展示了完全不同于其他的表现形式.命令式语言. 面向对象语言甚至函数式编程语言(虽然有些人觉得SQL 还是有些类似功 ...

  2. Solr相似度算法一:DefaultSimilarity(基于TF-IDF的默认相似度算法)

    默认的similarity是基于TF/IDF 模块. 该 similarity有以下配置选项: discount_overlaps –确定是否重叠的标识(标记位置增量为0)都将被忽略在正常计算的时候. ...

  3. Linux tomcat 添加开机启动

    准备工作:将 jdk-7u80-linux-x64.tar.gz 解压到到 /usr/local/目录下将 apache-tomcat-7.0.82.zip 解压到/opt/etcoud目录下,并切换 ...

  4. nginx 托管.net core的service文件

    在 /etc/systemd/system/ 中新建一个服务文件site1.service vim /etc/systemd/system/site1.service [Unit] Descripti ...

  5. Mybatis 优化:

    Mybatis 的优化: ** 第一个 对于数据库配置的优化: 创建一个 DB.properties 的文件 里面编写Key = value 形式的数据库信息 比如: driver = com.mys ...

  6. CF1109DSasha and Interesting Fact from Graph Theory(数数)

    题面 传送门 前置芝士 Prufer codes与Generalized Cayley's Formula 题解 不行了脑子已经咕咕了连这么简单的数数题都不会了-- 首先这两个特殊点到底是啥并没有影响 ...

  7. delphi 10.2 ----简单的递归函数例子求和

    unit Unit10; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  8. Linux中的任务调度

    1.crond,linux中的任务调度器 crond的概念和crontab是不可分割的.crontab是一个命令,常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入 ...

  9. Luogu P1342 请柬 题解

    差不多是Dijkstra的裸题吧... 这道题可以分为来回两个阶段. 去的时候很简单,直接用一次Dijkstra,然后统计答案. 回来的时候就有些巧妙了,虽然表面上是每个点回到起点,但是何尝不可将其看 ...

  10. IDEA 笔记汇总

    Intellij IDEA 像eclipse那样给maven添加依赖 Intellij idea maven 引用无法搜索远程仓库的解决方案 Intellij IDEA 封装Jar包(提示错误: 找不 ...