# How are the keys sorted in ES?

**URL:** <https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146>\
**Category:** Elasticsearch\
**Created:** [March 27, 2012, 5:24pm UTC](https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146 "2012-03-27T17:24:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [March 27, 2012, 5:24pm UTC](https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146/1 "2012-03-27T17:24:39Z")

</div>

Hi,  
I am rather new to ES, so this might be a very simple question.

I played with HBase for quite a while, and I know that HBase stores  
keys lexicographically.  
So generating keys in time series is not recommended. Because when a  
query is based on a time range, this kind of key usage leads to users  
accessing only a couple of nodes, since the keys are not distributed  
out to all the nodes evenly.

I am worried that the same situation might happen with ES. I am  
currently storing documents that has time-based id's (such as  
20120327\_23\_20471029471097097)  
Should I change the document id's so that documents will be stored  
evenly across all the nodes?

Thanks,  
Ed

---

<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:** [March 27, 2012, 6:15pm UTC](https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146/2 "2012-03-27T18:15:09Z")

</div>

By default, elasticsearch is using hash of the document id. So, you should  
be fine. In elasticsearch you can control this process by specifying  
"routing" parameter with your request. See Routing section of this  
page [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html) for more  
details.

On Tuesday, March 27, 2012 1:24:39 PM UTC-4, mp2893 wrote:

> Hi,  
> I am rather new to ES, so this might be a very simple question.
> 
> I played with HBase for quite a while, and I know that HBase stores  
> keys lexicographically.  
> So generating keys in time series is not recommended. Because when a  
> query is based on a time range, this kind of key usage leads to users  
> accessing only a couple of nodes, since the keys are not distributed  
> out to all the nodes evenly.
> 
> I am worried that the same situation might happen with ES. I am  
> currently storing documents that has time-based id's (such as  
> 20120327\_23\_20471029471097097)  
> Should I change the document id's so that documents will be stored  
> evenly across all the nodes?
> 
> Thanks,  
> Ed

---

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [March 28, 2012, 1:50pm UTC](https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146/3 "2012-03-28T13:50:59Z")

</div>

Thanks Igor, for the quick reply.  
Routing section was just what I needed to read.

Regards,  
Ed

2012/3/28 Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com)

> By default, elasticsearch is using hash of the document id. So, you should  
> be fine. In elasticsearch you can control this process by specifying  
> "routing" parameter with your request. See Routing section of this page  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html) for more  
> details.
> 
> On Tuesday, March 27, 2012 1:24:39 PM UTC-4, mp2893 wrote:
> 
> > Hi,  
> > I am rather new to ES, so this might be a very simple question.
> > 
> > I played with HBase for quite a while, and I know that HBase stores  
> > keys lexicographically.  
> > So generating keys in time series is not recommended. Because when a  
> > query is based on a time range, this kind of key usage leads to users  
> > accessing only a couple of nodes, since the keys are not distributed  
> > out to all the nodes evenly.
> > 
> > I am worried that the same situation might happen with ES. I am  
> > currently storing documents that has time-based id's (such as  
> > 20120327\_23\_20471029471097097)  
> > Should I change the document id's so that documents will be stored  
> > evenly across all the nodes?
> > 
> > Thanks,  
> > Ed

---

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [March 28, 2012, 1:54pm UTC](https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146/4 "2012-03-28T13:54:51Z")

</div>

One more question,

Since ES is hashing the document id, is there a limit to the number of  
documents I can store?  
Or is ES using some kind of infinitely expanding hashing algorithm?

2012/3/28 edward choi [mp2893@gmail.com](mailto:mp2893@gmail.com)

> Thanks Igor, for the quick reply.  
> Routing section was just what I needed to read.
> 
> Regards,  
> Ed
> 
> 2012/3/28 Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com)
> 
> > By default, elasticsearch is using hash of the document id. So, you  
> > should be fine. In elasticsearch you can control this process by specifying  
> > "routing" parameter with your request. See Routing section of this page  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html) for more  
> > details.
> > 
> > On Tuesday, March 27, 2012 1:24:39 PM UTC-4, mp2893 wrote:
> > 
> > > Hi,  
> > > I am rather new to ES, so this might be a very simple question.
> > > 
> > > I played with HBase for quite a while, and I know that HBase stores  
> > > keys lexicographically.  
> > > So generating keys in time series is not recommended. Because when a  
> > > query is based on a time range, this kind of key usage leads to users  
> > > accessing only a couple of nodes, since the keys are not distributed  
> > > out to all the nodes evenly.
> > > 
> > > I am worried that the same situation might happen with ES. I am  
> > > currently storing documents that has time-based id's (such as  
> > > 20120327\_23\_20471029471097097)  
> > > Should I change the document id's so that documents will be stored  
> > > evenly across all the nodes?
> > > 
> > > Thanks,  
> > > Ed

---

<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:** [March 28, 2012, 2:22pm UTC](https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146/5 "2012-03-28T14:22:09Z")

</div>

Hash of id is only used to determine to which shard the document should be  
assigned. During indexing, elasticsearch assigns each document to the shard  
with the number Math.abs(hash(id) % numberOfShards). You can specify the  
number of shards during index creation, but it cannot be changed  
afterwards.

Shay is talking about it in some details in this video

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

starting at 25:24.

On Wednesday, March 28, 2012 9:54:51 AM UTC-4, mp2893 wrote:

> One more question,
> 
> Since ES is hashing the document id, is there a limit to the number of  
> documents I can store?  
> Or is ES using some kind of infinitely expanding hashing algorithm?
> 
> 2012/3/28 edward choi [mp2893@gmail.com](mailto:mp2893@gmail.com)
> 
> > Thanks Igor, for the quick reply.  
> > Routing section was just what I needed to read.
> > 
> > Regards,  
> > Ed
> > 
> > 2012/3/28 Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com)
> > 
> > > By default, elasticsearch is using hash of the document id. So, you  
> > > should be fine. In elasticsearch you can control this process by specifying  
> > > "routing" parameter with your request. See Routing section of this page  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/index_.html) for more  
> > > details.
> > > 
> > > On Tuesday, March 27, 2012 1:24:39 PM UTC-4, mp2893 wrote:
> > > 
> > > > Hi,  
> > > > I am rather new to ES, so this might be a very simple question.
> > > > 
> > > > I played with HBase for quite a while, and I know that HBase stores  
> > > > keys lexicographically.  
> > > > So generating keys in time series is not recommended. Because when a  
> > > > query is based on a time range, this kind of key usage leads to users  
> > > > accessing only a couple of nodes, since the keys are not distributed  
> > > > out to all the nodes evenly.
> > > > 
> > > > I am worried that the same situation might happen with ES. I am  
> > > > currently storing documents that has time-based id's (such as  
> > > > 20120327\_23\_20471029471097097)  
> > > > Should I change the document id's so that documents will be stored  
> > > > evenly across all the nodes?
> > > > 
> > > > Thanks,  
> > > > Ed

---

<div class="post-metadata">

**Author:** ![mp2893](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mp2893](https://discuss.elastic.co/u/mp2893)\
**Post date:** [March 29, 2012, 11:52pm UTC](https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146/6 "2012-03-29T23:52:42Z")

</div>

Thanks again for the info.  
I will definitely check out the video!!

Regards,  
Ed

2012/3/28 Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com)

> Hash of id is only used to determine to which shard the document should be  
> assigned. During indexing, elasticsearch assigns each document to the shard  
> with the number Math.abs(hash(id) % numberOfShards). You can specify the  
> number of shards during index creation, but it cannot be changed  
> afterwards.
> 
> Shay is talking about it in some details in this video  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/videos/2011/08/09/road-to-a-distributed-searchengine-berlinbuzzwords.htmlstarting) at 25:24.
> 
> On Wednesday, March 28, 2012 9:54:51 AM UTC-4, mp2893 wrote:
> 
> > One more question,
> > 
> > Since ES is hashing the document id, is there a limit to the number of  
> > documents I can store?  
> > Or is ES using some kind of infinitely expanding hashing algorithm?
> > 
> > 2012/3/28 edward choi [mp2893@gmail.com](mailto:mp2893@gmail.com)
> > 
> > > Thanks Igor, for the quick reply.  
> > > Routing section was just what I needed to read.
> > > 
> > > Regards,  
> > > Ed
> > > 
> > > 2012/3/28 Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com)
> > > 
> > > > By default, elasticsearch is using hash of the document id. So, you  
> > > > should be fine. In elasticsearch you can control this process by specifying  
> > > > "routing" parameter with your request. See Routing section of this page  
> > > > [http://www.elasticsearch](http://www.elasticsearch). **org/guide/reference/api/index\_**.html[http://www.elasticsearch.org/guide/reference/api/index\_.html](http://www.elasticsearch.org/guide/reference/api/index_.html)for more details.
> > > > 
> > > > On Tuesday, March 27, 2012 1:24:39 PM UTC-4, mp2893 wrote:
> > > > 
> > > > > Hi,  
> > > > > I am rather new to ES, so this might be a very simple question.
> > > > > 
> > > > > I played with HBase for quite a while, and I know that HBase stores  
> > > > > keys lexicographically.  
> > > > > So generating keys in time series is not recommended. Because when a  
> > > > > query is based on a time range, this kind of key usage leads to users  
> > > > > accessing only a couple of nodes, since the keys are not distributed  
> > > > > out to all the nodes evenly.
> > > > > 
> > > > > I am worried that the same situation might happen with ES. I am  
> > > > > currently storing documents that has time-based id's (such as  
> > > > > 20120327\_23\_20471029471097097)  
> > > > > Should I change the document id's so that documents will be stored  
> > > > > evenly across all the nodes?
> > > > > 
> > > > > Thanks,  
> > > > > Ed

---

<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:34am UTC](https://discuss.elastic.co/t/how-are-the-keys-sorted-in-es/7146/7 "2017-07-06T03:34:11Z")

</div>

Closed on July 6, 2017, 3:34am UTC


