# Redistribute documents with custom routing and reindex API

**URL:** <https://discuss.elastic.co/t/redistribute-documents-with-custom-routing-and-reindex-api/68059>\
**Category:** Elasticsearch\
**Created:** [December 5, 2016, 3:45pm UTC](https://discuss.elastic.co/t/redistribute-documents-with-custom-routing-and-reindex-api/68059 "2016-12-05T15:45:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [December 5, 2016, 3:45pm UTC](https://discuss.elastic.co/t/redistribute-documents-with-custom-routing-and-reindex-api/68059/1 "2016-12-05T15:45:05Z")

</div>

I have an index with 3 shards, with default routing.

Now, I want to use a custom route (let says field "userID").

How can I use \_reindex API to achieve this?

Thanks you,

PS: I saw there is a routing options for reindex API, but it seems you cannot specify document field in it ;/

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 5, 2016, 4:21pm UTC](https://discuss.elastic.co/t/redistribute-documents-with-custom-routing-and-reindex-api/68059/2 "2016-12-05T16:21:29Z")

</div>

You can do it with scripts. It'd look something like:

```auto
POST _reindex
{
  "source": {
    "index": "old"
  },
  "dest": {
    "index": "new"
  },
  "script": {
    "inline": "ctx._routing = doc.whatever.value"
  }
}

```

---

<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:** [January 2, 2017, 4:21pm UTC](https://discuss.elastic.co/t/redistribute-documents-with-custom-routing-and-reindex-api/68059/3 "2017-01-02T16:21:52Z")

</div>

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