# Sorting by relevance in matching children - top\_children and has\_child

**URL:** <https://discuss.elastic.co/t/sorting-by-relevance-in-matching-children-top-children-and-has-child/5310>\
**Category:** Elasticsearch\
**Created:** [September 5, 2011, 6:02am UTC](https://discuss.elastic.co/t/sorting-by-relevance-in-matching-children-top-children-and-has-child/5310 "2011-09-05T06:02:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwho](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cwho](https://discuss.elastic.co/u/cwho)\
**Post date:** [September 5, 2011, 6:02am UTC](https://discuss.elastic.co/t/sorting-by-relevance-in-matching-children-top-children-and-has-child/5310/1 "2011-09-05T06:02:05Z")

</div>

We've been using the has\_child query extensively and understand that its an  
extension of the has\_child filter (no scoring), while I understand the  
top\_children query does translate the scoring (so parent docs are ranked  
according to child docs relevance).

I like to know in detail how top\_children plays out if we have additional  
criteria on the parents document, and multiple top\_children criteria, e.g.  
parent doc type [user] has children doc types [tweet] and [status\_update]

so I like to query for users who are not deleted, have a tweet containing  
"bored" and a status\_update containing "school". Right now I have:

[user] field "deleted" matches "no"  
AND has\_child [tweet] contains "bored"  
AND has\_child [status\_update] contains "school"

which is working but this does not rank the user matches in terms of how  
well "bored" is matched in [tweet] and "school" is matched in  
[status\_update]  
Is top\_children a suitable replacement in this case?

[user] field "deleted" matches "no"  
AND top\_children [tweet] contains "bored"  
AND top\_children [status\_update] contains "school"

Generally can I run the query with has\_child once to get the "correct" total  
count, and then run it again with top\_children to get the "correct" ranking?  
It seems like a roundabout way - is there a easier way for this?

Thanks!

---

<div class="post-metadata">

**Author:** ![cwho](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cwho](https://discuss.elastic.co/u/cwho)\
**Post date:** [September 26, 2011, 3:57am UTC](https://discuss.elastic.co/t/sorting-by-relevance-in-matching-children-top-children-and-has-child/5310/2 "2011-09-26T03:57:05Z")

</div>

Hi,

Just an update if anyone can help me on my query about trying to put  
relevance from a child query into a parent one. Thanks!

> We've been using the has\_child query extensively and understand that its an  
> extension of the has\_child filter (no scoring), while I understand the  
> top\_children query does translate the scoring (so parent docs are ranked  
> according to child docs relevance).
> 
> I like to know in detail how top\_children plays out if we have additional  
> criteria on the parents document, and multiple top\_children criteria, e.g.  
> parent doc type [user] has children doc types [tweet] and [status\_update]
> 
> so I like to query for users who are not deleted, have a tweet containing  
> "bored" and a status\_update containing "school". Right now I have:
> 
> [user] field "deleted" matches "no"  
> AND has\_child [tweet] contains "bored"  
> AND has\_child [status\_update] contains "school"
> 
> which is working but this does not rank the user matches in terms of how  
> well "bored" is matched in [tweet] and "school" is matched in  
> [status\_update]  
> Is top\_children a suitable replacement in this case?
> 
> [user] field "deleted" matches "no"  
> AND top\_children [tweet] contains "bored"  
> AND top\_children [status\_update] contains "school"
> 
> Generally can I run the query with has\_child once to get the "correct" total  
> count, and then run it again with top\_children to get the "correct" ranking?  
> It seems like a roundabout way - is there a easier way for this?
> 
> Thanks!

---

<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:53am UTC](https://discuss.elastic.co/t/sorting-by-relevance-in-matching-children-top-children-and-has-child/5310/3 "2017-07-06T03:53:36Z")

</div>


