How to change a documents _id during a reindex

Hi,

according to the docs it is possible to change the _id of a document using a script with the reindex API. But if I

POST /_reindex
{
  "source": {
    "remote": {
      "host": "<old-host>:9200"
    },
    "index": "buildserver",
    "type": "build",
    "_source": true
  },
  "dest": {
    "index": "buildserver",
    "version_type": "internal"
  },
  "script": {
    "lang": "painless",
    "inline": "ctx._source._id = ctx._source['Branch'] + '-' + ctx._source['BuildNumber']"
  }
}

I get the following error:

{
  "index" : "buildserver",
  "type" : "build",
  "id" : "AViZMdskdMcAfTnlyvdG",
  "cause" : {
    "type" : "mapper_parsing_exception",
    "reason" : "Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters."
  },
  "status" : 400
}

Am I doing anything wrong or is the documentation wrong? Most importantly: Is there a way to change the _id field?

Cheers,
Seb

1 Like

The _id does not belong within the _source. Please see my example below:

  "script": {
    "inline": "ctx._id= ctx._source['Branch'] + '-' + ctx._source['BuildNumber']",
    "lang": "painless"
  }
3 Likes

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