2016-03-09 34 views
0

我想用下面的腳本ElasticSearch腳本本期的UPDATE

POST /book/123/_update 
{ 
"script": "foreach (item : ctx._source.BookAvailability) { if (item['BookPublishDate'] == publish_date) { item['Price'] = start_price;} }", 
    "params": { 
    "publish_date":"2016-09-09T12:00:00", 
    "start_price": "123"} 
} 

但是我得到以下錯誤,更新數組值,

{ 
    "error": "ElasticsearchIllegalArgumentException[failed to execute script]; nested: GroovyScriptExecutionException[MissingMethodException[No signature of method: a7119b66f45277239600593efdc39002b301e293.foreach() is applicable for argument types: (java.util.LinkedHashMap, a7119b66f45277239600593efdc39002b301e293$_run_closure1) values: [[item:[[Price:189.000, BookCategoryID:e62dcda3-579a-4936-902c-05e1bcc97e3e, ...], ...]], ...]\nPossible solutions: each(groovy.lang.Closure)]]; ", 
    "status": 400 
} 

和我的模式是這樣的,

{ 
"BookID" : "123", 
"BookName": "abcd", 
. 
. 
. 
"BookAvailability" : [ 
{ 
    "BookCategoryID" : "e62dcda3-579a-4936-902c-05e1bcc97e3e", 
    "Price": "189.00", 
    "BookPublishDate": "2016-09-09T12:00:00" 
    . 
    . 
    . 
} 
] 
} 

回答

1

試試這個腳本代替

ctx._source.BookAvailability.findAll { it['BookPublishDate'] == publish_date }.each { it['Price'] = start_price } 

它首先選擇其BookPublishDatepublish_date參數匹配,然後將其遍歷的比賽的所有元素,並設置Pricestart_price

+0

No Luck獲取另一個錯誤:「ElasticsearchIllegalArgumentException [未能執行腳本];嵌套:GroovyScriptExecutionException [MissingMethodException [方法的簽名:java.util.ArrayList.filter()適用於參數類型: (0d80cd763af1def98f143ee0694cc84eaefbd1d8 $ _run_closure1)values:[0d80cd763af1def98f143ee0694cc84eaefbd1d8 $ _run_closure1 @ 1f7f5c5] \ n可能的解決方案:clear(),size(),clear(),clear(),size(),first()]];「, 」狀態「 :400 }' – TBA

+0

對不起,它不是'過濾器'它是'findAll',我的不好,我已經更新了我的答案。 (混合太多的語言在一次可以毀了你的健康:) – Val

+0

謝謝它的工作:) – TBA