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

相关推荐
Java 码思客10 小时前
【ElasticSearch从入门到架构师】第5章:ES DSL 检索语法精讲(核心重点)
大数据·elasticsearch
2601_9618752415 小时前
法考资料电子版|pdf|资料已整理
elasticsearch·搜索引擎·pdf·全文检索·solr·lucene·sphinx
MemoriKu17 小时前
Flutter 相册 APP 收尾优化实战:未分析任务横幅持久隐藏与标签回归测试补强
大数据·人工智能·flutter·elasticsearch·机器学习·搜索引擎·重构
JAVA面经实录91718 小时前
Elasticsearch 完整版完整知识体系
java·elasticsearch·搜索引擎·es
Upsy-Daisy18 小时前
Hermes Agent 学习笔记 06:Skills 系统,Agent 如何把经验沉淀为可复用能力?
大数据·elasticsearch·搜索引擎
代码讲故事19 小时前
在没有kibana的ES(elasticsearch)线上生产环境集群中,如何通过命令行修改或增加字段而不需要reindex?
大数据·elasticsearch·搜索引擎·命令行·es·索引·模版
Java 码思客1 天前
【ElasticSearch从入门到架构师】第3章:ES 核心基础概念(架构师必备底层认知)
大数据·elasticsearch·jenkins
徐寿春2 天前
什么是 Elasticsearch 倒排索引
大数据·elasticsearch·搜索引擎
Java 码思客3 天前
【ElasticSearch从入门到架构师】第1章:ElasticSearch 核心认知与行业定位
大数据·elasticsearch·搜索引擎
serve the people3 天前
Elasticsearch(5) i want to monitor the es health from a http api
大数据·http·elasticsearch