1

I have a rather basic requirement, I would like to add an object to a document with painless within a query for update.

This is the object I would like to add

"memoire": {
      "codeActe": "C",
      "docType": "M",
      "uuid": "added ",
      "nonFacturable": false,
      "published": false
    }, 

I tried

    POST /csa_encounters/_update_by_query
{
  "script":
{ "source": """

  ctx._source.memoire.codeActe = "C";
  ctx._source.memoire.docType= "M";
  ctx._source.memoire.uuid = "added ";
  ctx._source.memoire.nonFacturable = false;
  ctx._source.memoire.published =false
""",
"lang": "painless"
},
    "query": {
        "bool" : {
            "must" : [
                { "query_string" : { "query" : "140c7646", "fields" : [ "_id" ] } }
            ]
        }
    }
}    

But I get the message null pointer exception as the field does not exist of course

"type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.memoire.codeActe = \"C\";\n  ",
      "                   ^---- HERE"
    ],
 ...
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 400

I am not able to figure out how to create the object as a new field if it does not exist already.

1 Answer 1

2

It's because the field memoire doesn't exist in your document. So just add the following lines at the beginning of your script:

if (ctx._source.memoire == null) {
    ctx._source.memoire = [:];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, I get the error type=no_viable_alt_exception, reason=null when doing this.
@HaroldL.Brown please create a new question possibly referencing this one but explaining the context of your issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.