1

I have a nested type and I need to get the index of a specific object to perform an update:

{
  "_index": "asset_en_v1",
  "_type": "note",
  "_id": "23217",
  "_version": 24,
  "found": true,
  "_source": {
    "user_id": "11",
    "title": "Title",
    "note": "Note.",
    "creation": "2017-05-31T21:36:01",
    "modification": "2017-05-31T21:36:01",
    "links": [
      {
        "note_link_id": "7310",
        "user_id": "11",
        "creation": "2017-06-01T14:41:50",
        "modification": "2019-06-01T14:42:00",
        "comment": "Comment goes here."
      },
      {
        "note_link_id": "7311",
        "user_id": "11",
        "creation": "2017-06-01T14:42:42",
        "modification": "2019-06-01T14:42:00",
        "comment": "Yep..."
      },
      {
        "note_link_id": "7312",
        "user_id": "11",
        "creation": "2017-06-01T15:33:55",
        "modification": "2017-06-01T15:34:00",
        "comment": "Jumo."
      }
    ]
  }
}

So far, I've created an _update statement in Painless script that almost does the job, but I'm struggling to get a match:

{
    "script": {
        "lang": "painless",
        "inline": "def note_link_id = 7311; def links = ctx._source.links; for (int i = 0; i < links.length; ++i) { if (links[i].note_link_id == note_link_id) { ctx._source.links[note_link_id].comment = params.comment; ctx._source.links[note_link_id].modification = params.modification } }",
        "params": {
            "modification": "2019-06-01T14:42:00",
            "comment": "QWERTY!"
        }
    }
}

Here, links[i].note_link_id == note_link_id isn't matching.

Any ideas?

Wayne Smallman
  • 1,690
  • 11
  • 34
  • 56

1 Answers1

3

In your inline script, it should have been i++, instead of ++i

def note_link_id = 7311; 
def links = ctx._source.links; 
for (int i = 0; i < links.length; i++) //Note i++, instead of ++i
{ 
    if (links[i].note_link_id == note_link_id) 
    { 
        ctx._source.links[note_link_id].comment = params.comment;
        ctx._source.links[note_link_id].modification = params.modification 
    } 
}
Sunil Purushothaman
  • 8,435
  • 1
  • 22
  • 20
  • Sunil, thank you for suggestion. In the end, I had to use another technique, and I'm not in a position to make the changes to check if your idea would work or not. But if I return to this issue in the future, I'll keep this in mind. – Wayne Smallman Jan 04 '18 at 08:56
  • I have since used this code for my own purposes and have found it to be correct, though the end result was slightly different to the purposes of @WayneSmallman as you can see here: https://stackoverflow.com/questions/67126622/elasticsearch-node-js-api-remove-an-object-from-an-array-on-a-document-using-pai – Jarede Apr 18 '21 at 13:47