# How does sorting on \_id work?

**URL:** https://discuss.elastic.co/t/how-does-sorting-on--id-work/21854
**Category:** Elasticsearch
**Created:** [January 27, 2015, 6:24pm UTC](https://discuss.elastic.co/t/how-does-sorting-on--id-work/21854 "2015-01-27T18:24:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Abid\_Hussain](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Abid\_Hussain](https://discuss.elastic.co/u/Abid_Hussain)
#### Post date: [January 27, 2015, 6:24pm UTC](https://discuss.elastic.co/t/how-does-sorting-on--id-work/21854/1 "2015-01-27T18:24:41Z")

</div>

Hi all,

I want to determine the doc with max and min \_id value. So, when I run this  
query:  
GET /my\_index/order/\_search  
{  
"fields": ["\_id"],  
"sort": [  
{ "\_uid": { "order": "desc" } }  
],  
"size": 1  
}  
I get a result:  
{  
...  
"hits": {  
...  
"hits": [  
{  
"\_index": "my\_index",  
"\_type": "order",  
"\_id": "999999",  
"\_score": null,  
"sort": [  
"order#999999"  
]  
}  
]  
}  
}

There is definitevely a doc with \_id value 11132106 in index which I would  
have expected as result.

And, when I run the same search with _order asc_ I get a result with  
\_id 1000000 which is higher than 999999...?

What am I doing wrong?

Regards,

Abid

--  
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/92429b89-0741-40cc-9d76-8e1a36e5c1f5%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/92429b89-0741-40cc-9d76-8e1a36e5c1f5%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Abid\_Hussain](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Abid\_Hussain](https://discuss.elastic.co/u/Abid_Hussain)
#### Post date: [January 27, 2015, 6:28pm UTC](https://discuss.elastic.co/t/how-does-sorting-on--id-work/21854/2 "2015-01-27T18:28:44Z")

</div>

... can it be that \_id is treated as string? If so, is there any way  
retrieve the max \_id field with treating \_id as integer?

Am Dienstag, 27. Januar 2015 19:24:41 UTC+1 schrieb Abid Hussain:

> Hi all,
> 
> I want to determine the doc with max and min \_id value. So, when I run  
> this query:  
> GET /my\_index/order/\_search  
> {  
> "fields": ["\_id"],  
> "sort": [  
> { "\_uid": { "order": "desc" } }  
> ],  
> "size": 1  
> }  
> I get a result:  
> {  
> ...  
> "hits": {  
> ...  
> "hits": [  
> {  
> "\_index": "my\_index",  
> "\_type": "order",  
> "\_id": "999999",  
> "\_score": null,  
> "sort": [  
> "order#999999"  
> ]  
> }  
> ]  
> }  
> }
> 
> There is definitevely a doc with \_id value 11132106 in index which I would  
> have expected as result.
> 
> And, when I run the same search with _order asc_ I get a result with  
> \_id 1000000 which is higher than 999999...?
> 
> What am I doing wrong?
> 
> Regards,
> 
> Abid

--  
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/d3e540ef-8e40-4f38-b655-db9da9b64946%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d3e540ef-8e40-4f38-b655-db9da9b64946%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)
#### Post date: [January 27, 2015, 7:14pm UTC](https://discuss.elastic.co/t/how-does-sorting-on--id-work/21854/3 "2015-01-27T19:14:24Z")

</div>

Yes, the \_id field is a string. You are not limited to numbers. In fact, an  
automatically generated ID has many non-numeric characters in it.

For what you want, you should create an id field, map it to a long integer,  
and then copy your \_id into that id field when you load the document. Then  
when you sort on the id field, you will get a numeric sort.

Hope this helps.

Brian

On Tuesday, January 27, 2015 at 1:28:44 PM UTC-5, Abid Hussain wrote:

> ... can it be that \_id is treated as string? If so, is there any way  
> retrieve the max \_id field with treating \_id as integer?
> 
> Am Dienstag, 27. Januar 2015 19:24:41 UTC+1 schrieb Abid Hussain:
> 
> > Hi all,
> > 
> > I want to determine the doc with max and min \_id value. So, when I run  
> > this query:  
> > GET /my\_index/order/\_search  
> > {  
> > "fields": ["\_id"],  
> > "sort": [  
> > { "\_uid": { "order": "desc" } }  
> > ],  
> > "size": 1  
> > }  
> > I get a result:  
> > {  
> > ...  
> > "hits": {  
> > ...  
> > "hits": [  
> > {  
> > "\_index": "my\_index",  
> > "\_type": "order",  
> > "\_id": "999999",  
> > "\_score": null,  
> > "sort": [  
> > "order#999999"  
> > ]  
> > }  
> > ]  
> > }  
> > }
> > 
> > There is definitevely a doc with \_id value 11132106 in index which I  
> > would have expected as result.
> > 
> > And, when I run the same search with _order asc_ I get a result with  
> > \_id 1000000 which is higher than 999999...?
> > 
> > What am I doing wrong?
> > 
> > Regards,
> > 
> > Abid

--  
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/7842cdcf-67ee-48ed-8ef6-e8be2bb63a4a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7842cdcf-67ee-48ed-8ef6-e8be2bb63a4a%40googlegroups.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, 12:36am UTC](https://discuss.elastic.co/t/how-does-sorting-on--id-work/21854/4 "2017-07-06T00:36:21Z")

</div>


