UPDATE的ElasticSearch脚本问题

时间:2016-03-09 05:32:50

标签: elasticsearch groovy

我正在尝试使用以下脚本

更新数组值
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 个答案:

答案 0 :(得分:1)

尝试使用此脚本

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

首先选择BookPublishDatepublish_date参数匹配的所有元素,然后迭代匹配并将Price设置为start_price