调用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查询来说,更是雪上加霜了。