# Sorting by an nested object field

**URL:** <https://discuss.elastic.co/t/sorting-by-an-nested-object-field/6171>\
**Category:** Elasticsearch\
**Created:** [December 15, 2011, 10:13pm UTC](https://discuss.elastic.co/t/sorting-by-an-nested-object-field/6171 "2011-12-15T22:13:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrik\_Johansson](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@Patrik\_Johansson](https://discuss.elastic.co/u/Patrik_Johansson)\
**Post date:** [December 15, 2011, 10:13pm UTC](https://discuss.elastic.co/t/sorting-by-an-nested-object-field/6171/1 "2011-12-15T22:13:37Z")

</div>

Hi all!

I have an nested mapping and data like the one in this example:  
[http://www.elasticsearch.org/guide/reference/mapping/nested-type.html](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)  
This works great when I'm using a nested filter or nested query.

{  
"obj1" : [  
{  
"name" : "blue",  
"count" : 4  
},  
{  
"name" : "green",  
"count" : 6  
}  
]  
}  
[Snippet is copied from [http://www.elasticsearch.org/guide/reference/mapping/nested-type.html](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)]

Now I'm trying to sort after on one the fields in the nested object  
but having problems how to solve this problem.  
So, how would you guys try to sort based on value of "count" in the  
example above?

Thanks in advance,  
Patrik

---

<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:** [December 16, 2011, 3:47pm UTC](https://discuss.elastic.co/t/sorting-by-an-nested-object-field/6171/2 "2011-12-16T15:47:26Z")

</div>

You can't sort on nest fields, moreover, since they are multi valued, even  
if they were in the "root" object, you can't sort on them.

On Fri, Dec 16, 2011 at 12:13 AM, Patrik Johansson \<  
patrik.j.johansson@kentor.se\> wrote:

> Hi all!
> 
> I have an nested mapping and data like the one in this example:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)  
> This works great when I'm using a nested filter or nested query.
> 
> {  
> "obj1" : [  
> {  
> "name" : "blue",  
> "count" : 4  
> },  
> {  
> "name" : "green",  
> "count" : 6  
> }  
> ]  
> }  
> [Snippet is copied from  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)]
> 
> Now I'm trying to sort after on one the fields in the nested object  
> but having problems how to solve this problem.  
> So, how would you guys try to sort based on value of "count" in the  
> example above?
> 
> Thanks in advance,  
> Patrik

---

<div class="post-metadata">

**Author:** ![Patrik\_Johansson](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@Patrik\_Johansson](https://discuss.elastic.co/u/Patrik_Johansson)\
**Post date:** [December 19, 2011, 8:13am UTC](https://discuss.elastic.co/t/sorting-by-an-nested-object-field/6171/3 "2011-12-19T08:13:44Z")

</div>

Thanks for the reply!

So if pure sorting on nest fields is a no-go, is it possible to do some kind of  
boosting or custom\_score based of the value (or boost) for the nest fields?

//Patrik J

16 dec 2011 kl. 16.47 skrev Shay Banon:

You can't sort on nest fields, moreover, since they are multi valued, even if they were in the "root" object, you can't sort on them.

On Fri, Dec 16, 2011 at 12:13 AM, Patrik Johansson \<patrik.j.johansson@kentor.se[mailto:patrik.j.johansson@kentor.se](mailto:patrik.j.johansson@kentor.se)\> wrote:  
Hi all!

I have an nested mapping and data like the one in this example:  
[http://www.elasticsearch.org/guide/reference/mapping/nested-type.html](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)  
This works great when I'm using a nested filter or nested query.

{  
"obj1" : [  
{  
"name" : "blue",  
"count" : 4  
},  
{  
"name" : "green",  
"count" : 6  
}  
]  
}  
[Snippet is copied from [http://www.elasticsearch.org/guide/reference/mapping/nested-type.html](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)]

Now I'm trying to sort after on one the fields in the nested object  
but having problems how to solve this problem.  
So, how would you guys try to sort based on value of "count" in the  
example above?

Thanks in advance,  
Patrik

---

<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:** [December 20, 2011, 3:11pm UTC](https://discuss.elastic.co/t/sorting-by-an-nested-object-field/6171/4 "2011-12-20T15:11:40Z")

</div>

You can, but "within" the nested query per nested element, but check if its  
fast enough for you.

On Mon, Dec 19, 2011 at 10:16 AM, Patrik Johansson \<  
patrik.j.johansson@kentor.se\> wrote:

> Thanks for the reply!
> 
> So if pure sorting on nest fields is a no-go, is it possible to do some  
> kind of  
> boosting or custom\_score based of the value (or boost) for the nest fields?
> 
> //Patrik J
> 
> 16 dec 2011 kl. 16.47 skrev Shay Banon:
> 
> You can't sort on nest fields, moreover, since they are multi valued, even  
> if they were in the "root" object, you can't sort on them.
> 
> On Fri, Dec 16, 2011 at 12:13 AM, Patrik Johansson \<  
> patrik.j.johansson@kentor.se\> wrote:
> 
> > Hi all!
> > 
> > I have an nested mapping and data like the one in this example:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)  
> > This works great when I'm using a nested filter or nested query.
> > 
> > {  
> > "obj1" : [  
> > {  
> > "name" : "blue",  
> > "count" : 4  
> > },  
> > {  
> > "name" : "green",  
> > "count" : 6  
> > }  
> > ]  
> > }  
> > [Snippet is copied from  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)]
> > 
> > Now I'm trying to sort after on one the fields in the nested object  
> > but having problems how to solve this problem.  
> > So, how would you guys try to sort based on value of "count" in the  
> > example above?
> > 
> > Thanks in advance,  
> > Patrik

---

<div class="post-metadata">

**Author:** ![Dane\_Bertram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dane_bertram/32/1840_2.png) [@Dane\_Bertram](https://discuss.elastic.co/u/Dane_Bertram)\
**Post date:** [May 22, 2013, 6:24pm UTC](https://discuss.elastic.co/t/sorting-by-an-nested-object-field/6171/5 "2013-05-22T18:24:27Z")

</div>

In case you're still working on this problem, support for sorting by fields  
inside of nested documents was added in 0.90:

> <https://github.com/elastic/elasticsearch/issues/2662>
>
> The sorting by nested field support has the following parameters on top of the a…lready existing sort options:
> \- \`nested\_path\` - Defines the on what nested object to sort. The actual sort field must be a direct field inside this nested object. The default is to use the most immediate inherited nested object from the sort field. 
> \- \`nested\_filter\` - A filter the inner objects inside the nested path should match with in order for its field values to be taken into account by sorting. Common case is to repeat the query / filter inside the nested filter or query. By default no \`nested\_filter\` is active.
> 
> Either the highest (max) or lowest (min) inner object is picked for during sorting depending on the \`sort\_mode\` being used. The \`sort\_mode\` options \`avg\` and \`sum\` can still be used for number based fields inside nested objects. All the values for the sort field are taken into account for each nested object.

On Thursday, 15 December 2011 17:13:37 UTC-5, Patrik Johansson wrote:

> Hi all!
> 
> I have an nested mapping and data like the one in this example:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)  
> This works great when I'm using a nested filter or nested query.
> 
> {  
> "obj1" : [  
> {  
> "name" : "blue",  
> "count" : 4  
> },  
> {  
> "name" : "green",  
> "count" : 6  
> }  
> ]  
> }  
> [Snippet is copied from  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)]
> 
> Now I'm trying to sort after on one the fields in the nested object  
> but having problems how to solve this problem.  
> So, how would you guys try to sort based on value of "count" in the  
> example above?
> 
> Thanks in advance,  
> Patrik

--  
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:** ![garnaes](https://avatars.discourse-cdn.com/v4/letter/g/ea5d25/32.png) [@garnaes](https://discuss.elastic.co/u/garnaes)\
**Post date:** [May 23, 2013, 8:36am UTC](https://discuss.elastic.co/t/sorting-by-an-nested-object-field/6171/6 "2013-05-23T08:36:07Z")

</div>

That's what I thought, but I can't get it working  
(see [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/r-oxvNZjeEY)).  
Can someone provide a working example?

On Wednesday, 22 May 2013 20:24:27 UTC+2, Dane Bertram wrote:

> In case you're still working on this problem, support for sorting by  
> fields inside of nested documents was added in 0.90:
> 
> [Add support for sorting by fields inside one or more nested objects · Issue #2662 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2662)
> 
> On Thursday, 15 December 2011 17:13:37 UTC-5, Patrik Johansson wrote:
> 
> > Hi all!
> > 
> > I have an nested mapping and data like the one in this example:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)  
> > This works great when I'm using a nested filter or nested query.
> > 
> > {  
> > "obj1" : [  
> > {  
> > "name" : "blue",  
> > "count" : 4  
> > },  
> > {  
> > "name" : "green",  
> > "count" : 6  
> > }  
> > ]  
> > }  
> > [Snippet is copied from  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)]
> > 
> > Now I'm trying to sort after on one the fields in the nested object  
> > but having problems how to solve this problem.  
> > So, how would you guys try to sort based on value of "count" in the  
> > example above?
> > 
> > Thanks in advance,  
> > Patrik

--  
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/sorting-by-an-nested-object-field/6171/7 "2017-07-06T02:35:15Z")

</div>


