# Question about shard routing

**URL:** <https://discuss.elastic.co/t/question-about-shard-routing/10017>\
**Category:** Elasticsearch\
**Created:** [December 10, 2012, 1:03pm UTC](https://discuss.elastic.co/t/question-about-shard-routing/10017 "2012-12-10T13:03:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konstantin\_Nikiforov](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@Konstantin\_Nikiforov](https://discuss.elastic.co/u/Konstantin_Nikiforov)\
**Post date:** [December 10, 2012, 1:03pm UTC](https://discuss.elastic.co/t/question-about-shard-routing/10017/1 "2012-12-10T13:03:20Z")

</div>

1. I have index with (for example) 3 shards with "\_routing":{"required" :  
true}  
I'm using year and month as routing keys. For example: "2012.01",  
"2001.08", etc.  
Is it possible to ensure, that all routing keys "2012xx" are sent to  
shard#1, and "2011xx" sent to shard#2, etc? Or they will be mixed somehow?  
What is behaviour of ES when number\_of\_shards much less than number of  
routing keys?

2. I have 20 shards and 20 routing keys: "2012", "2011", "2010", etc. (no  
replicas)  
How can i ensure, that each shard relates to _only\_one_ routing key? (There  
are no shard with two routing keys?).

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 11, 2012, 12:32am UTC](https://discuss.elastic.co/t/question-about-shard-routing/10017/2 "2012-12-11T00:32:03Z")

</div>

In order to find which shard a document belongs to Elasticsearch is using  
the following formula:

```
 hash(routing) % numberOfShards

```

where

- routing is type id or the routing key if it's explicitly specified
- hash() - hash function
- numberOfShards - the number of shards in the current index.

In other words, Elasticsearch is trying to distribute records evenly across  
all shards and records with the same routing key will go the same shards.  
That's pretty much all you can expect.

If you want more control over record distribution I would suggest creating  
multiple indices so you could fully control record allocation. By wrapping  
all these indices into an alias, you can hide routing logic from the rest  
of the application.

On Monday, December 10, 2012 8:03:20 AM UTC-5, Konstantin Nikiforov wrote:

> 1. I have index with (for example) 3 shards with "\_routing":{"required" :  
> true}  
> I'm using year and month as routing keys. For example: "2012.01",  
> "2001.08", etc.  
> Is it possible to ensure, that all routing keys "2012xx" are sent to  
> shard#1, and "2011xx" sent to shard#2, etc? Or they will be mixed somehow?  
> What is behaviour of ES when number\_of\_shards much less than number of  
> routing keys?
> 
> 2. I have 20 shards and 20 routing keys: "2012", "2011", "2010", etc. (no  
> replicas)  
> How can i ensure, that each shard relates to _only\_one_ routing key?  
> (There are no shard with two routing keys?).

--

---

<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:** [July 6, 2017, 3:00am UTC](https://discuss.elastic.co/t/question-about-shard-routing/10017/3 "2017-07-06T03:00:27Z")

</div>


