Hi there
today I was a little bit confused with the documentation about the Painless language. My goal was to use the _update_by_query
API to correct the year of some date fields. I was happy to read that...
Date fields are exposed as ZonedDateTime, so they support methods like getYear, getDayOfWeek
SO I was hoping that the below code would work...
POST my-index/_update_by_query
{
"script": {
"source": """
ctx._source.myDate = ctx._source.myDate.withYear(year);
""",
"lang": "painless"
}
}
...but it failed with:
dynamic method [java.lang.String, withYear/1] not found
I managed to get my update to work by first parsing the field, as shown below:
POST my-index/_update_by_query
{
"script": {
"source": """
ctx._source.myDate = ZonedDateTime.parse(ctx._source.myDate).withYear(year);
""",
"lang": "painless"
}
}
Was that expected to work this way? Did I misunderstood something?