【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

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

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,在不影响性能下同时也满足我们的需要。

相关推荐
忍冬行者11 小时前
Elasticsearch 超大日志流量集群搭建(网关 + 独立 Master + 独立 Data 纯生产架构,角色完全分离,百万级日志吞吐)
大数据·elasticsearch·云原生·架构·云计算
·云扬·16 小时前
使用Prometheus+Grafana实现Elasticsearch监控的完整实践
elasticsearch·grafana·prometheus
阿杰 AJie18 小时前
Git 分支与多人开发使用指南(Gitee + 本地 Git)
git·elasticsearch·gitee
海鸥8118 小时前
ArgoCD App of Apps 模式详解
java·elasticsearch·argocd
Dxy123931021619 小时前
深度解析 Elasticsearch 8.13.4 范围查询 DSL
大数据·elasticsearch
Zilliz Planet19 小时前
官宣 | Milvus 2.6云上GA:三层存储降本85% 、速度快ES 4-7 倍,多数据类型支持
大数据·elasticsearch·搜索引擎·全文检索·milvus
峥嵘life20 小时前
Android16 系统每月Google安全补丁导入说明
大数据·elasticsearch·搜索引擎
Dxy123931021620 小时前
Elasticsearch 8.13.4 地理范围查询实战指南
大数据·elasticsearch·jenkins
天草二十六_简村人21 小时前
ES索引检索课程名称时,同时支持模糊搜索和精准搜索
大数据·后端·elasticsearch·搜索引擎·全文检索
铭毅天下1 天前
从软考开始考 Elasticsearch 说开去 ......
大数据·elasticsearch·搜索引擎·全文检索