# Painless script to set Nested Fields value

**URL:** https://discuss.elastic.co/t/painless-script-to-set-nested-fields-value/85217
**Category:** Elasticsearch
**Created:** [May 10, 2017, 9:33am UTC](https://discuss.elastic.co/t/painless-script-to-set-nested-fields-value/85217 "2017-05-10T09:33:01Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Abhay](https://avatars.discourse-cdn.com/v4/letter/a/b77776/32.png) [@Abhay](https://discuss.elastic.co/u/Abhay)
#### Post date: [May 10, 2017, 9:33am UTC](https://discuss.elastic.co/t/painless-script-to-set-nested-fields-value/85217/1 "2017-05-10T09:33:01Z")

</div>

I am having data mapping as below -

```
   {
    "mappings": {
		"data": {
			"properties": {
                "attributeNames": {
                  "type": "string"
               },
                "attributes": {
                    "type": "nested",
                    "include_in_parent": true,
                    "properties": {
                        "mc_name": {
        				"type":"string"
    },
                    "mc_original_severity": {
                        "type": "string",
                        "index": "not_analyzed",
                        "fields": {
                           "converted": {
                              "type": "string",
                              "index": "not_analyzed"
                           }
                        }
                },
                    "mc_score": {
                        "type": "integer"  
                    }
                    }    
    			}
			}
		},
	}
}

```

I am writing a Script Processor for ingest plugin. I have to write a painless script to put the value in "mc\_original\_severity.converted " based on value in "mc\_original\_severity".  
Previously we are using Elasticsearch 1.7 and in that we were using Groovy script in "transform" field in mapping.

> `if (ctx._source.attributes.mc_original_severity?.equals('CRITICAL')) {ctx._source['mc_original_severity.converted'] = '1';}`

This script was giving us the result.

But now I am using Painless script -

> `if (ctx._type.equals('data')) { if (ctx.mc_original_severity.equals('MODERATE')) {ctx._source['mc_original_severity.converted'] = '0';}}`

And I am getting below error -

> ```
> "error": {
> "type": "exception",
> "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
> "caused_by": {
> "type": "illegal_argument_exception",
> "reason": "ScriptException[runtime error]; nested: NullPointerException;",
> "caused_by": {
> "type": "script_exception",
> "reason": "runtime error",
> "caused_by": {
> "type": "null_pointer_exception",
> "reason": null
> },
> "script_stack": [
> "ctx._source['mc_original_severity.converted'] = '0';}else ",
> " ^---- HERE"
> ],
> "script": "if (ctx._type.equals('data')) { if (ctx.mc_original_severity.equals('MODERATE')) {ctx._source['mc_original_severity.converted'] = '0';}else if(ctx.mc_original_severity.equals('CRITICAL')){ctx._source['mc_original_severity.converted'] = '1';} }",
> "lang": "painless"
> }
> 
> ```

Can anyone guide me on how to achieve it using painless script.

---

<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 7, 2017, 9:37am UTC](https://discuss.elastic.co/t/painless-script-to-set-nested-fields-value/85217/2 "2017-06-07T09:37:48Z")

</div>

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