一、问题
最近做项目,在elasticsearch中进行分页查询时,遇到这样一个问题,页码刚开始时,es正常返回结果,
可一旦页数增大,返回结果就抛异常,出错了。异常信息如下:
org.elasticsearch.search.query.QueryPhaseExecutionException: Result window is too large, from + size must be less than or equal to: [10000] but was [31350]. 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 parameter.
二、问题分析
仔细阅读异常信息,其实已经说的很明白。
es的index索引中,查询时,默认结果集最大长度为10000,超过则会抛异常。
这和mysql查询类似,分页查询时,其实获取的是查询条件的所有数据,然后根据页码,忽略指定页码之前的数据,
只返回指定页码后的指定大小的数据集。
因此即只查询几条数据,只要页码过大,查询时的初始结果集依然很大,也就抛错了。
三、问题解决
解决方案异常信息其实也说的很明白。修改es指定index索引的结果集大小。这里以将结果集大小多10000
调整为500000,使用http api修改为例,如下:
curl -XPUT "$host:$port/test_index/_settings" -d '{ "index" : { "max_result_window" : 500000 } }'
查看修改结果:
curl -XGET "$host:$port/test_index/_settings?pretty"
此到,问题解决。