# Sort on id not working

**URL:** https://discuss.elastic.co/t/sort-on-id-not-working/5553
**Category:** Elasticsearch
**Created:** [October 10, 2011, 4:13pm UTC](https://discuss.elastic.co/t/sort-on-id-not-working/5553 "2011-10-10T16:13:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [October 10, 2011, 4:13pm UTC](https://discuss.elastic.co/t/sort-on-id-not-working/5553/1 "2011-10-10T16:13:23Z")

</div>

Hi -- I have a bunch of items in a search index that look like this: '{  
"item": { "id": 42 } }'  
There are 100 of them with ids from 1 to 100, and their "\_id" property is  
also set correctly.  
My problem is that when I search for them with an ids filter, I can't get  
the results to sort correctly.  
Here is my attempt:  
curl -XGET "[http://localhost:9200/items\_test/items/\_search?pretty=true](http://localhost:9200/items_test/items/_search?pretty=true)" -d  
'{"size":20,"from":0,"filter":{"ids":{"values":[11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]}},"sort":[{"id":{"order":"asc"}}]}'

I would expect this (specifying a size of 20, and a from of 0) to return ids  
11 through to 30. But it doesn't -- it returns a random series of 20 of the  
items in the range specified, including the one id-ed 37, in a random order.  
If I call again with size = 20 and from = 20, then I'll get the rest  
correctly, but I need to be able to sort these results for them to be of any  
use.  
Could anyone advise as to what I'm doing wrong, or what assumptions I may be  
making in error?  
Thanks,  
Doug.

---

<div class="post-metadata">

### Author: ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)
#### Post date: [October 11, 2011, 3:18pm UTC](https://discuss.elastic.co/t/sort-on-id-not-working/5553/2 "2011-10-11T15:18:38Z")

</div>

did you try "sort" :{"\_uid":"desc"} as well? (btw: it is not id it is  
\_id ...)

On 10 Okt., 18:13, doug livesey [biot...@gmail.com](mailto:biot...@gmail.com) wrote:

> Hi -- I have a bunch of items in a search index that look like this: '{  
> "item": { "id": 42 } }'  
> There are 100 of them with ids from 1 to 100, and their "\_id" property is  
> also set correctly.  
> My problem is that when I search for them with an ids filter, I can't get  
> the results to sort correctly.  
> Here is my attempt:  
> curl -XGET "[http://localhost:9200/items\_test/items/\_search?pretty=true](http://localhost:9200/items_test/items/_search?pretty=true)" -d  
> '{"size":20,"from":0,"filter":{"ids":{"values":[11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]}},"sort":[{"id":{"order":"asc"}}]}'
> 
> I would expect this (specifying a size of 20, and a from of 0) to return ids  
> 11 through to 30. But it doesn't -- it returns a random series of 20 of the  
> items in the range specified, including the one id-ed 37, in a random order.  
> If I call again with size = 20 and from = 20, then I'll get the rest  
> correctly, but I need to be able to sort these results for them to be of any  
> use.  
> Could anyone advise as to what I'm doing wrong, or what assumptions I may be  
> making in error?  
> Thanks,  
> Doug.

---

<div class="post-metadata">

### Author: ![doug\_livesey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doug_livesey/32/2114_2.png) [@doug\_livesey](https://discuss.elastic.co/u/doug_livesey)
#### Post date: [October 11, 2011, 3:54pm UTC](https://discuss.elastic.co/t/sort-on-id-not-working/5553/3 "2011-10-11T15:54:23Z")

</div>

That did it, cheers! 🙂

On Oct 11, 4:18 pm, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> did you try "sort" :{"\_uid":"desc"} as well? (btw: it is not id it is  
> \_id ...)
> 
> On 10 Okt., 18:13, doug livesey [biot...@gmail.com](mailto:biot...@gmail.com) wrote:
> 
> > Hi -- I have a bunch of items in a search index that look like this: '{  
> > "item": { "id": 42 } }'  
> > There are 100 of them with ids from 1 to 100, and their "\_id" property is  
> > also set correctly.  
> > My problem is that when I search for them with an ids filter, I can't get  
> > the results to sort correctly.  
> > Here is my attempt:  
> > curl -XGET "[http://localhost:9200/items\_test/items/\_search?pretty=true](http://localhost:9200/items_test/items/_search?pretty=true)" -d  
> > '{"size":20,"from":0,"filter":{"ids":{"values":[11,12,13,14,15,16,17,18,19, 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]}},"sort":[{ "id":{"order":"asc"}}]}'
> 
> > I would expect this (specifying a size of 20, and a from of 0) to return ids  
> > 11 through to 30. But it doesn't -- it returns a random series of 20 of the  
> > items in the range specified, including the one id-ed 37, in a random order.  
> > If I call again with size = 20 and from = 20, then I'll get the rest  
> > correctly, but I need to be able to sort these results for them to be of any  
> > use.  
> > Could anyone advise as to what I'm doing wrong, or what assumptions I may be  
> > making in error?  
> > Thanks,  
> > Doug.

---

<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:52am UTC](https://discuss.elastic.co/t/sort-on-id-not-working/5553/4 "2017-07-06T03:52:23Z")

</div>


