Painless script to set Nested Fields value


(Abhay Verma) #1

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.


(system) #2

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