# Update a field value using a script processor

**URL:** https://discuss.elastic.co/t/update-a-field-value-using-a-script-processor/179766
**Category:** Elasticsearch
**Created:** [May 6, 2019, 12:21pm UTC](https://discuss.elastic.co/t/update-a-field-value-using-a-script-processor/179766 "2019-05-06T12:21:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sreed](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@sreed](https://discuss.elastic.co/u/sreed)
#### Post date: [May 6, 2019, 12:21pm UTC](https://discuss.elastic.co/t/update-a-field-value-using-a-script-processor/179766/1 "2019-05-06T12:21:47Z")

</div>

**My use-case** : Before the document is indexed, I would like to update one of the fields using a script processor (defined in a pipeline). When I try to create a index using the pipeline with the script processor a null-pointer exception is thrown. Any pointers would be of great help.

Created a index

```auto
curl -v -H "Content-Type:application/json" -X PUT http://localhost:9200/students

```

Created a pipeline with script processor

```auto
curl -X PUT "localhost:9200/_ingest/pipeline/my-scriptpipeline" -H 'Content-Type: application/json' -d'
{
       "description" : "script pipeline",
      "processors" : [
         {
        		"script": {
        			"source": "ctx._source.age += params.extra",
       			"params": {
        			  "extra": 2
       			}
        		}}]}| json_pp

```

Simulated the pipeline

```auto
curl -X POST "localhost:9200/_ingest/pipeline/my-scriptpipeline/_simulate?verbose" -H 'Content-Type: application/json' -d'
    {
     "docs": [
    	{
   		"_index": "students",
    		"_type": "student",
   		"_id": "1",
   		"_source": {
   			"name":"Chota Bheem", 
   			"age":10,
   			"address":"Dholakpur", 
   	  }}]}' | json_pp

```

I get a null-pointer exception

```auto
{
       "docs" : [
          {
             "processor_results" : [
                {
                   "error" : {
                      "script" : "ctx._source.age += params.extra",
                      "reason" : "runtime error",
                      "lang" : "painless",
                      "script_stack" : [
                         "ctx._source.age += params.extra",
                         " ^---- HERE"
                      ],
                      "caused_by" : {
                         "reason" : null,
                         "type" : "null_pointer_exception"
                      },
                      "type" : "script_exception",
                      "root_cause" : [
                         {
                            "reason" : "runtime error",
                            "script" : "ctx._source.age += params.extra",
                            "script_stack" : [
                               "ctx._source.age += params.extra",
                               " ^---- HERE"
                            ],
                            "type" : "script_exception",
                            "lang" : "painless"
                         }]}}]}]}

```

---

<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: [May 6, 2019, 2:11pm UTC](https://discuss.elastic.co/t/update-a-field-value-using-a-script-processor/179766/2 "2019-05-06T14:11:00Z")

</div>

Hey,

no need to use `ctx._source.age`, just try to `ctx.age`.

--Alex

---

<div class="post-metadata">

### Author: ![sreed](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@sreed](https://discuss.elastic.co/u/sreed)
#### Post date: [May 7, 2019, 4:25am UTC](https://discuss.elastic.co/t/update-a-field-value-using-a-script-processor/179766/3 "2019-05-07T04:25:39Z")

</div>

Thanks Alex. It works!

---

<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: [June 4, 2019, 4:25am UTC](https://discuss.elastic.co/t/update-a-field-value-using-a-script-processor/179766/4 "2019-06-04T04:25:51Z")

</div>

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