gpt4 book ai didi

arrays - Elasticsearch:设置字段中数组元素的数量

转载 作者:行者123 更新时间:2023-12-02 23:35:50 25 4
gpt4 key购买 nike

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

例如

{
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"
}

关于arrays - Elasticsearch:设置字段中数组元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31090839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com