我正在尝试使用以下脚本
更新数组值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"
.
.
.
}
]
}
答案 0 :(得分:1)
尝试使用此脚本
ctx._source.BookAvailability.findAll { it['BookPublishDate'] == publish_date }.each { it['Price'] = start_price }
首先选择BookPublishDate
与publish_date
参数匹配的所有元素,然后迭代匹配并将Price
设置为start_price
。