# Difference on sorting between query\_and\_fetch and query\_then\_fetch

**URL:** <https://discuss.elastic.co/t/difference-on-sorting-between-query-and-fetch-and-query-then-fetch/5527>\
**Category:** Elasticsearch\
**Created:** [October 6, 2011, 9:27am UTC](https://discuss.elastic.co/t/difference-on-sorting-between-query-and-fetch-and-query-then-fetch/5527 "2011-10-06T09:27:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alheim](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@alheim](https://discuss.elastic.co/u/alheim)\
**Post date:** [October 6, 2011, 9:27am UTC](https://discuss.elastic.co/t/difference-on-sorting-between-query-and-fetch-and-query-then-fetch/5527/1 "2011-10-06T09:27:37Z")

</div>

Hi,

I try to get a list of documents ordered by a field. No specific limit or  
offset used. Just a search with a sort.

The returned documents are just sorted "by shards". In other words, the  
first 3 documents are sorted right, then the 4th is not well sorted but the  
10 following are well sorted comparing to the 4th...

I understand that each shard returns a sorted result set. Right ?

But I thought that the search\_type could change this behavior and apply the  
sort after having fetched documents.

Please explain to me, thanks

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [October 6, 2011, 6:48pm UTC](https://discuss.elastic.co/t/difference-on-sorting-between-query-and-fetch-and-query-then-fetch/5527/2 "2011-10-06T18:48:04Z")

</div>

You get proper sorted results when you use query\_then\_fetch (across all top  
"size" results), if you use query\_and\_fetch, then each shard return the size  
requested hits, and then they are sorted between them.

On Thu, Oct 6, 2011 at 11:27 AM, Alexandre Heimburger \<  
[ahb@bluekiwi-software.com](mailto:ahb@bluekiwi-software.com)\> wrote:

> Hi,
> 
> I try to get a list of documents ordered by a field. No specific limit or  
> offset used. Just a search with a sort.
> 
> The returned documents are just sorted "by shards". In other words, the  
> first 3 documents are sorted right, then the 4th is not well sorted but the  
> 10 following are well sorted comparing to the 4th...
> 
> I understand that each shard returns a sorted result set. Right ?
> 
> But I thought that the search\_type could change this behavior and apply the  
> sort after having fetched documents.
> 
> Please explain to me, thanks

---

<div class="post-metadata">

**Author:** ![alheim](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@alheim](https://discuss.elastic.co/u/alheim)\
**Post date:** [October 10, 2011, 7:31am UTC](https://discuss.elastic.co/t/difference-on-sorting-between-query-and-fetch-and-query-then-fetch/5527/3 "2011-10-10T07:31:06Z")

</div>

Hi Kimchy,

My issue is related to nested fields (again sorry). I got this strange  
behavior each time a use a sort pattern like "path1.path2.sortedField".  
Everything is working fine when sorting fields are attached directly to the  
root document.

Any idea ?

---

<div class="post-metadata">

**Author:** ![alheim](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@alheim](https://discuss.elastic.co/u/alheim)\
**Post date:** [October 10, 2011, 9:51am UTC](https://discuss.elastic.co/t/difference-on-sorting-between-query-and-fetch-and-query-then-fetch/5527/4 "2011-10-10T09:51:32Z")

</div>

Forget my last comment please...It's monday morning. I wanted to index  
integer but the field was indexed as a string...  
Thanks again for your help (and your patience).

---

<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/difference-on-sorting-between-query-and-fetch-and-query-then-fetch/5527/5 "2017-07-06T03:52:30Z")

</div>


