Elasticsearch:设置字段中数组元素的个数

Elasticsearch : Set the number of the elements of an array in a field

我正在尝试将数组元素的数量存储到具有更新的字段中。 (用数组的计数更新记录)。

例如

{
  array = [ { "a" : "b" }, { "c" : "d" } ]
  nb_elements = 2
}

我已尝试使用以下脚本进行更新

{
  "script" : "ctx._source.nb_elements= ctx._source.array.values.length"
}

但它不起作用。 我也试过了:

{
"script" : "ctx._source.nb_elements = count",
"params" : {
   "count" : ctx._source.array.values.length
   }
}

但我并没有更成功。

有人知道这是否可行吗?如果可行,该怎么做?

首先,您需要通过将 script.disable_dynamic: false 添加到您的配置文件来启用动态脚本。

其次,我假设您正在使用 groovy (It's the default scripting language from 1.4)。

update api 与此脚本一起使用应该有效:

{
  "script": "ctx._source.nb_elements=ctx._source.array.size"
}