解决ES分页异常:Result window is too large, from + size must be less than or equal to: [10000]

问题表现

在分页查询时报错:

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

  1. 登录Kibana管理界面(通常为 http://localhost:5601)。
  2. 点击左侧导航栏中的 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:适用于实时分页场景,性能更好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TracyCoder123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值