0
votes

I am using below inline script with update by query to update approved=true in elastic and the timestamp lastModifiedDate.

POST /limp-access/_update_by_query
{
    "query": {
        "terms": {
            "_id": [
                "asdasfasf-laHg5qeld",
                "asdfadfdfdsf-asdasd"
             ]
        }
    },
    "script":{
    "source": "ctx._source.approved = true; ctx._source.fields.lastModifiedDate = ['2019-05-21T06:16:05.133Z'];"
  }
}

The problem is that some documents do not have this field lastModifiedDate at all. So whenever it encounters such doc, it fails with null pointer expression. Is there a way to ignore such docs and update the one's where it is found?

EDIT: Posting the error that I get below:

{ "error": { "root_cause": [ { "type": "script_exception", "reason": "runtime error", "script_stack": [ "ctx._source.fields.lastModifiedDate = ['2019-05-21T06:16:05.133Z'];", " ^---- HERE" ], "script": "ctx._source.approved = true; ctx._source.fields.lastModifiedDate = ['2019-05-21T06:16:05.133Z'];", "lang": "painless" } ], "type": "script_exception", "reason": "runtime error", "script_stack": [ "ctx._source.fields.lastModifiedDate = ['2019-05-21T06:16:05.133Z'];", " ^---- HERE" ], "script": "ctx._source.approved = true; ctx._source.fields.lastModifiedDate = ['2019-05-21T06:16:05.133Z'];", "lang": "painless", "caused_by": { "type": "null_pointer_exception", "reason": null } }, "status": 500 }

The expectation is that if one the IDs do not have the field fields, I want it to be ignored and the script to update the rest of the documents where the field fields is found. Is this possible?

1
You mean that some documents don't have the field named fields. Can you show the error you get?Val
@Val Yes, some documents do not have this field. I have updated the error in the original post.Koshur

1 Answers

2
votes

Then simply update your script like this:

"source": "ctx._source.approved = true; if (ctx._source.fields != null) { ctx._source.fields.lastModifiedDate = ['2019-05-21T06:16:05.133Z'];}"