我需要从满足条件的数组中删除对象,我可以根据条件更新数组的对象,如下所示:
PUT twitter/twit/1
{"list":
[
{
"tweet_id": "1",
"a": "b"
},
{
"tweet_id": "123",
"a": "f"
}
]
}
POST /twitter/twit/1/_update
{"script":"foreach (item :ctx._source.list) {
if item['tweet_id'] == tweet_id) {
item['new_field'] = 'ghi';
}
}",
"params": {tweet_id": 123"}
}
这是有效的
为了删除我正在这样做
POST /twitter/twit/1/_update
{ "script": "foreach (item : ctx._source.list) {
if item['tweet_id'] == tweet_id) {
ctx._source.list.remove(item);
}
}",
"params": { tweet_id": "123" }
}
但这不起作用并给出此错误,
ElasticsearchIllegalArgumentException[执行脚本失败]; 嵌套:并发修改异常;错误:ElasticsearchIllegalArgumentException[未能执行脚本];嵌套:ConcurrentModificationException
我可以使用删除整个数组或整个字段
"script": "ctx._source.remove('list')"
我还可以通过使用指定对象的所有键来从数组中删除对象
"script":"ctx._source.list.remove(tag)",
"params" : {
"tag" : {"tweet_id": "123","a": "f"}
我的节点模块弹性搜索版本是 2.4.2 弹性搜索服务器是 1.3.2