2013-03-03 27 views
1

我試圖只有當項目不在列表中時才更新列表字段。elasticsearch更新一組

有什麼錯:

curl -X POST 'http://localhost:9200/my_index/my_doc/id/_update' -d 
'{ "script": 
"{if !(ctx._source.my_field contains new_item) {ctx._source.my_field.add(new_item)}}" 
, "params":{"new_item":"hopefully_new_text"}}' 

這無論如何增加的項目,即使它的存在。

任何更聰明的方法來做到這一點?

回答

1

這是關於MVEL解析行爲的許多奇怪事情之一。如果將去除最外面的{},你會看到,你有一個語法錯誤在你的if語句,如果你將修復這個錯誤,一切都將工作:

curl -X POST 'http://localhost:9200/my_index/my_doc/id/_update' -d '{ 
    "script": "if (!(ctx._source.my_field contains new_item)) {ctx._source.my_field.add(new_item)}", 
    "params": { 
     "new_item": "hopefully_new_text" 
    } 
} 
'