# Parent-child and sorting on a child

**URL:** https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354
**Category:** Elasticsearch
**Created:** [January 11, 2012, 9:20pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354 "2012-01-11T21:20:59Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [January 11, 2012, 9:20pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/1 "2012-01-11T21:20:59Z")

</div>

Hi guys. If you create a parent-child relationship between 2 indices, is it  
possible to sort the parent documents by one of its children's fields?

---

<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: [January 12, 2012, 10:46am UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/2 "2012-01-12T10:46:08Z")

</div>

No, you can't... . I assume you mean parent and child docs within the same  
index (as there is no cross index, or cross shard relationships).

On Wed, Jan 11, 2012 at 11:20 PM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:

> Hi guys. If you create a parent-child relationship between 2 indices, is  
> it possible to sort the parent documents by one of its children's fields?

---

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [January 12, 2012, 4:24pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/3 "2012-01-12T16:24:42Z")

</div>

On Thursday, 12 January 2012 05:46:08 UTC-5, kimchy wrote:

> No, you can't... . I assume you mean parent and child docs within the same  
> index (as there is no cross index, or cross shard relationships).

That's what I meant. Thanks for clarifying it for me, mate.

So is this the only way to sort parent docs by a child doc's field?

1. Search ES for the matching child docs, sorted on the desired field.

2. In my application, figure out which parent docs each child doc relates  
to.

Thanks again for your help, Shay. It's really helpful and valuable.  
Nick

---

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [January 16, 2012, 6:35am UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/4 "2012-01-16T06:35:17Z")

</div>

Bump =)

On Thursday, 12 January 2012 11:24:42 UTC-5, Nick Hoffman wrote:

> On Thursday, 12 January 2012 05:46:08 UTC-5, kimchy wrote:
> 
> > No, you can't... . I assume you mean parent and child docs within the  
> > same index (as there is no cross index, or cross shard relationships).
> 
> That's what I meant. Thanks for clarifying it for me, mate.
> 
> So is this the only way to sort parent docs by a child doc's field?
> 
> 1. Search ES for the matching child docs, sorted on the desired field.
> 
> 2. In my application, figure out which parent docs each child doc relates  
> to.
> 
> Thanks again for your help, Shay. It's really helpful and valuable.  
> Nick

---

<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: [January 17, 2012, 9:26am UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/5 "2012-01-17T09:26:59Z")

</div>

Yea, thats a way to solve it. Note you can get the parent id from the child  
by asking for \_parent field.

On Thu, Jan 12, 2012 at 6:24 PM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:

> On Thursday, 12 January 2012 05:46:08 UTC-5, kimchy wrote:
> 
> > No, you can't... . I assume you mean parent and child docs within the  
> > same index (as there is no cross index, or cross shard relationships).
> 
> That's what I meant. Thanks for clarifying it for me, mate.
> 
> So is this the only way to sort parent docs by a child doc's field?
> 
> 1. Search ES for the matching child docs, sorted on the desired field.
> 
> 2. In my application, figure out which parent docs each child doc relates  
> to.
> 
> Thanks again for your help, Shay. It's really helpful and valuable.  
> Nick

---

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [January 17, 2012, 2:43pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/6 "2012-01-17T14:43:12Z")

</div>

On Tuesday, 17 January 2012 04:26:59 UTC-5, kimchy wrote:

> Yea, thats a way to solve it. Note you can get the parent id from the  
> child by asking for \_parent field.

Ah, that makes life much easier, actually. Thanks, Shay.

---

<div class="post-metadata">

### Author: ![James\_McGill](https://avatars.discourse-cdn.com/v4/letter/j/8797f3/32.png) [@James\_McGill](https://discuss.elastic.co/u/James_McGill)
#### Post date: [July 19, 2012, 6:45am UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/7 "2012-07-19T06:45:28Z")

</div>

We would really like to be able to do this kind of sorting. We do  
filtering on a Child document and sorting on a field in the Parent, for  
potential results that would be much too large to sort in the application.  
(E.g., hundreds of pages of thousands of docs, sorted by ES, and only  
presenting the top docs.) The Child record actually has the field we'd  
like to sort by, and we copy this field to the parent. This means the  
parents are often out of sync with the child, and so the sort results are  
misleading when the parent and child are out of sync. Even if it was a  
relatively expensive operation, it would be extremely useful to be able to  
give sorted results of a Parent based on a field in a Child.

James

On Thursday, January 12, 2012 3:46:08 AM UTC-7, kimchy wrote:

> No, you can't... . I assume you mean parent and child docs within the same  
> index (as there is no cross index, or cross shard relationships).
> 
> On Wed, Jan 11, 2012 at 11:20 PM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com)wrote:
> 
> > Hi guys. If you create a parent-child relationship between 2 indices, is  
> > it possible to sort the parent documents by one of its children's fields?

---

<div class="post-metadata">

### Author: ![grantgochnauer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grantgochnauer/32/2300_2.png) [@grantgochnauer](https://discuss.elastic.co/u/grantgochnauer)
#### Post date: [March 7, 2013, 8:26pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/8 "2013-03-07T20:26:59Z")

</div>

Hi all --

I was just curious if this was still impossible since it's been many months  
since the last update. I have the same need in my application except a  
further request since I'm returning two types in my sorted list - one of  
which has children objects and one of which that does not but need to sort  
consistently across both top level objects.

I opened a thread here to ask about using a custom score via a script to  
achieve the same thing. I'm not sure if that is a possibility:  
[https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/KMmEOuc9Ce0](https://groups.google.com/forum/?fromgroups=#!topic/elasticsearch/KMmEOuc9Ce0)

thanks

On Thursday, July 19, 2012 1:45:28 AM UTC-5, James McGill wrote:

> We would really like to be able to do this kind of sorting. We do  
> filtering on a Child document and sorting on a field in the Parent, for  
> potential results that would be much too large to sort in the application.  
> (E.g., hundreds of pages of thousands of docs, sorted by ES, and only  
> presenting the top docs.) The Child record actually has the field we'd  
> like to sort by, and we copy this field to the parent. This means the  
> parents are often out of sync with the child, and so the sort results are  
> misleading when the parent and child are out of sync. Even if it was a  
> relatively expensive operation, it would be extremely useful to be able to  
> give sorted results of a Parent based on a field in a Child.
> 
> James
> 
> On Thursday, January 12, 2012 3:46:08 AM UTC-7, kimchy wrote:
> 
> > No, you can't... . I assume you mean parent and child docs within the  
> > same index (as there is no cross index, or cross shard relationships).
> > 
> > On Wed, Jan 11, 2012 at 11:20 PM, Nick Hoffman \<[ni...@deadorange.com](mailto:ni...@deadorange.com)\<javascript:\>
> > 
> > > wrote:
> > 
> > > Hi guys. If you create a parent-child relationship between 2 indices, is  
> > > it possible to sort the parent documents by one of its children's fields?

--  
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: ![zdeseb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zdeseb/32/42106_2.png) [@zdeseb](https://discuss.elastic.co/u/zdeseb)
#### Post date: [January 26, 2016, 4:45pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/9 "2016-01-26T16:45:40Z")

</div>

Any changes in ES 2.x version related to child field based sorting?

Is it still not possible?

---

<div class="post-metadata">

### Author: ![Sundar\_V](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sundar_v/32/6928_2.png) [@Sundar\_V](https://discuss.elastic.co/u/Sundar_V)
#### Post date: [January 26, 2016, 4:55pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/10 "2016-01-26T16:55:12Z")

</div>

AFAIK still the same - [https://github.com/elastic/elasticsearch/issues/2917](https://github.com/elastic/elasticsearch/issues/2917)

---

<div class="post-metadata">

### Author: ![shakunvohra](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@shakunvohra](https://discuss.elastic.co/u/shakunvohra)
#### Post date: [May 3, 2017, 5:45pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/11 "2017-05-03T17:45:29Z")

</div>

Hey Guys - i'm having similar issues with parent sorting based on child field.  
Using Elastic 5.3  
i see on the has\_child page it says to use script\_score but it's wont work for text and date fields? and how do we specify the order? any ideas?

---

<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 5, 2017, 10:00pm UTC](https://discuss.elastic.co/t/parent-child-and-sorting-on-a-child/6354/12 "2017-07-05T22:00:39Z")

</div>


