# Custom routing, how to prepare new custom routing formula? or get to know the hashing in detail

**URL:** <https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544>\
**Category:** Elasticsearch\
**Created:** [January 16, 2019, 9:14pm UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544 "2019-01-16T21:14:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [January 16, 2019, 9:14pm UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/1 "2019-01-16T21:14:15Z")

</div>

In my data model i store data based on language, so i am thinking to distribute documents based on one language per shard. Let say if i have product with id 10, and have fields for product like Label, cost or color  
So i store data, language wise like 10\_1, 10\_2,10\_3, .....etc here 1,2,3 are language ids.

10\_1=\> Label : english\_label, color : color of the product in english lang.,  
10\_2=\> Label : japanese\_label, color : color of the product in japanese\_lang

So my concern here currently elastic use routing formula is  
shard no. = hash(routing) % no. of primary shards  
then in my case routing = \_id =10\_1 like that.  
So please help me to find out unique formula to send all same language products to single shard. Because no matter what you do, hash() function internally changes the final value.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 17, 2019, 12:19am UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/2 "2019-01-17T00:19:21Z")

</div>

> [@kshi\_yelpale000](#):
>
> In my data model i store data based on language, so i am thinking to distribute documents based on one language per shard

That's not a great idea, because language use is lumpy. A lot of people speak english (either natively or as an alternate language), so your shard for that will be huge.  
Not many people speak australian, so that shard would be small.

And then how do you manage shards when you want to add/remove languages? Do you just start a single index with 500 shards and hope you use them all? (Note, no, you should never do that, it's a huge waste).

So the question is, what value do you see having everything from the same language in the same shard?

---

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [January 17, 2019, 8:42am UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/3 "2019-01-17T08:42:47Z")

</div>

Hi Mark,

Yes i can understand your point regarding wastage size or having poor architecture, but here are the answers to points and questions

> [@warkolm](#):
>
> Not many people speak australian, so that shard would be small.

i ensure that all language will have the data for every product.

> [@warkolm](#):
>
> And then how do you manage shards when you want to add/remove languages

on addition of language of i'll add that language's data in to any one existing shard, obviously i'll write logic that if no. of languages are more than no. of shards by particular percent then i would reindex data by increasing shards (again shard per language) . no Elasticsearch' actual reindex but yes similar kind of that.

> [@warkolm](#):
>
> So the question is, what value do you see having everything from the same language in the same shard?

well here i see all data of that product for that language, i come with approach that initially elastic node broadcast to all shards and then gather all data and then perform other operations like sorting, relevance score and all that, so instead of that i'll tell elastic to go on particular shard so that time and operations at elastic will be saved.

Now, i hope you get the idea.  
So do you know how to improve routing mechanism, please let me know

Thanks and regards  
Kshitij.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 17, 2019, 9:25am UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/4 "2019-01-17T09:25:31Z")

</div>

> [@kshi\_yelpale000](#):
>
> i'll tell elastic to go on particular shard so that time and operations at elastic will be saved.

How much time will you save? Have you tested it?

---

<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:** [January 17, 2019, 9:44am UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/5 "2019-01-17T09:44:03Z")

</div>

Routing ensures that all data related to a specific routing key ends up in the same shard, but it does not make a specific shard hold data for just a single routing key. If this is what you are looking for you MAY be better off having multiple indices with a single primary shard each.

---

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [January 22, 2019, 7:25pm UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/6 "2019-01-22T19:25:59Z")

</div>

Hi Mark,

No i have not tested it because currently i am not able to distribute data as per language and one shard consist more than one language data. It will be really great if you tell me to do like this, either some value for "routing" or can we design our own formula.

Bye and thanks

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 22, 2019, 10:25pm UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/7 "2019-01-22T22:25:02Z")

</div>

I don't think there is a way to do this and be sure that all shards are used.

---

<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:** [January 23, 2019, 3:22am UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/8 "2019-01-23T03:22:09Z")

</div>

Did you consider my earlier comment?

---

<div class="post-metadata">

**Author:** ![kshi\_yelpale000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kshi_yelpale000/32/42759_2.png) [@kshi\_yelpale000](https://discuss.elastic.co/u/kshi_yelpale000)\
**Post date:** [January 23, 2019, 11:57am UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/9 "2019-01-23T11:57:10Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> es not make a specific shard hold data for just a single routing key. If this is what you are looking for you MAY be better off having multiple indices with a single primary shard each.

Hi Christian,

Yes I have considered your comment and thinking on it and will propose this to my team members and discuss, but before that thinking to change the formula some way.

Thanks for reply

---

<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:** [February 20, 2019, 11:57am UTC](https://discuss.elastic.co/t/custom-routing-how-to-prepare-new-custom-routing-formula-or-get-to-know-the-hashing-in-detail/164544/10 "2019-02-20T11:57:13Z")

</div>

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