Having an issue while trying to reindex using a script

Hello,

So, I'm trying to reindex documents into a new index with a stored script. the used script has a parameter called "increment". when I start reindexing, I get a null pointer exception.

This is my code.

POST _scripts/control_reindex_batch
{
  "script": {
    "lang": "painless",
    "source": """
      if (ctx._source.reindexBatch == null){
        ctx._source.reindexBatch += params.increment;
      }
      else{
        ctx._source.reindexBatch = 1;
      }
    """
  }
}

POST _reindex
{
  "source": {
    "index": "hamlet"
  },
  "dest": {
    "index": "hamlet-new"
  },
  "script": {
    "id": "control_reindex_batch",
    "params": {
      "increment": 1
    }
  }
}

I get the following error message:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.reindexBatch += params.increment;\n      }\n      else{\n        ",
          "                                  ^---- HERE"
        ],
        "script": "control_reindex_batch",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
      "ctx._source.reindexBatch += params.increment;\n      }\n      else{\n        ",
      "                                  ^---- HERE"
    ],
    "script": "control_reindex_batch",
    "lang": "painless",
    "caused_by": {
      "type": "null_pointer_exception",
      "reason": null
    }
  },
  "status": 400
}

I forgot to mention that I'm using elasticsearch v7.2.1.

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