Elasticsearch rename nested fields using reindex

Trying to rename the existing fields using reindex api
Works well for non-nested fields:

POST _reindex
{
  "source": {
    "index": "products-2018.04.05"
  },
  "dest": {
    "index": "products-reindex-2018.04.05"
  },
  "script": {
    "inline": "ctx._source.pid = ctx._source.remove(\"ProductID\")"
  }
}

But, when I tried on nested fields, its not working:

 POST _reindex
    {
      "source": {
        "index": "products-2018.04.05"
      },
      "dest": {
        "index": "products-reindex-2018.04.05"
      },
      "script": {
        "inline": "ctx._source.PRODUCT.RPT.VALUEAVG = ctx._source.remove(\"PRODUCT.RPT.VALUE.AVG\")"
      }
    }

Error:

{
    "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "ctx._source.PRODUCT.RPT.VALUEAVG = ctx._source.remove(\"PRODUCT.RPT.VALUE.AVG\")",
          "              ^---- HERE"
        ],
        "script": "ctx._source.PRODUCT.RPT.VALUEAVG = ctx._source.remove(\"PRODUCT.RPT.VALUE.AVG\")",
        "lang": "painless",
        "caused_by": {
          "type": "null_pointer_exception",
          "reason": null
        }
}

Mapping:

PRODUCT.RPT.VALUEAVG --> text and not-analyzed

I tried different ways, nothing worked:

ctx._source.'PRODUCT.RPT.VALUEAVG'
ctx._source.\"PRODUCT.RPT.VALUEAVG"
ctx._source.[PRODUCT][RPT][VALUEAVG]

Hi , anyone with solution to this problem.
Any help appreciated !!

This syntax should work:

POST _reindex
{
  "source": {
    "index": "products-2018.04.05"
  },
  "dest": {
    "index": "products-reindex-2018.04.05"
  },
  "script": {
    "inline": """ctx._source['PRODUCT.RPT.VALUEAVG'] = ctx._source.remove("PRODUCT.RPT.VALUE.AVG")"""
  }
}
2 Likes

Worked like a charm !! Thank you :grinning:

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