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

目录

一、前言

工作遇到一个ES深度分页查询时出现报错,报错内容如下

ElasticsearchExceptionElasticsearch 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

或者代码中动态修改参数,如下

java 复制代码
  //更新索引的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,在不影响性能下同时也满足我们的需要。

相关推荐
互联网中的一颗神经元4 小时前
ZZ — Git 速查表
大数据·git·elasticsearch
Elasticsearch6 小时前
跳过有状态的 OTel Collector:Elasticsearch 9.5 原生存储两种指标时间类型
elasticsearch
C++、Java和Python的菜鸟15 小时前
第2章 项目前置课-代码版本控制Git
大数据·elasticsearch·搜索引擎
Elasticsearch1 天前
什么是混合搜索?
elasticsearch
Elasticsearch1 天前
从点击流中提升搜索相关性:使用 Learn To Rank 和 OpenTelemetry 行为信号
elasticsearch
倒流时光三十年1 天前
第六阶段 57 · ES|QL 与 SQL API(用 SQL 查 ES)
数据库·sql·elasticsearch
guwentian2 天前
Git Worktree 实战:用并行多 Agent 把开发提速 N 倍
大数据·git·elasticsearch·wroktree
Elastic 中国社区官方博客2 天前
Elasticsearch:使用 AI Agent 来创建 workflow
大数据·运维·人工智能·elasticsearch·搜索引擎·自动化·全文检索
阿里云大数据AI技术2 天前
AI Search+ES 9.4.X最佳实践:“更快、更准、更安全的企业级搜索引擎”"为AI Agent提供坚实底座”
人工智能·elasticsearch·agent
Elasticsearch2 天前
Elastic 社区通讯 — 2026 年 8 月
elasticsearch