文章目录
问题表现
在分页查询时报错:
Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Result window is too large, from + size must be less than or equal to: [10000] but was [18660]. 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.]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Result window is too large, from + size must be less than or equal to: [10000] but was [18660]. 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.]];
- 原因:
这个错误是由于 Elasticsearch 的默认分页限制导致的。Elasticsearch 默认只允许查询前 10,000 条记录(from + size <= 10000),而查询请求超出了这个限制(请求了 18,660 条)。
解决方式一:临时调整Elasticsearch索引设置
通过Kibana临时调整Elasticsearch索引设置的步骤如下:
步骤1:打开Kibana Dev Tools
- 登录Kibana管理界面(通常为
http://localhost:5601
)。 - 点击左侧导航栏中的 Dev Tools(扳手图标)。
步骤2:执行索引设置更新请求
在Dev Tools的控制台中,执行以下请求(将 your_index_name
替换为实际索引名):
PUT your_index_name/_settings
{
"index.max_result_window": 20000 // 临时提高分页上限到20,000
}
执行方式:
- 复制上述代码到左侧编辑器。
- 点击绿色的 ▶ 执行 按钮(或按
Shift + Enter
)。
步骤3:验证设置是否生效
执行以下请求查看索引当前设置:
GET your_index_name/_settings/index.max_result_window
预期响应:
{
"your_index_name": {
"settings": {
"index": {
"max_result_window": "20000"
}
}
}
}
解决方式二:使用 Scroll API 或 Search After,而不是普通分页
推荐方案:使用 Scroll API 或 Search After:
Scroll API:适用于需要一次性获取大量数据的场景(如导出)。
Search After:适用于实时分页场景,性能更好。