# 需求

ES 里有两个 fileds 数据,其实性质上是一致的,都是统计 cluster,但是名字不同,一个叫 cluster,一个叫 cluster_name,在尝试做 visualize 时因为 terms 时仅能够对其中一个 filed 生效,会导致另一列数据无法统计,会丢掉

# 探索

# 尝试 ES 官方提供的 3 种方法

ES 官方给了三种方法,参考地址

# 方法一:Script

在 dsl 中写入脚本,在 devtool 测试脚本时报错,说没有 runtime_mappings 这个 type,网上查询后原来我们 es 版本低不支持 runtime_mappings,并且 es 不是我们 team,不是想升级就能升级的。

# 方法二:copy to

这个需要在数据写入时添加 copy_to 属性,数据写入不是我们负责,我们是下游团队,该方法也不行。我们无权写入数据,而且是不好沟通的。

# 方法三:multi_terms

在聚合时指定多个 terms,devtool 执行时报错,说没有 multi_terms 这个 type,同方法一,这个也要升级 es 到 7.12,所以该方法也不行。

# ES 团队提供的方法

在创建 visualize 时,设置两层聚合(即两层 split series)。

  • 第一个 split series 里 terms cluster 字段,并且勾选 Show missing values;
  • 第二个 split series 里 terms cluster_name 字段,并且勾选 Show missing values

查询 Inspect,可以看到 DSL 具体数据格式。程序里通过该 DSL 查询即可。这里的弊端是数据层级比较复杂。

# 参考

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_multi_field_terms_aggregation

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script

https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-multi-terms-aggregation.html