问题
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?
回答1:
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.
回答2:
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"
}
}
来源:https://stackoverflow.com/questions/34170897/how-to-set-up-elasticsearch-routing-on-existing-index