Node.js 使用javascript api从数组中删除满足弹性搜索条件的对象
假设我的弹性搜索文档中有一个数组类型字段,如下所示:-Node.js 使用javascript api从数组中删除满足弹性搜索条件的对象,node.js,elasticsearch,Node.js,elasticsearch,假设我的弹性搜索文档中有一个数组类型字段,如下所示:- "prog_lists": [ { "description": "engineer", "age": 25, "name": "ashu" }, { "description": "programmer", "age": 26, "name": "rajit" }, { "description": "designer", "age": 27, "name": "naveen" } ] 我
"prog_lists": [
{
"description": "engineer",
"age": 25,
"name": "ashu"
},
{
"description": "programmer",
"age": 26,
"name": "rajit"
},
{
"description": "designer",
"age": 27,
"name": "naveen"
}
]
我想删除name等于ashu的对象,或者删除满足查询的多个对象,比如年龄大于25岁的对象
我可以通过指定对象中的所有内容来实现这一点,如下所示:-
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"
}
}
}})
但我只想指定姓名或年龄
client.update({
"index": "daffo_netgear",
"type": "array",
"id": "2201",
"body": {
"script":"ctx._source.prog_lists.remove(list)",
"params" : {
"list" :{
"name": "ashu"
}
}
}})
我的节点模块弹性搜索版本是2.4.2弹性搜索服务器是1.3.2。我得到了答案,它的解决方案是
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"}
}
如果您有多个符合条件的项目,请使用列表:
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"}
}