【问题标题】:Elastic search update script to return a value用于返回值的弹性搜索更新脚本
【发布时间】:2019-11-04 18:06:52
【问题描述】:

我正在编写一个更新索引嵌套字段的脚本。基本上它添加了新的嵌套文档。我想更新脚本以返回该字段的嵌套文档的总数。

这是结构

索引:主题 嵌套字段:users_read

每当有新用户阅读时,他的名字都会添加到“users_read”中。这通过脚本发生。

我想修改脚本返回users_read的数值。

{
                "_index": "topic",
                "_type": "_doc",
                "_id": "11111",
                "_score": 1.0,
                "_source": {
                    "id": "11111",
                    "users": [
                        {
                            "users_read": "div.sar@abc.com"
                        },
                        {
                            "users_read": "admin2@xyz.com"
                        },
                        {
                            "users_read": "admin"
                        },
                        {
                            "users_read": "admin2@def.com"
                        }
                    ]
        }
}

这是我正在使用的脚本

POST - http://elasticserver:9200/topic/_update/11111

{
    "script": {
        "inline": "ctx._source.users.add(params.user)",
        "lang": "painless",
        "params": {
            "user": {
                "users_read":"admin2@dfg.com"
            }
        },
        "source": "ctx._source.users.size()"
    }
}

但我看到了一个异常

{
    "error": {
        "root_cause": [
            {
                "type": "x_content_parse_exception",
                "reason": "[10:19] [script] failed to parse field [source]"
            }
        ],
        "type": "x_content_parse_exception",
        "reason": "[10:19] [UpdateRequest] failed to parse field [script]",
        "caused_by": {
            "type": "x_content_parse_exception",
            "reason": "[10:19] [script] failed to parse field [source]",
            "caused_by": {
                "type": "illegal_argument_exception",
                "reason": "must only use one of [source, id] when specifying a script"
            }
        }
    },
    "status": 400
}

当我删除“源”部分时,脚本会更新该字段。

POST - http://elasticserver:9200/topic/_update/11111

{
    "script": {
        "inline": "ctx._source.users.add(params.user)",
        "lang": "painless",
        "params": {
            "user": {
                "users_read":"admin2@dfg.com"
            }
        }
    }
}

{
    "_index": "topic",
    "_type": "_doc",
    "_id": "11111",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

非常感谢任何帮助。

【问题讨论】:

  • 你必须使用聚合,我不认为我们可以使用源脚本。

标签: elasticsearch nested return


【解决方案1】:

我不得不从脚本中删除“来源”。在单个脚本中同时包含源代码和内联代码是行不通的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-06
    • 2020-05-23
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多