Sahil Gupta
Sahil Gupta

Reputation: 2176

How to update routingId for set of documents

Context:

  1. Our application do have accountId and subaccountIds
  2. We do set routingId = accountId, so that all the data of an account resides in a shard
  3. There are cases where the accountId need to be changed.

Question:

Is there any way to update the routingId of existing docs in ES ?

I do understand that we can persist old routingId to get the data. But is there any other way ?

Upvotes: 1

Views: 520

Answers (1)

Val
Val

Reputation: 217574

If you try to modify the _routing value during an update by query you're going to get the following error:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Modifying [_routing] not allowed"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Modifying [_routing] not allowed"
  },
  "status" : 400
}

Modifying the routing of a given document has the effect of potentially moving that document to a new shard and this is not allowed. Also you'd end up with two documents having the same ID in the same index, just located on two different shards.

The only way you can achieve what you need, while keeping a consistent index, is to

  1. first delete the document with the old routing value
  2. and then reindex it with the new routing value.

Upvotes: 1

Related Questions