# @Kimchy - Sort not working in nested query, Is this bug or missing feature

**URL:** <https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778>\
**Category:** Elasticsearch\
**Created:** [November 5, 2011, 7:54am UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778 "2011-11-05T07:54:52Z")\
**Posts on this page:** 9\
**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 5, 2011, 7:54am UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/1 "2011-11-05T07:54:52Z")

</div>

_Query::_

{  
"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:** ![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:** [November 8, 2011, 3:47am UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/2 "2011-11-08T03:47:18Z")

</div>

You can't sort on nested docs.

On Sat, Nov 5, 2011 at 9:54 AM, Narinder Kaur [narinder.kaur@izap.in](mailto:narinder.kaur@izap.in) wrote:

> _Query::_
> 
> {  
> "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:** ![Carl\_Sharma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carl_sharma/32/2767_2.png) [@Carl\_Sharma](https://discuss.elastic.co/u/Carl_Sharma)\
**Post date:** [November 8, 2011, 4:46am UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/3 "2011-11-08T04:46:26Z")

</div>

Hmmm, Is there any other solution for this kind of problem ?

---

<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:** [November 9, 2011, 7:08am UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/4 "2011-11-09T07:08:12Z")

</div>

Why is it a problem? If you have multiple values for a field (which is what  
nested docs are usually used for), then you can't really sort on that  
field...

On Tue, Nov 8, 2011 at 6:46 AM, Carl Sharma [carl@izap.in](mailto:carl@izap.in) wrote:

> Hmmm, Is there any other solution for this kind of problem ?

---

<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 10, 2011, 10:38am UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/5 "2011-11-10T10:38:15Z")

</div>

But In my case, It is unique against an unique array key

---

<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:** [November 13, 2011, 7:06am UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/6 "2011-11-13T07:06:22Z")

</div>

But the result is the "root" document, so based on what will it sort? We  
can come up with saying something like the "highest" or "lowest" value of  
all available values within the nested docs, but its not supported.

On Thu, Nov 10, 2011 at 12:38 PM, Narinder Kaur [narinder.kaur@izap.in](mailto:narinder.kaur@izap.in)wrote:

> But In my case, It is unique against an unique array key

---

<div class="post-metadata">

**Author:** ![Pavel\_Horal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pavel_horal/32/2653_2.png) [@Pavel\_Horal](https://discuss.elastic.co/u/Pavel_Horal)\
**Post date:** [August 13, 2012, 2:27pm UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/7 "2012-08-13T14:27:14Z")

</div>

That would be great! I am facing similar problem with documents:

{  
"itemName": "foo",  
"itemDescription": "bar",  
"locations": [  
{ "coordinates": { "lat": 50.12, "lon": 14.32] },  
{ "coordinates": { "lat": 51.12, "lon": 14.88] }  
]  
}

Now I would like to implement use case to "search for the closest items". I  
think being able to support such sorting will greatly improve the  
flexibility. Does that make sense? Are there any future plans for this kind  
of feature?

On Sunday, November 13, 2011 8:06:22 AM UTC+1, kimchy wrote:

> But the result is the "root" document, so based on what will it sort? We  
> can come up with saying something like the "highest" or "lowest" value of  
> all available values within the nested docs, but its not supported.
> 
> On Thu, Nov 10, 2011 at 12:38 PM, Narinder Kaur \<narind...@izap.in\<javascript:\>
> 
> > wrote:
> 
> > But In my case, It is unique against an unique array key

--

---

<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:** [August 13, 2012, 4:12pm UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/8 "2012-08-13T16:12:38Z")

</div>

Yes, there are plans to support it. No dates though.

On Aug 13, 2012, at 4:27 PM, Pavel Horal [horal.pavel@gmail.com](mailto:horal.pavel@gmail.com) wrote:

> That would be great! I am facing similar problem with documents:
> 
> {  
> "itemName": "foo",  
> "itemDescription": "bar",  
> "locations": [  
> { "coordinates": { "lat": 50.12, "lon": 14.32] },  
> { "coordinates": { "lat": 51.12, "lon": 14.88] }  
> ]  
> }
> 
> Now I would like to implement use case to "search for the closest items". I think being able to support such sorting will greatly improve the flexibility. Does that make sense? Are there any future plans for this kind of feature?
> 
> On Sunday, November 13, 2011 8:06:22 AM UTC+1, kimchy wrote:  
> But the result is the "root" document, so based on what will it sort? We can come up with saying something like the "highest" or "lowest" value of all available values within the nested docs, but its not supported.
> 
> On Thu, Nov 10, 2011 at 12:38 PM, Narinder Kaur [narind...@izap.in](mailto:narind...@izap.in) wrote:  
> But In my case, It is unique against an unique array key
> 
> --

--

---

<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:16am UTC](https://discuss.elastic.co/t/kimchy-sort-not-working-in-nested-query-is-this-bug-or-missing-feature/5778/9 "2017-07-06T03:16:32Z")

</div>


