Skip to content

Instantly share code, notes, and snippets.

@martinhynar
Last active February 22, 2023 12:55
Show Gist options
  • Save martinhynar/d149019d11e76f05da12 to your computer and use it in GitHub Desktop.
Save martinhynar/d149019d11e76f05da12 to your computer and use it in GitHub Desktop.
ElasticSearch: Updating nested document in array

Given the document

curl -XPUT 'localhost:9200/test/me/here' -d '{
  "top" : [
    { "searchkey" : "change"},
    { "searchkey" : "keep"}
  ]
}'

I need an update query that will add new field to sub-document with searchkey equal to change and will keep any other sub-document. The expected result is:

{
  "top" : [
    { "searchkey" : "change", "newfield" : "newvalue"},
    { "searchkey" : "keep"}
  ]
}

To update nested document by index, run:

curl -XPOST 'localhost:9200/test/me/here/_update' -d '{
    "script" : "ctx._source.top[0].newfield = v",
    "params" : {
      "v" : "newvalue"
    }
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment