0

I have an index which uses the default routing of elasticsearch. Now I want to set up my own routing, how do I implement this?

The example in the official doc is:

$ curl -XPUT 'http://localhost:9200/store/order/_mapping' -d '
{
   "order":{
      "_routing":{
         "required":true,
         "path":"customerID"
      }
   }
}

After doing this, do I need to reindex of even rebuild the whole index?

  • Actually the doc says "WARNING: extracting custom routing from the document is no longer supported; it was removed in Elasticsearch v2.0." – Thomas Decaux Dec 05 '16 at 15:40

2 Answers2

0

If you want to change the routing behavior in an existing mapping type, you need to create a new index, recreate the mapping type and re-index your data. I don't think it's even possible to change the _routing on an existing mapping type. Even if you see acknowledged: true, the _routing part will probably have been ignored altogether. You can execute your update mapping command above, and then check the mapping to see if your mapping type has the new _routing, but I'm pretty sure it won't.

The reason has to do with how the routing works. If it was possible to update the routing dynamically, you could end up with the same document being on two different shards of the same index. The first time you indexed the document (without routing), the document might have ended up on shard1, then then second time you index the document (with routing), the same document might end up on shard3. This means you'd have a duplicate document in your index, which is probably not the desired behavior.

Since it's easy to create new indices and mapping types, whenever in doubt, just create a brand new index from scratch and re-index your data. You'll lose less time figuring out why your query returns weird results.

Val
  • 207,596
  • 13
  • 358
  • 360
  • Thanks for your detailed answer! –  Dec 09 '15 at 14:54
  • Hello @val, how can you use reindex API with a custom routing in this way? cheers – Thomas Decaux Dec 05 '16 at 15:39
  • @ThomasDecaux Yes, you can, as [described in the official documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html) – Val Dec 07 '16 at 05:09
  • I am not sure, routing in mapping is no more supported, reindex says it preserves routing, unless you change it by script. – Thomas Decaux Dec 07 '16 at 09:29
  • @ThomasDecaux I suggest you create a new question and explain what your issue is, you'll have more chance to get a better answer. This one was not specifically about the reindex API ;-) – Val Dec 07 '16 at 10:31
  • Is not an issue, your answer is wrong, because ES 2.0 drop support for routing in mapping. – Thomas Decaux Dec 08 '16 at 10:18
  • @ThomasDecaux it was correct in the context of 1.x, but not adapted to ES 2.0. – Val Dec 08 '16 at 18:21
0

Extracting custom routing from the document is no longer supported after elasticsearch 2.0 onwards. https://github.com/elastic/elasticsearch/pull/11074 If you want to add routing to your existing index then you might be required to do something like this. Iterate over all the customerIDs and use _reindex api to reindex.

POST _reindex
{
  "source": {
    "index": "source",
    "query": {
      "match": {
        "customerIDs": "customerid1"
      }
    }
  },
  "dest": {
    "index": "dest",
    "routing": "customerid1"
  }
}
Paul Weiland
  • 727
  • 10
  • 24