无法执行脚本Elasticsearch

问题描述:

我想更新特定字段中的某些值,我希望它是唯一的。无法执行脚本Elasticsearch

但是,当我给脚本条件.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()适用于参数类型:() values:[] \ n可能的解决方案:minus(java.lang.Object), minus(java.util.regex.Pattern),minus(java.lang.Object ),size(), size(),use([Ljava.lang.Object;)]];“,”status“: 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()'; 

它也会返回相同的错误! 不知道为什么!

从错误中,关键点是No signature of method: java.lang.String.unique()。这可能是因为update_field不存在或在某个点是空的,因此+运算符将简单地尝试将它连接为您指定的数组的字符串。

解决此问题的一种方法是确保update_field被视为一个数组,无论如何。你应该可以通过用(ctx._source.update_field ?: [])代替ctx._source.update_field来实现这一点,那样如果update_field不存在或为空,那么将使用空数组而不是在字符串字段上使用数组。

$script['script'] = 'ctx._source.update_field = ((ctx._source.update_field ?: []) + new_value).unique()'; 
+0

感谢您的答案和解释,但它也返回我同样的错误,我也有一个样本文档更新我的问题,能不能请看看,非常感谢 –

+1

你可能需要擦拭你的'myIndex'索引并重新创建它,因为你的'update_field'已经被索引为一个字符串,例如''[funny]“'。 – Val

+0

@Oops我的不好,非常感谢Val,非常感谢您的帮助:),你真棒 –