petrolis
petrolis

Reputation: 143

Elasticsearch Updating nested objects

I found out the same problem in internet, but i dont understand a solution. The problem is, how can i update a nestend document without to update all array. Thank you so much.

Here is a nasted document

"links": [
  [
    {
      "note_link_id": "1",
      "user_id": "11",
      "creation": "2016-11-15T11:21:10",
      "modification": "2016-11-15T13:38:04",
      "to_asset": "100",
      "from_asset": "99",
      "comment": "Comment 1."
    },
    {
      "note_link_id": "2",
      "user_id": "11",
      "creation": "2016-11-15T13:37:04",
      "modification": "2016-11-15T13:37:27",
      "to_asset": "101",
      "from_asset": "99",
      "comment": "Comment 2."
    },
    {
      "note_link_id": "3",
      "user_id": "11",
      "creation": "2016-11-15T14:01:27",
      "modification": "2016-11-15T14:02:52",
      "to_asset": "102",
      "from_asset": "99",
      "comment": "Comment 3."
    }
  ]
],

Question: how can i update "note_link_id": "2" and update only comment from "Comment 2" to "blja blja blja"

I repeat, how can i update this array without to updating all fields, like "note_link_id": "1", "note_link_id": "2" and "note_link_id": "3"

Thank you

Upvotes: 2

Views: 3067

Answers (1)

Joanna Mamczynska
Joanna Mamczynska

Reputation: 2208

You can try to use Partial Updates and some scripting language. The script would look more or less (here in groovy) like:

if (ctx._source.links != null) {
    for (item in ctx._source.links) { 
        if (item.note_link_id == params.link_id_param) {
            item.comment = params.comment_param;
        }
    }
}

Then use the script with Update API:

POST /index_name/type_name/document_id/_update
{
   "script" : "...",
   "params" : {
      "link_id_param" : "2",
      "comment_param" : "blja blja blja"
   }
}

Just remember that documents in Elasticsearch are immutable so technically the whole document will be reindexed.

Upvotes: 2

Related Questions