# Sort not working with nested query in elasticsearch

**URL:** <https://discuss.elastic.co/t/sort-not-working-with-nested-query-in-elasticsearch/5787>\
**Category:** Elasticsearch\
**Created:** [November 7, 2011, 5:11am UTC](https://discuss.elastic.co/t/sort-not-working-with-nested-query-in-elasticsearch/5787 "2011-11-07T05:11:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [November 7, 2011, 5:11am UTC](https://discuss.elastic.co/t/sort-not-working-with-nested-query-in-elasticsearch/5787/1 "2011-11-07T05:11:37Z")

</div>

I have been working on the nested query, and i need to sort the results,  
but as explained at  
[http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html) there  
is not dattribute for the sort, but i tried it at query level, even script  
is not working inside nested query, can anyone please give me the  
solution?? the data is as follows:

{  
"query": {  
"nested": {  
"path": "guids\_rated\_fan\_inverse",  
"query": {  
"term": {  
"guids\_rated\_fan\_inverse.guid": "34"  
}  
},  
"sort": {  
"guids\_rated\_fan\_inverse.time\_created": {  
"order": "desc"  
}  
}  
}  
}  
}

_Data is::_

guids\_rated\_fan\_inverse: [

- {
  - guid: 34
  - time\_created: 1320479261  
}

- {
  - guid: 600
  - time\_created: 1320479635  
}

]

Thanks...

---

<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:** [November 7, 2011, 10:04am UTC](https://discuss.elastic.co/t/sort-not-working-with-nested-query-in-elasticsearch/5787/2 "2011-11-07T10:04:53Z")

</div>

Hi Narinder

On Sun, 2011-11-06 at 21:11 -0800, Narinder Kaur wrote:

> I have been working on the nested query, and i need to sort the  
> results, but as explained at  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html) there is not dattribute for the sort, but i tried it at query level, even script is not working inside nested query, can anyone please give me the solution?? the data is as follows:

Two problems here:

1. The data from nested objects is only available outside a nested query  
if you have set include\_in\_parent or include\_in\_root

2. If you do the above, then the parent object will have multiple  
values for guids\_rated\_fan\_inverse.time\_created and you can't  
sort on multi-value fields

clint

> {  
> "query": {  
> "nested": {  
> "path": "guids\_rated\_fan\_inverse",  
> "query": {  
> "term": {  
> "guids\_rated\_fan\_inverse.guid": "34"  
> }  
> },  
> "sort": {  
> "guids\_rated\_fan\_inverse.time\_created": {  
> "order": "desc"  
> }  
> }  
> }  
> }  
> }
> 
> Data is::
> 
> guids\_rated\_fan\_inverse: [  
> \* {  
> \* guid: 34  
> \* time\_created: 1320479261  
> }  
> \* {  
> \* guid: 600  
> \* time\_created: 1320479635  
> }  
> ]
> 
> Thanks...

---

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [November 7, 2011, 11:29am UTC](https://discuss.elastic.co/t/sort-not-working-with-nested-query-in-elasticsearch/5787/3 "2011-11-07T11:29:00Z")

</div>

No I have not set any of the two parameters include\_in\_parent or  
include\_in\_root.

---

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [November 7, 2011, 11:44am UTC](https://discuss.elastic.co/t/sort-not-working-with-nested-query-in-elasticsearch/5787/4 "2011-11-07T11:44:00Z")

</div>

Rather I should say that sort is not working in nested query, here in the  
following example, the title is not the attribute of the nested structure,  
but sort is not working on title even.  
{  
"query": {  
"nested": {  
"path": "guids\_rated\_fan\_inverse",  
"query": {  
"term": {  
"guids\_rated\_fan\_inverse.guid": "34"  
}  
},  
"sort": {  
"title": {  
"order": "desc"  
}  
}  
}  
}  
}  
and the structure of the document is :

title: "abc",  
guids\_rated\_fan\_inverse: [

- {
  - guid: 34
  - time\_created: 1320479261  
}

- {
  - guid: 600
  - time\_created: 1320479635  
}

]

---

<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:49am UTC](https://discuss.elastic.co/t/sort-not-working-with-nested-query-in-elasticsearch/5787/5 "2017-07-06T03:49:48Z")

</div>


