我正在尝试更新特定字段中的某些值,我希望这些值是唯一的。
但是当我给脚本条件.unique()时,它会给我一个错误。
如果我不给出唯一的条件,那么它会一次又一次地更新。 意味着价值将是
update_field =数组('世界','价值观''霜冻','世界'价值' ;,'霜',...)
我在做错了什么人都知道!!!
$script['script'] = 'ctx._source.update_field = (ctx._source.update_field + new_value).unique()';
// unique() is giving the error but why
错误消息---
{"错误":" ElasticsearchIllegalArgumentException [未能执行 脚本];嵌套: GroovyScriptExecutionException [MissingMethodException [无签名] 方法:java.lang.String.unique()适用于参数类型:() 值:[] \ n可能的解决方案:减去(java.lang.Object), 减去(java.util.regex.Pattern),减去(java.lang.Object),size(), size(),use([Ljava.lang.Object;)]]; ""状态" 400}
示例文件---
hits: {
total: 19,
max_score: 5.5197725,
hits: [
{
_index: "myIndex",
_type: "myType",
_id: "pdd9da099f380b3951a627a5d5a38758680016f16",
_score: 5.5197725,
_source: {
content: "After shooting attacks are several police cars sent to the streets to watch.
title: "The police in Copenhagen when bødemål despite extra tasks",
update_field: "[funny]"
}},}
所以你可以看到update_field不是空的并且有一些价值 - 当我尝试的时候---
$script['script'] = 'ctx._source.update_field = ((ctx._source.update_field ?: []) - new_value).unique()';
它也会给我带来同样的错误! 不知道为什么!!
答案 0 :(得分:1)
从错误中,关键点是No signature of method: java.lang.String.unique()
。这可能是因为update_field
不存在或在某些时候是空的,因此+
运算符将只是尝试将其连接为与您指定的数组的字符串。
解决此问题的一种方法是确保将update_field
视为数组,无论如何。您应该可以通过将ctx._source.update_field
替换为(ctx._source.update_field ?: [])
来实现此目的,如果update_field
不存在或为空,那么将使用空数组,而{{1} }运算符将处理数组而不是字符串字段。
+