Java代码解决ElasticSearch的Result window is too large

调用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);

1 if (agg != null && !agg.isEmpty()) { 2 for (int i = 0; i < agg.size(); i++) { 3 searchRequestBuilder.addAggregation(agg.get(i)); 4 } 5 } 6 updateIndexs(client, logIndex, page, size); 7 8 SearchResponse searchResponse = searchRequestBuilder 9 .setQuery(query) 10 .setFrom(page \* size) 11 .setSize(size) 12 .get(); 13 return searchResponse; 14} 15 16//更新索引的max\_result\_window参数 17private boolean updateIndexs(TransportClient client, String indices, int from, int size) { 18 int records = from \* size + size; 19 if (records <= 10000) return true; 20 UpdateSettingsResponse indexResponse = client.admin().indices() 21 .prepareUpdateSettings(indices) 22 .setSettings(Settings.builder() 23 .put("index.max\_result\_window", records) 24 .build() 25 ).get(); 26 return indexResponse.isAcknowledged(); 27}

搞定。

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

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

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

Java代码解决ElasticSearch的Result window is too large - HelloWorld