【问题标题】:Elasticsearch: How do I remove a nested object element?Elasticsearch:如何删除嵌套对象元素?
【发布时间】:2017-07-02 21:53:53
【问题描述】:

我正在使用 Elasticsearch 5.4,并尝试从嵌套数据类型中删除一个元素。

我有以下映射:

"links_to_asset": {
    "type": "nested",
    "properties": {
        "note_link_id": {
            "type": "long"
        },
        "user_id": {
            "type": "long"
        },
        "creation": {
            "type": "date",
            "format": "date_hour_minute_second"
        },
        "modification": {
            "type": "date",
            "format": "date_hour_minute_second"
        },
        "to_asset": {
            "type": "integer"
        },
        "from_asset": {
            "type": "integer"
        },
        "comment": {
            "type": "text",
            "fields": {
                "std": {
                    "type": "text",
                    "analyzer": "asset_en_analyzer",
                    "fields": {
                        "std": {
                            "type": "text",
                            "analyzer": "standard"
                        }
                    }
                }
            }
        }
    }
}

我在 Postman 中尝试过以下操作:

localhost:9200/asset/bookmark/20976/_update?pretty

{
    "script": "ctx._source.links_to_asset.removeAll{it['note_link_id'] == id}",
    "params": {
        "id": 7343
    }
}

但我收到以下错误:

{
    "error": {
        "root_cause": [
            {
                "type": "remote_transport_exception",
                "reason": "[NvXYDwh][127.0.0.1:9300][indices:data/write/update[s]]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "failed to execute script",
        "caused_by": {
            "type": "script_exception",
            "reason": "compile error",
            "script_stack": [
                "... .links_to_asset.removeAll{it['links_to_asset.note_ ...",
                "                             ^---- HERE"
            ],
            "script": "ctx._source.links_to_asset.removeAll{it['links_to_asset.note_link_id'] == id}",
            "lang": "painless",
            "caused_by": {
                "type": "illegal_argument_exception",
                "reason": "unexpected token ['{'] was expecting one of [{<EOF>, ';'}]."
            }
        }
    },
    "status": 400
}

我已经按照 StackOverflow [1] [2] 上几个不同问题的建议,但没有成功。

嵌套对象是有效的,因为我已经用数据填充了它。另外,id 值也是有效的。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您尝试编写 groovy 脚本,但您使用的脚本语言是 Elasticsearch 5.x 中的 painless

    试试类似的东西

    ctx._source.foo = ctx._source.foo.stream().filter(x -> x =='a').collect(Collectors.toList())
    

    【讨论】:

    • 几乎——它会删除嵌套对象中的所有内容,而不是 ID 为params:ctx._source.links_to_asset = ctx._source.links_to_asset.stream().filter(x -&gt; x =='a').collect(Collectors.toList())的元素
    • @WayneSmallman 那么你可以使用params: ctx._source.links_to_asset = ctx._source.links_to_asset.stream().filter(x -&gt; x.id != params.id).collect(Collectors.toList())
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多