问题
Suppose I have a array type field in my document of elastic search, like below:-
"prog_lists": [
{
"description": "engineer",
"age": 25,
"name": "ashu"
},
{
"description": "programmer",
"age": 26,
"name": "rajit"
},
{
"description": "designer",
"age": 27,
"name": "naveen"
}
]
I want to remove objects which have name equals to ashu, or remove multiple object which satisfy the query like whose age is greater then 25
I am able to do this by specifying all the things within object like below:-
client.update({
"index": "daffo_netgear",
"type": "array",
"id": "2201",
"body": {
"script":"ctx._source.prog_lists.remove(list)",
"params" : {
"list" :{
"description": "engineer",
"age": 25,
"name": "ashu"
}
}
}})
but i want to do this just specifying name or age
client.update({
"index": "daffo_netgear",
"type": "array",
"id": "2201",
"body": {
"script":"ctx._source.prog_lists.remove(list)",
"params" : {
"list" :{
"name": "ashu"
}
}
}})
My node module elastic search version is 2.4.2 elastic search server is 1.3.2.
回答1:
I have got my answer, and its solution is
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"}
}
来源:https://stackoverflow.com/questions/26232731/remove-objects-from-array-that-satisfying-the-condition-in-elastic-search-with-j