2014-10-07 37 views
1

假设我有一个数组类型字段我的弹性搜索的文件中,象下面这样: -从数组中删除对象的JavaScript API满足弹性搜索条件

"prog_lists": [ 
{ 
    "description": "engineer", 
    "age": 25, 
    "name": "ashu" 
}, 
{ 
    "description": "programmer", 
    "age": 26, 
    "name": "rajit" 
}, 
{ 
    "description": "designer", 
    "age": 27, 
    "name": "naveen" 
} 
] 

我想删除其中有名称的对象等于阿苏,或删除其满足查询又像年龄大于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。

回答

1

我有我的答案,它的解决方案是

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"} 
}