elasticsearch结果窗口限制10000[from+size小于或等于10000]

/ 0条评论 / 0 个点赞 / 590人阅读

Elasicsearch版本7.8,执行DSL查询

GET /my_test_sort/_search
{
  "query": {
    "match_all": {}
  },
  "from": 1,
  "size": 10000
}

结果Elasicsearch报如下错误

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

其实Elasicsearch这个错误已经提示的很明显了,甚至提出了解决方案。大概意思是说:结果窗口太大,from+size必须小于或等于10000,但我们检索的DSL中from+size=10001。我们在查询大量数据时可以采用 scroll api 这种高效的方式。结果窗口大小可以修改索引参数:max_result_window进行设置

问题分析

其实问题已经很明了了,我们可以查看相关官网的解释进一步赘述

Elasicsearch通过index.max_result_window参数控制了能够获取的数据总数from+size的最大值,默认是10000条。但是,由于数据需要从其它节点分别上报到协调节点,因此搜索请求的数据越多,会导致在协调节点占用分配给Elasticsearch的堆内存和搜索、排序时间越大。针对这种满足条件数量较多的深度搜索,官方建议我们使用Scroll

解决方案

已经创建好的索引,可以动态的修改max_result_window的大小

PUT /my_test_sort/_settings?preserve_existing=true
{
  "max_result_window": "200000"
}

新建索引的时候可以直接指定max_result_window的大小

PUT index_name
{
  "settings": {
    "max_result_window": "200000"
  }
}

如果没有执行DSL的环境,可以直接在服务器上执行

curl -H "Content-Type: application/json" -X PUT 'http://127.0.0.1:9200/my_test_sort/_settings?preserve_existing=true' -d '{"max_result_window" : "200000"}'

max_result_window支持的最大返回数是2^31-1,也就是2147483647