从ElasticSearch数组中删除元素/对象,然后跟随匹配查询

11
我在尝试从Elasticsearch的数组中删除元素/对象时遇到了问题。
这是索引的映射:
{
    "example1": {
        "mappings": {
            "doc": {
                "properties": {
                    "locations": {
                        "type": "geo_point"
                    },
                    "postDate": {
                        "type": "date"
                    },
                    "status": {
                        "type": "long"
                    },
                    "user": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }
    }
}

这是一个示例文档。

{
    "_index": "example1",
    "_type": "doc",
    "_id": "8036",
    "_score": 1,
    "_source": {
        "user": "kimchy8036",
        "postDate": "2009-11-15T13:12:00",
        "locations": [
            [
                72.79887719999999,
                21.193036000000003
            ],
            [
                -1.8262150000000001,
                51.178881999999994
            ]
        ]
    }
}

使用下面的查询,我可以添加多个位置。
POST /example1/_update_by_query
{
    "query": {
        "match": {
            "_id": "3"
        }
    },
    "script": {
        "lang": "painless",
        "inline": "ctx._source.locations.add(params.newsupp)",
        "params": {
            "newsupp": [
                -74.00,
                41.12121
            ]
        }
    }
}

但是我无法从位置中删除数组对象。我尝试了下面的查询,但它没有起作用。
POST example1/doc/3/_update
{
    "script": {
        "lang": "painless",
        "inline": "ctx._source.locations.remove(params.tag)",
        "params": {
            "tag": [
                -74.00,
                41.12121
            ]
        }
    }
}

请告诉我在这里哪里出错了。我正在使用弹性版本5.5.2。

1
尝试使用_update_by_query而不是_update端点。 - sramalingam24
我也尝试了_update_by_query。但它也不起作用。 POST /example1/_update_by_query { "query": { "match": { "_id": "3" } }, "script" : { "lang":"painless", "inline": "ctx._source.locations.remove(params.newsupp)", "params":{ "newsupp":[-74.00,41.12121 ] }} } - 5a01d01P
3个回答

9
在无痛脚本中,Array.remove() 方法是通过索引而非值来移除数组元素的。
以下是一个能够根据值来删除 Elasticsearch 脚本数组元素的工作示例:
POST objects/_update_by_query
{
    "query": {
        ...  // use regular ES query to remove only in relevant documents
    },
    "script": {
        "source": """
            if (ctx._source[params.array_attribute] != null) {
                for (int i=ctx._source[params.array_attribute].length-1; i>=0; i--) {
                    if (ctx._source[params.array_attribute][i] == params.value_to_remove) {
                        ctx._source[params.array_attribute].remove(i);
                    }
                }
            }
        """,
        "params": {
            "array_attribute": "<NAME_OF_ARRAY_PROPERTY_TO_REMOVE_VALUE_IN>",
            "value_to_remove": "<VALUE_TO_REMOVE_FROM_ARRAY>",
        }
    }
}

如果您的脚本只需从一个特定的数组属性中删除值,那么您可能希望将其简化。例如,从文档的.color_list数组中删除"green":
_doc/001 = {
    "color_list": ["red", "blue", "green"]
}

用于删除"green"的脚本:

POST objects/_update_by_query
{
    "query": {
        ...  // use regular ES query to remove only in relevant documents
    },
    "script": {
        "source": """
            for (int i=ctx._source.color_list.length-1; i>=0; i--) {
                if (ctx._source.color_list[i] == params.color_to_remove) {
                    ctx._source.color_list.remove(i);
                }
            }
        """,
        "params": {
            "color_to_remove": "green"
        }
    }
}

4
add()不同,remove()需要传入元素的索引来进行移除。
在painless中,ctx._source.locations是一个ArrayList。它拥有Listremove()方法:

E remove(int index)

删除列表中指定位置的元素(可选操作)。...

请参见Painless API - List获取其他方法。
请参见此答案获取示例代码。

0
"script" : {
    "lang":"painless",
    "inline":"ctx._source.locations.remove(params.tag)",
    "params":{
      "tag":indexToRemove
    }
}

如果使用ctx._source.locations.add(elt)添加元素,则可以使用ctx._source.locations.remove(indexToRemove)通过数组中的元素索引来删除。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接