# How to add two new fields to a document

**URL:** https://discuss.elastic.co/t/how-to-add-two-new-fields-to-a-document/249259
**Category:** Elasticsearch
**Tags:** painless
**Created:** [September 20, 2020, 2:47pm UTC](https://discuss.elastic.co/t/how-to-add-two-new-fields-to-a-document/249259 "2020-09-20T14:47:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Rui\_Goncalves](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rui_goncalves/32/72197_2.png) [@Rui\_Goncalves](https://discuss.elastic.co/u/Rui_Goncalves)
#### Post date: [September 20, 2020, 2:47pm UTC](https://discuss.elastic.co/t/how-to-add-two-new-fields-to-a-document/249259/1 "2020-09-20T14:47:56Z")

</div>

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

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

```

> Blockquote

```auto
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'}"""
  }
}

```

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [September 21, 2020, 11:48am UTC](https://discuss.elastic.co/t/how-to-add-two-new-fields-to-a-document/249259/2 "2020-09-21T11:48:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Rui\_Goncalves](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rui_goncalves/32/72197_2.png) [@Rui\_Goncalves](https://discuss.elastic.co/u/Rui_Goncalves)
#### Post date: [September 21, 2020, 2:16pm UTC](https://discuss.elastic.co/t/how-to-add-two-new-fields-to-a-document/249259/3 "2020-09-21T14:16:37Z")

</div>

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

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

```

> Blockquote

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

```

> Blockquote

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

```

> Blockquote

```auto
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

```auto
{
  "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
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 19, 2020, 2:16pm UTC](https://discuss.elastic.co/t/how-to-add-two-new-fields-to-a-document/249259/4 "2020-10-19T14:16:52Z")

</div>

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