Update a field value using a script processor

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

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

Created a pipeline with script processor

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

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

{
       "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"
                         }]}}]}]}

Hey,

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

--Alex

Thanks Alex. It works!

1 Like

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