【问题标题】:failed to execute script Elasticsearch无法执行脚本 Elasticsearch
【发布时间】:2015-12-24 16:32:23
【问题描述】:

我正在尝试更新特定字段中的一些值,我希望这些值是唯一的。

但是当我给脚本提供条件 .unique() 时,它会返回一个错误。

如果我没有给出唯一的条件,那么它会一次又一次地更新。 表示该值将是

update_field=array('world','values','frost','world','values','frost',...)

我在这里做错了什么谁都知道!!!

        $script['script'] = 'ctx._source.update_field = (ctx._source.update_field + new_value).unique()';
        // unique() is giving the error but why

错误信息---

{"error":"ElasticsearchIllegalArgumentException[执行失败 脚本];嵌套: GroovyScriptExecutionException[MissingMethodException[没有签名 方法:java.lang.String.unique() 适用于参数类型:() 值:[]\n可能的解决方案:减号(java.lang.Object), 减(java.util.regex.Pattern),减(java.lang.Object),大小(), 大小(),使用([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()';

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

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    从错误来看,关键点是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()';
    

    【讨论】:

    • 感谢您的回答和解释,但它也给我同样的错误,我也用示例文档更新了我的问题,您可以看看,非常感谢
    • 您可能需要擦除您的myIndex 索引并重新创建它,因为您的update_field 已经被索引为字符串,即"[funny]"
    • @oops 我的错,非常感谢 Val,非常感谢你的帮助 :),你太棒了
    • 我已重新索引我的文档,它运行良好,再次感谢您的帮助 :)
    • 太棒了,很高兴它有帮助!
    猜你喜欢
    • 2016-10-18
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 2019-08-03
    相关资源
    最近更新 更多