# No results for top\_children query; same has\_child query works

**URL:** https://discuss.elastic.co/t/no-results-for-top-children-query-same-has-child-query-works/6609
**Category:** Elasticsearch
**Created:** [February 6, 2012, 10:10pm UTC](https://discuss.elastic.co/t/no-results-for-top-children-query-same-has-child-query-works/6609 "2012-02-06T22:10:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Matthew\_A\_Brown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthew_a_brown/32/2699_2.png) [@Matthew\_A\_Brown](https://discuss.elastic.co/u/Matthew_A_Brown)
#### Post date: [February 6, 2012, 10:10pm UTC](https://discuss.elastic.co/t/no-results-for-top-children-query-same-has-child-query-works/6609/1 "2012-02-06T22:10:59Z")

</div>

Hi all,

I've got a situation where running a top\_children query returns no  
results; running the same query wrapped in a has\_child returns the  
expected result. I know that top\_children may miss parent document  
results in the case where there are lots of matching children per  
parent; but since in this case top\_children returns nothing at all, it  
seems that that's not what's going on.

I can try to put together a reduction, but first just wanted to make  
sure my understanding of top\_children/has\_child is correct, and that  
what I'm describing is not expected behavior.

Thanks!  
Mat

---

<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: [February 7, 2012, 10:13am UTC](https://discuss.elastic.co/t/no-results-for-top-children-query-same-has-child-query-works/6609/2 "2012-02-07T10:13:58Z")

</div>

It sounds strange, it should return a value… . A recreation would help, thanks!

On Tuesday, February 7, 2012 at 12:10 AM, Matthew A. Brown wrote:

> Hi all,
> 
> I've got a situation where running a top\_children query returns no  
> results; running the same query wrapped in a has\_child returns the  
> expected result. I know that top\_children may miss parent document  
> results in the case where there are lots of matching children per  
> parent; but since in this case top\_children returns nothing at all, it  
> seems that that's not what's going on.
> 
> I can try to put together a reduction, but first just wanted to make  
> sure my understanding of top\_children/has\_child is correct, and that  
> what I'm describing is not expected behavior.
> 
> Thanks!  
> Mat

---

<div class="post-metadata">

### Author: ![Matthew\_A\_Brown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matthew_a_brown/32/2699_2.png) [@Matthew\_A\_Brown](https://discuss.elastic.co/u/Matthew_A_Brown)
#### Post date: [February 16, 2012, 4:25pm UTC](https://discuss.elastic.co/t/no-results-for-top-children-query-same-has-child-query-works/6609/3 "2012-02-16T16:25:19Z")

</div>

Finally put together a reduction for it -- the kicker seems to be  
having a nested field in the parent type with the same name as the  
field you're searching in the child type. I've opened a ticket:

> <https://github.com/elastic/elasticsearch/issues/1709>
>
> What reproduces the problem is when you have a field, say, "body", in the child …type, and you're searching on that field in \`top\_children\`. If the parent type also has a nested field with the name "body", then the \`top\_children\` query will return no results; the same \`has\_child\` query/filter does return results.
> 
> Here's a reduction (or \[download\](https://gist.github.com/1846196)):
> 
> \`\`\`
> curl -XDELETE localhost:9200/test?pretty=true
> echo ""
> curl -XPUT localhost:9200/test?pretty=true
> echo ""
> curl -XPUT localhost:9200/test/parent/\_mapping?pretty=true -d '{"parent":{"properties":{"name":{"type":"string","index":"analyzed"},"notes":{"properties":{"body":{"type":"string"}}}}}}'
> echo ""
> curl -XPUT localhost:9200/test/child/\_mapping?pretty=true -d '{"child":{"\_parent":{"type":"parent"}, "properties":{"body":{"type":"string","index":"analyzed"}}}}'
> echo ""
> curl -XPUT localhost:9200/test/parent/1?pretty=true -d "{\\"name\\":\\"parent document\\"}"
> echo ""
> curl -XPOST 'localhost:9200/test/child/\_create?parent=1&pretty=true&refresh=true' -d '{"body":"pizza monster"}'
> echo ""
> curl -XGET localhost:9200/test/parent/\_search?pretty=true -d '{"query":{"top\_children":{"type":"child","query":{"query\_string":{"query":"pizza monster","fields":\["body"\]}}}}}'
> echo ""
> curl -XGET localhost:9200/test/parent/\_search?pretty=true -d '{"query":{"has\_child":{"type":"child","query":{"query\_string":{"query":"pizza monster","fields":\["body"\]}}}}}'
> echo ""
> \`\`\`

Thanks!  
Mat

On Feb 7, 5:13 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> It sounds strange, it should return a value… . A recreation would help, thanks!
> 
> On Tuesday, February 7, 2012 at 12:10 AM, Matthew A. Brown wrote:
> 
> > Hi all,
> 
> > I've got a situation where running a top\_children query returns no  
> > results; running the same query wrapped in a has\_child returns the  
> > expected result. I know that top\_children may miss parent document  
> > results in the case where there are lots of matching children per  
> > parent; but since in this case top\_children returns nothing at all, it  
> > seems that that's not what's going on.
> 
> > I can try to put together a reduction, but first just wanted to make  
> > sure my understanding of top\_children/has\_child is correct, and that  
> > what I'm describing is not expected behavior.
> 
> > Thanks!  
> > Mat

---

<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:39am UTC](https://discuss.elastic.co/t/no-results-for-top-children-query-same-has-child-query-works/6609/4 "2017-07-06T03:39:07Z")

</div>


