# How to change a documents \_id during a reindex

**URL:** <https://discuss.elastic.co/t/how-to-change-a-documents--id-during-a-reindex/76132>\
**Category:** Elasticsearch\
**Created:** [February 23, 2017, 1:48am UTC](https://discuss.elastic.co/t/how-to-change-a-documents--id-during-a-reindex/76132 "2017-02-23T01:48:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Unger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastian_unger/32/16829_2.png) [@Sebastian\_Unger](https://discuss.elastic.co/u/Sebastian_Unger)\
**Post date:** [February 23, 2017, 1:48am UTC](https://discuss.elastic.co/t/how-to-change-a-documents--id-during-a-reindex/76132/1 "2017-02-23T01:48:00Z")

</div>

Hi,

according to [the docs](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/docs-reindex.html) 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

---

<div class="post-metadata">

**Author:** ![jkuang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jkuang/32/72637_2.png) [@jkuang](https://discuss.elastic.co/u/jkuang)\
**Post date:** [February 24, 2017, 11:23pm UTC](https://discuss.elastic.co/t/how-to-change-a-documents--id-during-a-reindex/76132/2 "2017-02-24T23:23:35Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 24, 2017, 11:23pm UTC](https://discuss.elastic.co/t/how-to-change-a-documents--id-during-a-reindex/76132/3 "2017-03-24T23:23:38Z")

</div>

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