从数组弹性搜索中删除对象 [英] remove objects from array elastic search
问题描述
我需要从数组中删除满足条件的对象,我可以根据条件更新数组的对象,如下所示:
I have required to remove object from array that satisfies the condition, I am able to update the object of array on the basis of condition, which is as follow:
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" }
}
但这不工作,并提供此错误,
but this is not working and giving this error,
ElasticsearchIllegalArgumentException [无法执行脚本]。
嵌套:ConcurrentModificationException;错误:
ElasticsearchIllegalArgumentException [执行脚本失败];
嵌套:ConcurrentModificationException
ElasticsearchIllegalArgumentException[failed to execute script]; nested: ConcurrentModificationException; Error: ElasticsearchIllegalArgumentException[failed to execute script]; nested: 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
my node module elastic search version is 2.4.2 elastic search server is 1.3.2
推荐答案
你得到这一点,因为你尝试在迭代列表时修改列表,这意味着你想要更改对象列表,同时列出这些对象。
You get that because you are trying to modify a list while iterating through it, meaning you want to change a list of object and, at the same time, listing those objects.
您需要这样做:
POST /twitter/twit/1/_update
{
"script": "item_to_remove = nil; foreach (item : ctx._source.list) { if (item['tweet_id'] == tweet_id) { item_to_remove=item; } } if (item_to_remove != nil) ctx._source.list.remove(item_to_remove);",
"params": {"tweet_id": "123"}
}
如果您有多个符合条件的项目,请改用列表:
If you have more than one item that matches the criteria, use a list instead:
POST /twitter/twit/1/_update
{
"script": "items_to_remove = []; foreach (item : ctx._source.list) { if (item['tweet_id'] == tweet_id) { items_to_remove.add(item); } } foreach (item : items_to_remove) {ctx._source.list.remove(item);}",
"params": {"tweet_id": "123"}
}
这篇关于从数组弹性搜索中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!