【ES】--track_total_hits参数影响ES分页数据

一、前言

工作遇到一个ES深度分页查询时出现报错,报错内容如下
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 [10001].This limit can be set by changing the [index.max_result_window] index level setting.
出现这个问题的原因是:
ES为了避免用户的过大分页请求造成ES服务所在机器内存溢出,默认对深度分页的条数进行了限制,默认的最大条数是10000条

二、解决方法

2.1、修改max_result_window参数

修改es的默认窗口大小 — index.max_result_window
或者代码中动态修改参数,如下

  //更新索引的max_result_window参数
    private boolean updateIndexs(TransportClient client, String indices, int records, int size) {
        int records = from * size + size;
        if (records <= 10000) return true;
        UpdateSettingsResponse indexResponse = client.admin().indices()
                .prepareUpdateSettings(indices)
                .setSettings(Settings.builder()
                        .put("index.max_result_window", records)
                        .build()
                ).get();
        return indexResponse.isAcknowledged();
    }

2.2、修改track_total_hits 参数

出现上面的问题,实际是ES查询时的totalHits返回值>10000。为什么会出现这种情况呢?Elasticsearch 的 track_total_hits 参数是指追踪查询结果的总命中数。默认情况下,当查询结果总命中数大于 10,000 时,Elasticsearch 将停止精确地计算命中数,以避免对集群造成过大的压力。
track_total_hits 为 true,则 Elasticsearch 将返回精确的命中数,这些命中数会大于10000,但可能会对性能产生影响。
track_total_hits 为 false,则 Elasticsearch 可能会返回近似的命中数,并且不会超过10000,但可以提高查询性能。

2.3、结论

最佳的解决方案是track_total_hits 为false,在不影响性能下同时也满足我们的需要。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DreamBoy_W.W.Y

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

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

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

打赏作者

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

抵扣说明:

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

余额充值