# Finding optimum number of shards for custom routing

**URL:** <https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606>\
**Category:** Elasticsearch\
**Created:** [July 11, 2018, 3:11pm UTC](https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606 "2018-07-11T15:11:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)\
**Post date:** [July 11, 2018, 3:11pm UTC](https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606/1 "2018-07-11T15:11:49Z")

</div>

Hi,  
I am trying to find optimum number of shards for my data.  
I have custom routing value in my application.

I know the formula: hash(routing) % num of primary shards.

**Question** :  
Can anyone point me to the hashing function above ? Is there a utility or source code which i can use ?

---

<div class="post-metadata">

**Author:** ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)\
**Post date:** [July 12, 2018, 5:35am UTC](https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606/2 "2018-07-12T05:35:28Z")

</div>

I found it out myself from the ES code.

Anyone who is looking for the hash function please use this from ES codebase:

> Math.floorMod(Murmur3HashFunction.hash(\_routing), numberOfShards)

This output a number which is a shard number to which your \_routing value will be allocated.

I hope this saves someones day, especially if they don't get replies on this forum.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [July 12, 2018, 5:57am UTC](https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606/3 "2018-07-12T05:57:40Z")

</div>

What are you going to use this for?

---

<div class="post-metadata">

**Author:** ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)\
**Post date:** [July 12, 2018, 6:24am UTC](https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606/4 "2018-07-12T06:24:49Z")

</div>

To decide the number of primary shards for my use case. I am using custom \_routing.  
I need to choose a number of primary shards so that all my content is evenly distributed across shards.

My current issue: when i am indexing data using multiple threads and all my data which has same \_routing, is getting shoved into the same shard, i am loosing data.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [July 12, 2018, 7:08am UTC](https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606/5 "2018-07-12T07:08:01Z")

</div>

What are you using routing for. Is it based on a low cardinality field?

---

<div class="post-metadata">

**Author:** ![pokaleshrey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pokaleshrey/32/48680_2.png) [@pokaleshrey](https://discuss.elastic.co/u/pokaleshrey)\
**Post date:** [July 12, 2018, 7:16am UTC](https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606/6 "2018-07-12T07:16:54Z")

</div>

I have data divided into several groups. Each group has a two character identifier. My \_routing is not based on any field in my document.  
I expect each group to be stored on a single shard.

Now my two character identifier serves as my "\_routing" while indexing.  
At search time i know my identifier, so it is easy to search.

I expect my groups to be evenly divided across my shards. Hence i need to decided optimum number of primary shards which will be useful while indexing and also beneficial in searching.

---

<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:** [August 9, 2018, 7:17am UTC](https://discuss.elastic.co/t/finding-optimum-number-of-shards-for-custom-routing/139606/7 "2018-08-09T07:17:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
