# Determine Shard Id based on routing key

**URL:** <https://discuss.elastic.co/t/determine-shard-id-based-on-routing-key/19502>\
**Category:** Elasticsearch\
**Created:** [August 28, 2014, 6:31am UTC](https://discuss.elastic.co/t/determine-shard-id-based-on-routing-key/19502 "2014-08-28T06:31:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElasticSearch\_Users\_](https://avatars.discourse-cdn.com/v4/letter/e/b4bc9f/32.png) [@ElasticSearch\_Users\_](https://discuss.elastic.co/u/ElasticSearch_Users_)\
**Post date:** [August 28, 2014, 6:31am UTC](https://discuss.elastic.co/t/determine-shard-id-based-on-routing-key/19502/1 "2014-08-28T06:31:48Z")

</div>

Hi

Say I create an index with 20 shards.

During indexing, if I specify a routing\_key as 0, will it be indexed in  
shardId 0? Will routing\_key 3 correspond to shard Id 3? Similarly for all  
other keys if I have 20 unique routing values since 0 % 20 will be 0 and 3  
% 20 will be 3, etc.

There is no hash but a specific set of routing keys [0..19] = number of  
shards [0..19] that I have.

Is this deterministic and documented by ES regarding this routing key to  
shard Id behavior? Or is it internal to ES and changeable anytime?

Please let me know soon!! Thanks in advance...

Thanks,  
Sandeep

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [August 28, 2014, 7:58am UTC](https://discuss.elastic.co/t/determine-shard-id-based-on-routing-key/19502/2 "2014-08-28T07:58:25Z")

</div>

Hi Sandeep,

Routing is deterministic, otherwise we couldn't know where data is located  
when using the get API (this API goes to a single shard, not all of them).  
However, you should not rely on the distribution of the hash values as this  
is an implementation detail that we could indeed change at some point.

I don't know what your use-case is, but if you really need to manage the  
sharding yourself, the easiest way to do it would be to creates 20 indices  
with 1 shard instead of 1 index with 20 shards. I would discourage to do it  
though.

On Thu, Aug 28, 2014 at 8:31 AM, 'Sandeep Ramesh Khanzode' via  
elasticsearch [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com) wrote:

> Hi
> 
> Say I create an index with 20 shards.
> 
> During indexing, if I specify a routing\_key as 0, will it be indexed in  
> shardId 0? Will routing\_key 3 correspond to shard Id 3? Similarly for all  
> other keys if I have 20 unique routing values since 0 % 20 will be 0 and 3  
> % 20 will be 3, etc.
> 
> There is no hash but a specific set of routing keys [0..19] = number of  
> shards [0..19] that I have.
> 
> Is this deterministic and documented by ES regarding this routing key to  
> shard Id behavior? Or is it internal to ES and changeable anytime?
> 
> Please let me know soon!! Thanks in advance...
> 
> Thanks,  
> Sandeep
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j4rczExhUwa1NSLLeekx%2BHzMFTcoYNrT5hbGeqPJSp-bw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j4rczExhUwa1NSLLeekx%2BHzMFTcoYNrT5hbGeqPJSp-bw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![ElasticSearch\_Users\_](https://avatars.discourse-cdn.com/v4/letter/e/b4bc9f/32.png) [@ElasticSearch\_Users\_](https://discuss.elastic.co/u/ElasticSearch_Users_)\
**Post date:** [September 1, 2014, 11:18am UTC](https://discuss.elastic.co/t/determine-shard-id-based-on-routing-key/19502/3 "2014-09-01T11:18:38Z")

</div>

Hi Adrian,

Thanks for the reply. That was important for me to understand.

However, I am a little concerned with your comment on the equivalence of 1  
index with 20 shards and 20 indices with one shard each. You mentioned that  
you would discourage the latter.

Can you please explain why? Is it for management reasons or performance  
overhead reasons? I can deal with the former but not the latter unless if  
you have some pointers. Thanks,

Thanks,  
Sandeep

On Thursday, 28 August 2014 13:28:32 UTC+5:30, Adrien Grand wrote:

> Hi Sandeep,
> 
> Routing is deterministic, otherwise we couldn't know where data is  
> located when using the get API (this API goes to a single shard, not all of  
> them). However, you should not rely on the distribution of the hash values  
> as this is an implementation detail that we could indeed change at some  
> point.
> 
> I don't know what your use-case is, but if you really need to manage the  
> sharding yourself, the easiest way to do it would be to creates 20 indices  
> with 1 shard instead of 1 index with 20 shards. I would discourage to do it  
> though.
> 
> On Thu, Aug 28, 2014 at 8:31 AM, 'Sandeep Ramesh Khanzode' via  
> elasticsearch \<[elasti...@googlegroups.com](mailto:elasti...@googlegroups.com) \<javascript:\>\> wrote:
> 
> > Hi
> > 
> > Say I create an index with 20 shards.
> > 
> > During indexing, if I specify a routing\_key as 0, will it be indexed in  
> > shardId 0? Will routing\_key 3 correspond to shard Id 3? Similarly for all  
> > other keys if I have 20 unique routing values since 0 % 20 will be 0 and 3  
> > % 20 will be 3, etc.
> > 
> > There is no hash but a specific set of routing keys [0..19] = number of  
> > shards [0..19] that I have.
> > 
> > Is this deterministic and documented by ES regarding this routing key to  
> > shard Id behavior? Or is it internal to ES and changeable anytime?
> > 
> > Please let me know soon!! Thanks in advance...
> > 
> > Thanks,  
> > Sandeep
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/32b16cf4-517e-4b33-9eb8-129cf5bd8cf0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fc1cc1c3-9987-4f51-98f9-0878145e6c66%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fc1cc1c3-9987-4f51-98f9-0878145e6c66%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 1, 2014, 11:22am UTC](https://discuss.elastic.co/t/determine-shard-id-based-on-routing-key/19502/4 "2014-09-01T11:22:40Z")

</div>

On Mon, Sep 1, 2014 at 1:18 PM, 'Sandeep Ramesh Khanzode' via elasticsearch  
[elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com) wrote:

> However, I am a little concerned with your comment on the equivalence of 1  
> index with 20 shards and 20 indices with one shard each. You mentioned that  
> you would discourage the latter.
> 
> Can you please explain why? Is it for management reasons or performance  
> overhead reasons? I can deal with the former but not the latter unless if  
> you have some pointers. Thanks,

Sorry for the confusion, what I would like to discourage is not having 20  
indices with one shard but trying to manage sharding manually instead of  
relying on elasticsearch's routing mechanism that abstracts the number of  
shards.

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5R4-BOtaMkFTDB1PfpVqVrh4BQb%3D4TsAfseOiCFP79Fg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5R4-BOtaMkFTDB1PfpVqVrh4BQb%3D4TsAfseOiCFP79Fg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Sandeep\_Ramesh\_Khanz](https://avatars.discourse-cdn.com/v4/letter/s/4491bb/32.png) [@Sandeep\_Ramesh\_Khanz](https://discuss.elastic.co/u/Sandeep_Ramesh_Khanz)\
**Post date:** [September 10, 2014, 11:55am UTC](https://discuss.elastic.co/t/determine-shard-id-based-on-routing-key/19502/5 "2014-09-10T11:55:06Z")

</div>

Hi Adrien,

Is it possible to intercept the Search Response, on the server itself,  
from the ES Server to the node hosting the TransportClient? I can use a  
plugin to do that, but I have no idea which classes to extend and which  
modules to register. Can you please provide some details? Thanks,

Thanks,  
Sandeep

On Mon, Sep 1, 2014 at 4:52 PM, Adrien Grand \<[adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)

> wrote:

> On Mon, Sep 1, 2014 at 1:18 PM, 'Sandeep Ramesh Khanzode' via  
> elasticsearch [elasticsearch@googlegroups.com](mailto:elasticsearch@googlegroups.com) wrote:
> 
> > However, I am a little concerned with your comment on the equivalence of  
> > 1 index with 20 shards and 20 indices with one shard each. You mentioned  
> > that you would discourage the latter.
> > 
> > Can you please explain why? Is it for management reasons or performance  
> > overhead reasons? I can deal with the former but not the latter unless if  
> > you have some pointers. Thanks,
> 
> Sorry for the confusion, what I would like to discourage is not having 20  
> indices with one shard but trying to manage sharding manually instead of  
> relying on elasticsearch's routing mechanism that abstracts the number of  
> shards.
> 
> --  
> Adrien Grand
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/Hh5uhhb70Mo/unsubscribe](https://groups.google.com/d/topic/elasticsearch/Hh5uhhb70Mo/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5R4-BOtaMkFTDB1PfpVqVrh4BQb%3D4TsAfseOiCFP79Fg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5R4-BOtaMkFTDB1PfpVqVrh4BQb%3D4TsAfseOiCFP79Fg%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5R4-BOtaMkFTDB1PfpVqVrh4BQb%3D4TsAfseOiCFP79Fg%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5R4-BOtaMkFTDB1PfpVqVrh4BQb%3D4TsAfseOiCFP79Fg%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKnM90ZCbT%2Bf2hS0dR7YHP6mkdVU4%3DutNNTPBkVQnT6HYgDf\_g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKnM90ZCbT%2Bf2hS0dR7YHP6mkdVU4%3DutNNTPBkVQnT6HYgDf_g%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:03am UTC](https://discuss.elastic.co/t/determine-shard-id-based-on-routing-key/19502/6 "2017-07-06T01:03:21Z")

</div>


