Painless processor add field and set name from variable

Hi,

I'm trying to use a painless script processor to split the content of a field and use the result of the split to add new fields to the document. The problem is that I can't figure out how to set a new field, the name and its content, from variables.

I have a field that looks like this:

{
    "_source": {
        "message": "test1 = 1 | test2 = 2 | test3 = 3"
    }
 }

And I want the result to look like this:

{
    "_source":{
         "message": "test1 = 1 | test2 = 2 | test3 = 3",
         "test1": 1,
         "test2": 2,
         "test3": 3
    }
}

This is the script that I have so far and what I am trying to do.

{
"source": """
    String  [] msgFields = ctx.message.splitOnToken('|');

    for (field in msgFields)
    {
      def keyValue=field.splitOnToken('=');

      //This is what i want to do set name of the field to the value of keyValue[0] and set the value of the field to keyValue[1]
      ctx.keyValue[0]=keyValue[1];
   }
"""
}