Q:

How to add a json object to multiple documents in a Elastic index using _update_by_query?

I need to update several documents in my Elasticsearch index and I tried the following using the the _update_by_query plugin.
What I need to do is to add a new field to several existing documents matching a certain condition. The new field is a nested JSON. So after adding it document source should look like
_source: {
    ...existing fields,
    "new_field" : {
        "attrName1" : "value",
        "attrName2" : "value",
    }
}  

I tried using the _update_by_query API to get this done. But so far I only could add String fields and arrays with it. When trying to add a JSON with the following query it gives me an error.
Query
curl -XPOST "http://xxx.xxx.xxx.xxx:pppp/my_index_name/_update_by_query" -d'
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "team.keyword": "search_phrase"
          }
        }
      ]
    }
  },
  "script" : {
    "inline":"ctx._source.field_name = {\"a\":\"b\"}"
  }
}'

Error 
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "ctx._source.field_name = {\"a\":\"b\"}",
          "                         ^---- HERE"
        ],
        "script": "ctx._source.field_name = {\"a\":\"b\"}",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "invalid sequence of tokens near ['{'].",
      "caused_by": {
        "type": "no_viable_alt_exception",
        "reason": null
      }
    },
    "script_stack": [
      "ctx._source.field_name = {\"a\":\"b\"}",
      "                         ^---- HERE"
    ],
    "script": "ctx._source.field_name = {\"a\":\"b\"}",
    "lang": "painless"
  },
  "status": 500
}

So far I could only add Strings as a new field. What is the correct way to achieve this?

A:

Instead of direct assignment, use params to achieve the same.
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "team.keyword": "search_phrase"
          }
        }
      ]
    }
  },
  "script": {
    "inline": "ctx._source.field_name = params.new_field",
    "params": {
      "new_field": {
        "a": "b"
      }
    }
  }
}

