How to add two new fields to a document

Hi, I'm trying to add two new fields to a document: all2_business_index.car, car being an array. The code below throws a null_pointer_exception
How canI fix this, thank you

Blockquote

        "script_stack": [
          "ctx._source.all2_business_index.car = []\r\n                                            ;",
          "                               ^---- HERE"
        ],

Blockquote

POST test_kris_assignment/_update_by_query
{
    "query": {
       "term": {
           "per_id": "10040298"
       }
  },
  "script": {
	"lang": "painless",
	"source" : """if (ctx._source.car.current_assignment[0].job_description[0].jdl1_id != null && ctx._source.car.current_assignment[0].job_description[0].jdl2_id != null && ctx._source.car.current_assignment[0].job_description[0].jdl3_id != null) 
                                            {def a = [ "domain_type" : ["jdl_id": ctx._source.car.current_assignment[0].job_description[0].jdl1_id, "jdl_descr":  ctx._source.car.current_assignment[0].job_description[0].jdl1_descr] ]
					    ;def scores = [a]
				            ;ctx._source.all2_business_index = null
					    ;ctx._source.all2_business_index.car = []
                                            ;for (int i = 0; i < scores.length; i++) {  ctx._source.all2_business_index.car.add(scores[i])}									
                                            ctx.op = 'index' }										   
 else  { ctx.op = 'noop'}"""
  }
}

Hey,

a fully reproducible example would help a lot here, that can be easily copied & pasted into dev-tools for local reproduction. Also a proper exception and the Elasticsearch version being used are important to mention.

My current asssumption is, that all2_business_index as a field does not exist, and needs to be initialized as a map first.

--Alex

Thank you for the feedback, I've been really struggling with this. I'll put here a test index, a test document on that index, my code and the exception. The Elasticsearch version it's the 7.3.2
I wanted to create a field called "all2_business_idx" that has an array, in this script would create an element called "car" and an element inside car.
To simplify I've put fixed values on jdl_id and jdl_descr, but in fact I take them from the document itself in the real code, so I can't use the params to build this.
The result would be someting like:

Blockquote

"all2_business_idx" :  [
 "car": [
      {
        "domain_type": {
          "jdl_id": 123,
          "jdl_descr": "field_description"
        }
      }
    ]
]

Blockquote

PUT /car
{
"mappings":{
"properties": {
"model" : { "type" : "text" },
"year": {"type" : "integer"},
"engine": {"type": "text"},
"star": {"type": "text"}
}
}
}

Blockquote

POST car/_doc/1
{
"model": "Porshe",
"year": 1972,
"engine": "2.0-liter four-cylinder Macan",
"horsepower": "252hp",
"genres": ["Sporty", "Classic"]
}

Blockquote

POST car2/_update_by_query
{
    "query": {
        "match_all": {}
  },
  "script": {
	"lang": "painless",
	"source" : """def a = [ "domain_type" : ["jdl_id": 123, "jdl_descr":  "field description"] ]
				        ;ctx._source.all2_business_idx = [:]
						;ctx._source.all2_business_idx.car = [:]
						;def scores = [a]
                        ;for (int i = 0; i < scores.length; i++) {  ctx._source.all2_business_idx.car.add(scores[i])}									
                        ctx.op = 'index'"""
}
}

Blockquote

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.all2_business_idx.car.add(scores[i])}\t\t\t\t\t\t\t\t\t\r\n                        ",
          "                                            ^---- HERE"
        ],
        "script": "def a = [ \"domain_type\" : [\"jdl_id\": 123, \"jdl_descr\":  \"field description\"] ]\r\n\t\t\t\t        ;ctx._source.all2_business_idx = [:]\r\n\t\t\t\t\t\t;ctx._source.all2_business_idx.car = [:]\r\n\t\t\t\t\t\t;def scores = [a]\r\n                        ;for (int i = 0; i < scores.length; i++) {  ctx._source.all2_business_idx.car.add(scores[i])}\t\t\t\t\t\t\t\t\t\r\n                        ctx.op = 'index'",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.all2_business_idx.car.add(scores[i])}\t\t\t\t\t\t\t\t\t\r\n                        ",
      "                                            ^---- HERE"
    ],
    "script": "def a = [ \"domain_type\" : [\"jdl_id\": 123, \"jdl_descr\":  \"field description\"] ]\r\n\t\t\t\t        ;ctx._source.all2_business_idx = [:]\r\n\t\t\t\t\t\t;ctx._source.all2_business_idx.car = [:]\r\n\t\t\t\t\t\t;def scores = [a]\r\n                        ;for (int i = 0; i < scores.length; i++) {  ctx._source.all2_business_idx.car.add(scores[i])}\t\t\t\t\t\t\t\t\t\r\n                        ctx.op = 'index'",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "dynamic method [java.util.HashMap, add/1] not found"
    }
  },
  "status": 400
}

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.