# ES not returning expected documents when using sort

**URL:** https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084
**Category:** Elasticsearch
**Created:** [May 22, 2013, 9:48pm UTC](https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084 "2013-05-22T21:48:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Aldo\_Sarmiento](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aldo_sarmiento/32/2317_2.png) [@Aldo\_Sarmiento](https://discuss.elastic.co/u/Aldo_Sarmiento)
#### Post date: [May 22, 2013, 9:48pm UTC](https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084/1 "2013-05-22T21:48:53Z")

</div>

Using a simple {"sort":[{"first\_name":"asc"}], "size":20, "from":0}} is  
returning inconsistent results.

When i rebuild the index, everything is returned correctly. However, as  
soon as i update a document, it no longer gets returned (using 0.90.0  
stable).

Here is a full gist of problem and mapping:

> <https://gist.github.com/sarmiena/d945848fd683f39d212c>

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)
#### Post date: [May 23, 2013, 7:24am UTC](https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084/2 "2013-05-23T07:24:39Z")

</div>

hey, does this also happen if you use the \_id as a secondary sort? If you  
update the document and all docs have the same sort value it might depend  
on what shard returns first. Can you try this as a tie-breaker?

On Wednesday, May 22, 2013 11:48:53 PM UTC+2, Aldo Sarmiento wrote:

> Using a simple {"sort":[{"first\_name":"asc"}], "size":20, "from":0}} is  
> returning inconsistent results.
> 
> When i rebuild the index, everything is returned correctly. However, as  
> soon as i update a document, it no longer gets returned (using 0.90.0  
> stable).
> 
> Here is a full gist of problem and mapping:
> 
> [gist:d945848fd683f39d212c · GitHub](https://gist.github.com/sarmiena/d945848fd683f39d212c)

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Aldo\_Sarmiento](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aldo_sarmiento/32/2317_2.png) [@Aldo\_Sarmiento](https://discuss.elastic.co/u/Aldo_Sarmiento)
#### Post date: [May 23, 2013, 8:08am UTC](https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084/3 "2013-05-23T08:08:50Z")

</div>

I've updated my query to be:

curl -X GET  
'[http://localhost:9200/development::application-contacts/contact/\_search?pretty](http://localhost:9200/development::application-contacts/contact/_search?pretty)'  
-d '  
{  
"sort": [  
{  
"first\_name\_sort": "asc"  
},  
"\_id"  
],  
"size": 20,  
"from": 0  
}

However, the record still doesn't get returned. I wonder if there is  
something wrong with the mapping. Not sure if that would affect the sort  
order, though.

On Thursday, May 23, 2013 12:24:39 AM UTC-7, simonw wrote:

> hey, does this also happen if you use the \_id as a secondary sort? If you  
> update the document and all docs have the same sort value it might depend  
> on what shard returns first. Can you try this as a tie-breaker?
> 
> On Wednesday, May 22, 2013 11:48:53 PM UTC+2, Aldo Sarmiento wrote:
> 
> > Using a simple {"sort":[{"first\_name":"asc"}], "size":20, "from":0}} is  
> > returning inconsistent results.
> > 
> > When i rebuild the index, everything is returned correctly. However, as  
> > soon as i update a document, it no longer gets returned (using 0.90.0  
> > stable).
> > 
> > Here is a full gist of problem and mapping:
> > 
> > [gist:d945848fd683f39d212c · GitHub](https://gist.github.com/sarmiena/d945848fd683f39d212c)

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Aldo\_Sarmiento](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aldo_sarmiento/32/2317_2.png) [@Aldo\_Sarmiento](https://discuss.elastic.co/u/Aldo_Sarmiento)
#### Post date: [May 23, 2013, 8:44am UTC](https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084/4 "2013-05-23T08:44:55Z")

</div>

Ok i've really stripped down the problem and made narrowed down the  
problem:

Problem

1  
2

elasticsearch sorting while using "size" and "from" not returning expected results after update

* * *

Create some records (using Ruby to create 100 records)

1  
2  
3  
4

(1).upto(100) do |i|  
`curl -XPUT 'http://localhost:9200/twitter/tweet/#{i}' -d '{ "user" : "#{i}"}'`end

* * *

View initial search/sort results

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86

curl -X GET '[http://localhost:9200/twitter/tweet/\_search?pretty](http://localhost:9200/twitter/tweet/_search?pretty)' -d '  
{  
"sort": [  
{  
"user": "asc"  
}  
],  
"size": 10,  
"from": 0  
}  
'

{  
"took" : 3,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 100,  
"max\_score" : null,  
"hits" : [ {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "1",  
"\_score" : null, "\_source" : { "user" : "1"},  
"sort" : ["1"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "10",  
"\_score" : null, "\_source" : { "user" : "10"},  
"sort" : ["10"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "100",  
"\_score" : null, "\_source" : { "user" : "100"},  
"sort" : ["100"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "11",  
"\_score" : null, "\_source" : { "user" : "11"},  
"sort" : ["11"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "12",  
"\_score" : null, "\_source" : { "user" : "12"},  
"sort" : ["12"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "13",  
"\_score" : null, "\_source" : { "user" : "13"},  
"sort" : ["13"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "14",  
"\_score" : null, "\_source" : { "user" : "14"},  
"sort" : ["14"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "15",  
"\_score" : null, "\_source" : { "user" : "15"},  
"sort" : ["15"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "16",  
"\_score" : null, "\_source" : { "user" : "16"},  
"sort" : ["16"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "17",  
"\_score" : null, "\_source" : { "user" : "17"},  
"sort" : ["17"]  
} ]  
}

* * *

Now let's update the first record

1  
2  
3  
4

curl -X POST "[http://localhost:9200/twitter/tweet/1](http://localhost:9200/twitter/tweet/1)" -d '  
{"user":"1"}  
'

* * *

And... for the problem (notice that previous first record is now missing)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85

curl -X GET '[http://localhost:9200/twitter/tweet/\_search?pretty](http://localhost:9200/twitter/tweet/_search?pretty)' -d '  
{  
"sort": [  
{  
"user": "asc"  
}  
],  
"size": 10,  
"from": 0  
}  
'  
{  
"took" : 2,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 100,  
"max\_score" : null,  
"hits" : [ {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "10",  
"\_score" : null, "\_source" : { "user" : "10"},  
"sort" : ["10"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "100",  
"\_score" : null, "\_source" : { "user" : "100"},  
"sort" : ["100"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "11",  
"\_score" : null, "\_source" : { "user" : "11"},  
"sort" : ["11"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "12",  
"\_score" : null, "\_source" : { "user" : "12"},  
"sort" : ["12"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "13",  
"\_score" : null, "\_source" : { "user" : "13"},  
"sort" : ["13"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "14",  
"\_score" : null, "\_source" : { "user" : "14"},  
"sort" : ["14"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "15",  
"\_score" : null, "\_source" : { "user" : "15"},  
"sort" : ["15"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "16",  
"\_score" : null, "\_source" : { "user" : "16"},  
"sort" : ["16"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "17",  
"\_score" : null, "\_source" : { "user" : "17"},  
"sort" : ["17"]  
}, {  
"\_index" : "twitter",  
"\_type" : "tweet",  
"\_id" : "18",  
"\_score" : null, "\_source" : { "user" : "18"},  
"sort" : ["18"]  
} ]  
}

On Thursday, May 23, 2013 1:08:50 AM UTC-7, Aldo Sarmiento wrote:

> I've updated my query to be:
> 
> curl -X GET '  
> [http://localhost:9200/development::application-contacts/contact/\_search?pretty](http://localhost:9200/development::application-contacts/contact/_search?pretty)'  
> -d '  
> {  
> "sort": [  
> {  
> "first\_name\_sort": "asc"  
> },  
> "\_id"  
> ],  
> "size": 20,  
> "from": 0  
> }
> 
> However, the record still doesn't get returned. I wonder if there is  
> something wrong with the mapping. Not sure if that would affect the sort  
> order, though.
> 
> On Thursday, May 23, 2013 12:24:39 AM UTC-7, simonw wrote:
> 
> > hey, does this also happen if you use the \_id as a secondary sort? If you  
> > update the document and all docs have the same sort value it might depend  
> > on what shard returns first. Can you try this as a tie-breaker?
> > 
> > On Wednesday, May 22, 2013 11:48:53 PM UTC+2, Aldo Sarmiento wrote:
> > 
> > > Using a simple {"sort":[{"first\_name":"asc"}], "size":20, "from":0}} is  
> > > returning inconsistent results.
> > > 
> > > When i rebuild the index, everything is returned correctly. However, as  
> > > soon as i update a document, it no longer gets returned (using 0.90.0  
> > > stable).
> > > 
> > > Here is a full gist of problem and mapping:
> > > 
> > > [gist:d945848fd683f39d212c · GitHub](https://gist.github.com/sarmiena/d945848fd683f39d212c)

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [May 23, 2013, 9:23am UTC](https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084/5 "2013-05-23T09:23:12Z")

</div>

Hi Simon

You can't sort on \_id out of the box, as it is not indexed.

Clint

On 23 May 2013 09:24, simonw [simon.willnauer@elasticsearch.com](mailto:simon.willnauer@elasticsearch.com) wrote:

> hey, does this also happen if you use the \_id as a secondary sort? If you  
> update the document and all docs have the same sort value it might depend  
> on what shard returns first. Can you try this as a tie-breaker?
> 
> On Wednesday, May 22, 2013 11:48:53 PM UTC+2, Aldo Sarmiento wrote:
> 
> > Using a simple {"sort":[{"first\_name":"asc"}]\*\*, "size":20, "from":0}}  
> > is returning inconsistent results.
> > 
> > When i rebuild the index, everything is returned correctly. However, as  
> > soon as i update a document, it no longer gets returned (using 0.90.0  
> > stable).
> > 
> > Here is a full gist of problem and mapping:
> > 
> > [https://gist.github.com/\*\*sarmiena/d945848fd683f39d212c](https://gist.github.com/**sarmiena/d945848fd683f39d212c)[https://gist.github.com/sarmiena/d945848fd683f39d212c](https://gist.github.com/sarmiena/d945848fd683f39d212c)
> > 
> > --  
> > 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).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [May 23, 2013, 9:44am UTC](https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084/6 "2013-05-23T09:44:18Z")

</div>

Hi Aldo

This looks like a bug. I've opened an issue

> <https://github.com/elastic/elasticsearch/issues/3078>
>
> After reindexing a doc, it is not being returned in the correct sort order (when… sorting on a string field)
> 
> First, index docs 1..100 with a string field \`user\`:
> 
> \`\`\`
> curl -XPOST 'http://127.0.0.1:9200/test/test/\_bulk?pretty=1' -d '
> {"index" : {"\_id" : "1"}}
> {"user" : "1"}
> {"index" : {"\_id" : "2"}}
> {"user" : "2"}
> {"index" : {"\_id" : "3"}}
> {"user" : "3"}
> {"index" : {"\_id" : "4"}}
> {"user" : "4"}
> {"index" : {"\_id" : "5"}}
> {"user" : "5"}
> {"index" : {"\_id" : "6"}}
> {"user" : "6"}
> {"index" : {"\_id" : "7"}}
> {"user" : "7"}
> {"index" : {"\_id" : "8"}}
> {"user" : "8"}
> {"index" : {"\_id" : "9"}}
> {"user" : "9"}
> {"index" : {"\_id" : "10"}}
> {"user" : "10"}
> {"index" : {"\_id" : "11"}}
> {"user" : "11"}
> {"index" : {"\_id" : "12"}}
> {"user" : "12"}
> {"index" : {"\_id" : "13"}}
> {"user" : "13"}
> {"index" : {"\_id" : "14"}}
> {"user" : "14"}
> {"index" : {"\_id" : "15"}}
> {"user" : "15"}
> {"index" : {"\_id" : "16"}}
> {"user" : "16"}
> {"index" : {"\_id" : "17"}}
> {"user" : "17"}
> {"index" : {"\_id" : "18"}}
> {"user" : "18"}
> {"index" : {"\_id" : "19"}}
> {"user" : "19"}
> {"index" : {"\_id" : "20"}}
> {"user" : "20"}
> {"index" : {"\_id" : "21"}}
> {"user" : "21"}
> {"index" : {"\_id" : "22"}}
> {"user" : "22"}
> {"index" : {"\_id" : "23"}}
> {"user" : "23"}
> {"index" : {"\_id" : "24"}}
> {"user" : "24"}
> {"index" : {"\_id" : "25"}}
> {"user" : "25"}
> {"index" : {"\_id" : "26"}}
> {"user" : "26"}
> {"index" : {"\_id" : "27"}}
> {"user" : "27"}
> {"index" : {"\_id" : "28"}}
> {"user" : "28"}
> {"index" : {"\_id" : "29"}}
> {"user" : "29"}
> {"index" : {"\_id" : "30"}}
> {"user" : "30"}
> {"index" : {"\_id" : "31"}}
> {"user" : "31"}
> {"index" : {"\_id" : "32"}}
> {"user" : "32"}
> {"index" : {"\_id" : "33"}}
> {"user" : "33"}
> {"index" : {"\_id" : "34"}}
> {"user" : "34"}
> {"index" : {"\_id" : "35"}}
> {"user" : "35"}
> {"index" : {"\_id" : "36"}}
> {"user" : "36"}
> {"index" : {"\_id" : "37"}}
> {"user" : "37"}
> {"index" : {"\_id" : "38"}}
> {"user" : "38"}
> {"index" : {"\_id" : "39"}}
> {"user" : "39"}
> {"index" : {"\_id" : "40"}}
> {"user" : "40"}
> {"index" : {"\_id" : "41"}}
> {"user" : "41"}
> {"index" : {"\_id" : "42"}}
> {"user" : "42"}
> {"index" : {"\_id" : "43"}}
> {"user" : "43"}
> {"index" : {"\_id" : "44"}}
> {"user" : "44"}
> {"index" : {"\_id" : "45"}}
> {"user" : "45"}
> {"index" : {"\_id" : "46"}}
> {"user" : "46"}
> {"index" : {"\_id" : "47"}}
> {"user" : "47"}
> {"index" : {"\_id" : "48"}}
> {"user" : "48"}
> {"index" : {"\_id" : "49"}}
> {"user" : "49"}
> {"index" : {"\_id" : "50"}}
> {"user" : "50"}
> {"index" : {"\_id" : "51"}}
> {"user" : "51"}
> {"index" : {"\_id" : "52"}}
> {"user" : "52"}
> {"index" : {"\_id" : "53"}}
> {"user" : "53"}
> {"index" : {"\_id" : "54"}}
> {"user" : "54"}
> {"index" : {"\_id" : "55"}}
> {"user" : "55"}
> {"index" : {"\_id" : "56"}}
> {"user" : "56"}
> {"index" : {"\_id" : "57"}}
> {"user" : "57"}
> {"index" : {"\_id" : "58"}}
> {"user" : "58"}
> {"index" : {"\_id" : "59"}}
> {"user" : "59"}
> {"index" : {"\_id" : "60"}}
> {"user" : "60"}
> {"index" : {"\_id" : "61"}}
> {"user" : "61"}
> {"index" : {"\_id" : "62"}}
> {"user" : "62"}
> {"index" : {"\_id" : "63"}}
> {"user" : "63"}
> {"index" : {"\_id" : "64"}}
> {"user" : "64"}
> {"index" : {"\_id" : "65"}}
> {"user" : "65"}
> {"index" : {"\_id" : "66"}}
> {"user" : "66"}
> {"index" : {"\_id" : "67"}}
> {"user" : "67"}
> {"index" : {"\_id" : "68"}}
> {"user" : "68"}
> {"index" : {"\_id" : "69"}}
> {"user" : "69"}
> {"index" : {"\_id" : "70"}}
> {"user" : "70"}
> {"index" : {"\_id" : "71"}}
> {"user" : "71"}
> {"index" : {"\_id" : "72"}}
> {"user" : "72"}
> {"index" : {"\_id" : "73"}}
> {"user" : "73"}
> {"index" : {"\_id" : "74"}}
> {"user" : "74"}
> {"index" : {"\_id" : "75"}}
> {"user" : "75"}
> {"index" : {"\_id" : "76"}}
> {"user" : "76"}
> {"index" : {"\_id" : "77"}}
> {"user" : "77"}
> {"index" : {"\_id" : "78"}}
> {"user" : "78"}
> {"index" : {"\_id" : "79"}}
> {"user" : "79"}
> {"index" : {"\_id" : "80"}}
> {"user" : "80"}
> {"index" : {"\_id" : "81"}}
> {"user" : "81"}
> {"index" : {"\_id" : "82"}}
> {"user" : "82"}
> {"index" : {"\_id" : "83"}}
> {"user" : "83"}
> {"index" : {"\_id" : "84"}}
> {"user" : "84"}
> {"index" : {"\_id" : "85"}}
> {"user" : "85"}
> {"index" : {"\_id" : "86"}}
> {"user" : "86"}
> {"index" : {"\_id" : "87"}}
> {"user" : "87"}
> {"index" : {"\_id" : "88"}}
> {"user" : "88"}
> {"index" : {"\_id" : "89"}}
> {"user" : "89"}
> {"index" : {"\_id" : "90"}}
> {"user" : "90"}
> {"index" : {"\_id" : "91"}}
> {"user" : "91"}
> {"index" : {"\_id" : "92"}}
> {"user" : "92"}
> {"index" : {"\_id" : "93"}}
> {"user" : "93"}
> {"index" : {"\_id" : "94"}}
> {"user" : "94"}
> {"index" : {"\_id" : "95"}}
> {"user" : "95"}
> {"index" : {"\_id" : "96"}}
> {"user" : "96"}
> {"index" : {"\_id" : "97"}}
> {"user" : "97"}
> {"index" : {"\_id" : "98"}}
> {"user" : "98"}
> {"index" : {"\_id" : "99"}}
> {"user" : "99"}
> {"index" : {"\_id" : "100"}}
> {"user" : "100"}
> '
> \`\`\`
> 
> Search, sorting on \`user\`:
> 
> \`\`\`
> curl -XGET 'http://127.0.0.1:9200/test/\_search?pretty=1' -d '
> {
> "sort" : {
> "user" : "asc"
> },
> "fields" : \[\],
> "size" : 10
> }
> '
> \`\`\`
> 
> Results show that \`user:1\` is in first position:
> 
> \`\`\`
> \# {
> \# "hits" : {
> \# "hits" : \[
> \# {
> \# "sort" : \[
> \# "1"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "1",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "10"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "10",
> \# "\_type" : "test"
> \# },
> \# ....
> \`\`\`
> 
> Now reindex the first doc, with the same values:
> 
> \`\`\`
> curl -XPUT 'http://127.0.0.1:9200/test/test/1?pretty=1' -d '
> {
> "user" : "1"
> }
> '
> \`\`\`
> 
> And search again:
> 
> \`\`\`
> curl -XGET 'http://127.0.0.1:9200/test/\_search?pretty=1' -d '
> {
> "sort" : {
> "user" : "asc"
> },
> "fields" : \[\],
> "size" : 10
> }
> '
> \`\`\`
> 
> Doc with \`user:1\` no longer appears in the correct position, in fact it doesn't appear anywhere in the first 10 results:
> 
> \`\`\`
> \# {
> \# "hits" : {
> \# "hits" : \[
> \# {
> \# "sort" : \[
> \# "10"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "10",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "100"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "100",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "11"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "11",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "12"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "12",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "13"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "13",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "14"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "14",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "15"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "15",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "16"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "16",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "17"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "17",
> \# "\_type" : "test"
> \# },
> \# {
> \# "sort" : \[
> \# "18"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "18",
> \# "\_type" : "test"
> \# }
> \# \],
> \# "max\_score" : null,
> \# "total" : 100
> \# },
> \# "timed\_out" : false,
> \# "\_shards" : {
> \# "failed" : 0,
> \# "successful" : 5,
> \# "total" : 5
> \# },
> \# "took" : 3
> \# }
> \`\`\`
> 
> However, if you return all 100 docs, then it appears in the first position again (correctly):
> 
> \`\`\`
> curl -XGET 'http://127.0.0.1:9200/test/\_search?pretty=1' -d '
> {
> "sort" : {
> "user" : "asc"
> },
> "fields" : \[\],
> "size" : 100
> }
> '
> 
> \# {
> \# "hits" : {
> \# "hits" : \[
> \# {
> \# "sort" : \[
> \# "1"
> \# \],
> \# "\_score" : null,
> \# "\_index" : "test",
> \# "\_id" : "1",
> \# "\_type" : "test"
> \# },
> \`\`\`
> 
> which leads me to think that it is the shard level sorting which is incorrect.

clint

On 23 May 2013 11:23, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> Hi Simon
> 
> You can't sort on \_id out of the box, as it is not indexed.
> 
> Clint
> 
> On 23 May 2013 09:24, simonw [simon.willnauer@elasticsearch.com](mailto:simon.willnauer@elasticsearch.com) wrote:
> 
> > hey, does this also happen if you use the \_id as a secondary sort? If you  
> > update the document and all docs have the same sort value it might depend  
> > on what shard returns first. Can you try this as a tie-breaker?
> > 
> > On Wednesday, May 22, 2013 11:48:53 PM UTC+2, Aldo Sarmiento wrote:
> > 
> > > Using a simple {"sort":[{"first\_name":"asc"}]\*\*, "size":20, "from":0}}  
> > > is returning inconsistent results.
> > > 
> > > When i rebuild the index, everything is returned correctly. However, as  
> > > soon as i update a document, it no longer gets returned (using 0.90.0  
> > > stable).
> > > 
> > > Here is a full gist of problem and mapping:
> > > 
> > > [https://gist.github.com/\*\*sarmiena/d945848fd683f39d212c](https://gist.github.com/**sarmiena/d945848fd683f39d212c)[https://gist.github.com/sarmiena/d945848fd683f39d212c](https://gist.github.com/sarmiena/d945848fd683f39d212c)
> > > 
> > > --  
> > > 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).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:35am UTC](https://discuss.elastic.co/t/es-not-returning-expected-documents-when-using-sort/12084/7 "2017-07-06T02:35:13Z")

</div>


