Reputation: 1197
I am trying to update an array value with following script
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"}
}
However I am getting following error,
{
"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
}
And My Schema goes like this,
{
"BookID" : "123",
"BookName": "abcd",
.
.
.
"BookAvailability" : [
{
"BookCategoryID" : "e62dcda3-579a-4936-902c-05e1bcc97e3e",
"Price": "189.00",
"BookPublishDate": "2016-09-09T12:00:00"
.
.
.
}
]
}
Upvotes: 0
Views: 307
Reputation: 217424
Try this script instead
ctx._source.BookAvailability.findAll { it['BookPublishDate'] == publish_date }.each { it['Price'] = start_price }
It first selects all elements whose BookPublishDate
matches the publish_date
argument and then it iterates over the matches and sets the Price
to start_price
.
Upvotes: 1