2014-10-07 34 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"} 
}