arrayselasticsearchupdate-by-query

Elastic update_by_query inside array


I have an array which looks like this:

"_source": {
    "dateCreated": "2023-07-12",
    "sources": [
      {
        "1": null,
        "lastUpdateDate": "2023-08-10T11:12:36.9848505",
        "postedDate": "2023-08-10T10:38:57.9440773" 

How can I update postedDate from sources array?

I've managed to update dateCreated with this query:

POST index/_update_by_query
{
  "script": {
    "source": "ctx._source.dateCreated='2023-07-12'",
    "lang": "painless"
  },
  "query": {
    "terms": {
      "_id": [
"12345"
]
    }
  }
}

Solution

  • You can do it like this by accessing the array:

    ctx._source.sources[0].postedDate='2023-07-12'