6.0 的 Aggregations 变更共 4 条:terms 聚合
include/exclude里的pattern子对象移除,正则直接作为值;date_range的数值to/from按目标字段的format解释,不再一律当epoch_millis;terms 聚合的global_ordinals_hash与global_ordinals_low_cardinality两个执行提示废弃,统一用global_ordinals;composite 聚合的missing废弃,改用missing_bucket。
说明
实测环境:本地起的 Elasticsearch 6.8.23 单节点实例(官方 tar 包直接跑,堆内存 512MB,机器学习关闭),curl 连本地 9200 端口。本文「实测」小节的命令和输出都出自这个实例。
来源和边界先交代:本文的依据出自 v6.8.23 标签下的官方文件(aggregations.asciidoc、IncludeExclude.java、DateRangeAggregationBuilder.java),文中引用的原文均来自这些文件。示例取自官方示例;哪些是文档写明、哪些是我的推断,正文里逐处标注。
以下均出自 6.0 迁移文档(migrate_6_0/aggregations.asciidoc @ v6.8.23),共 79 行 4 条,下面逐条给出行号。

图 1:官方文件 aggregations.asciidoc(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 aggregations.asciidoc
include/exclude 的 pattern 子对象移除
The
includeandexcludeoptions oftermsaggregations used to accept a subpatternobject which has been removed. The pattern should now be directly put as a value of theincludeandexcludefields.
旧写法(文档第 14–34 行的例子):
POST /twitter/_search?size=0
{
"aggs" : {
"top_users" : {
"terms" : {
"field" : "user",
"include": {
"pattern": "foo.*"
},
"exclude": {
"pattern": ".*bar"
}
}
}
}
}
新写法(文档第 38–54 行):
POST /twitter/_search?size=0
{
"aggs" : {
"top_users" : {
"terms" : {
"field" : "user",
"include": "foo.*",
"exclude": ".*bar"
}
}
}
}
这个字段现在只有两种形态
include/exclude 有两种用途:正则筛选和精确值集合。6.0 之后取值形态与用途一一对应:
- 字符串 → 正则;
- 字符串数组 → 精确值集合。
以前正则是「字符串外面再包一层对象」,精确值是数组,同一个字段有两种形态,解析时要先判断是对象还是数组。拍平之后判别规则简单了,解析器也少一层分支。
Java 客户端对应 IncludeExclude.java @ v6.8.23 的几个构造:正则走 IncludeExclude(String include, String exclude)(第 345 行)或 IncludeExclude(RegExp, RegExp)(第 333 行);精确值走 IncludeExclude(String[], String[])(第 365 行)或 IncludeExclude(SortedSet<BytesRef>, SortedSet<BytesRef>)(第 353 行),数值另有 double[]/long[] 两个重载(第 369、373 行);还有按分区取桶的 IncludeExclude(int partition, int numPartitions)(第 377 行)。
排查范围要超出代码仓库
这条的麻烦在于写法的散落程度:include/exclude 是 terms 聚合的常用参数,「只要某类前缀的值」「排除测试脏数据」这类需求到处都是。改法本身很小(去掉一层包装),但查询模板、Kibana 保存的可视化与看板、Watcher/告警规则、报表系统里的查询定义都可能带着旧写法,而后面这些不在代码仓库里,grep 代码找不到。
# 代码与配置里的 pattern 子对象写法
grep -rn '"pattern"' --include=*.json --include=*.java --include=*.py . | head -20
集群侧存的对象(stored script、搜索模板)可以这样扫:
curl -s 'localhost:9200/_scripts/_all' | grep -c 'pattern'
Kibana 的 saved objects 要去 Kibana 侧查,或者导出后 grep——这一步不能省,否则代码改完了会以为已经改完。

图 2:官方文件 IncludeExclude.java(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 IncludeExclude.java
include/exclude 解决什么问题
terms 聚合默认返回按文档数排序的前 N 个桶,很多时候只想要其中一部分:只要某个前缀的值(include 配正则);排掉测试数据(exclude 配正则);或者只要精确列举的几个值(传数组)。还有一种用法是把桶集合切成几份分别取,用 partition 与 num_partitions 指定取第几份(对应前面第 377 行的构造),常见场景是把一个很大的 terms 聚合拆成多次请求处理。
所以这个字段的语义一直是清楚的,6.0 改的只是「正则怎么表达」这一层包装。改法不难,难在把散落在各处的写法找全。

图 3:官方文件 DateRangeAggregationBuilder.java(v6.8.23 标签)。来源:GitHub elastic/elasticsearch v6.8.23 DateRangeAggregationBuilder.java
date_range 的数值 to/from 按 format 解释
Numeric
toandfromparameters indate_rangeaggregations used to always be interpreted asepoch_millis, making other numeric formats likeepoch_secondsunusable for numeric input values. Now we interpret these parameters according to theformatof the target field. If theformatin the mappings is not compatible with the numeric input value, a compatibleformat(e.g.epoch_millis,epoch_second) must be specified in thedate_rangeaggregation, otherwise an error is thrown.
以前 date_range 里的数值 to/from 一律按 epoch_millis 解释,于是 epoch_second 这类数值输入没法用。6.0 起按目标字段的 format 解释,mappings 里的 format 与数值输入不兼容时,必须在聚合里显式写 format(如 epoch_millis、epoch_second),否则抛错。
代码上对应 DateRangeAggregationBuilder.java 第 296–306 行:取 config.format() 做解析,注释里写的正是「不同 format(epoch_millis 与 epoch_second)配数值输入」这件事。
这一条是 4 条里第二个会直接报错的:字段 format 不是 epoch_millis、又没在聚合里写 format 的 date_range 请求,升级后从「算错」变成「抛错」。排查时把所有 date_range 找出来,看 to/from 是不是数值。
terms 聚合的两个执行提示废弃
The execution hints
global_ordinals_hashandglobal_ordinals_low_cardinalityare deprecated and should be replaced byglobal_ordinalswhich now internally choose whether it should remap global ordinals to dense ordinals or directly use the segment ordinals.
execution_hint 里写 global_ordinals_hash 或 global_ordinals_low_cardinality 的,6.0 起打废弃日志,统一换成 global_ordinals——后者内部自己决定是把全局序号重映射成稠密序号,还是直接用段内序号。
废弃日志在 TermsAggregatorFactory.java 第 311–316 行([global_ordinals_hash] is deprecated. Please use [global_ordinals] instead.;significant_terms 侧同样处理,见 SignificantTermsAggregatorFactory 第 311–312 行)。这条不阻断升级,但要排进整改清单,8.0 之前处理完。
composite 聚合的 missing 改成 missing_bucket
The
missingoption of thecompositeaggregation is deprecated,missing_bucketshould be used instead.
6.8 里 missing_bucket 是正式的解析字段(CompositeValuesSourceParserHelper.java 第 45 行),序列化时写成 missing_bucket(CompositeValuesSourceBuilder.java 第 127 行);用 missing 会打废弃日志(第 244 行),两个同时写会报 cannot use [missing] option in conjunction with [missing_bucket](第 335 行)。
composite 是 6.1 引入的,所以这条只影响 6.x 内部写过的配置;升级到 6.8 之后按新字段写即可。
检查清单
- 聚合定义里的
{"pattern": ...}全部改成直接的字符串(代码、模板、Kibana saved objects、Watcher/告警); - Java 侧
IncludeExclude按用途选构造:正则用String/RegExp,精确值用数组或SortedSet; - 所有
date_range检查to/from是不是数值,是的话确认字段format兼容,不兼容就在聚合里显式写format; execution_hint里的global_ordinals_hash/global_ordinals_low_cardinality换成global_ordinals;- composite 聚合的
missing换成missing_bucket(两者不能同时写); - 改完挑几个关键看板做前后对比,确认聚合桶集合没变。
第 3 条优先处理:它是这批里唯一从「静默算错」变成「直接报错」的,升级当天就会冒出来。第 1 条次之——不报错,但漏一处就有一处查询失败。
在 6.8.23 上跑一遍
同一个 terms 聚合,两种 include 写法:

图 4:本地 6.8.23 单节点实例上的实测输出。上:正则字符串;下:对象写法
curl -s -X POST "http://localhost:9200/t1/_search?filter_path=aggregations" \
-H 'Content-Type: application/json' \
-d '{"size":0,"aggs":{"a":{"terms":{"field":"f","include":"al.*"}}}}'
curl -s -X POST "http://localhost:9200/t1/_search" \
-H 'Content-Type: application/json' \
-d '{"size":0,"aggs":{"a":{"terms":{"field":"f","include":{"pattern":"al.*"}}}}}' \
| jq -r '"HTTP \(.status): \(.error.root_cause[0].reason)"'
字符串写法正常出桶(alpha 被选中);对象写法返回 HTTP 400: Unknown parameter in Include/Exclude clause: pattern。
这条报错会报错,属于好查的一类。但要注意它在 6.0 之前是静默忽略的——老版本上 {"pattern":...} 会被当空条件,聚合照常返回全部桶。也就是说同样一份聚合定义,在 5.x 上"结果偏多",在 6.x 上"直接报错",两边的现象完全不同。
结论是:升级后聚合结果变少的那些看板,先查 include/exclude 的写法;报错的反而好办,真正麻烦的是那些曾经被静默忽略、升级后突然生效的过滤条件。
写在最后
- 4 条里两条会报错:
pattern子对象写法与date_range的format不兼容; - 两条只打废弃日志:terms 的执行提示、composite 的
missing,不阻断但要排进整改; include/exclude现在只有两种形态:字符串是正则,数组是精确值集合;- 排查范围要扩到平台里存的对象,只 grep 代码仓库会漏掉 Kibana 与告警里的定义。
这 4 条和 6.0 移除同义词查询、_all 默认关闭属于同一类工作:把 API 表面收敛成「一种功能一种写法」。单独看每条都很小,合起来就是 6.0 升级清单里最长的一节,所以我的建议是迁移文档从头到尾过一遍,而不是只看特性级别的改动。
参考
本文对照 Elasticsearch
v6.8.23标签的官方文档与源码核对,引文逐字取自原文。图 4 的两次聚合来自本地 6.8.23 单节点实例。