# Confused when faceting on scope of has\_child query

**URL:** https://discuss.elastic.co/t/confused-when-faceting-on-scope-of-has-child-query/9775
**Category:** Elasticsearch
**Created:** [November 21, 2012, 11:11am UTC](https://discuss.elastic.co/t/confused-when-faceting-on-scope-of-has-child-query/9775 "2012-11-21T11:11:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Gaurav\_Bansal](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Gaurav\_Bansal](https://discuss.elastic.co/u/Gaurav_Bansal)
#### Post date: [November 21, 2012, 11:11am UTC](https://discuss.elastic.co/t/confused-when-faceting-on-scope-of-has-child-query/9775/1 "2012-11-21T11:11:27Z")

</div>

I have a confusion on the result of following gist  
: [https://gist.github.com/1551176](https://gist.github.com/1551176)  
The result correctly gives me 1 hit with '{"title": "One"}' having id =1  
,  
but looking at the facet count , it seems that children with parent=2 are  
also included !!

I was expecting only children having parent with property "title": "One"  
will be included in facets.  
Is there any way to achieve the same?

--

---

<div class="post-metadata">

### Author: ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)
#### Post date: [November 21, 2012, 3:29pm UTC](https://discuss.elastic.co/t/confused-when-faceting-on-scope-of-has-child-query/9775/2 "2012-11-21T15:29:12Z")

</div>

Hi Gaurav,

That is not how the scoped facets work. When associating a scope with  
a facet, it will take into account all documents that match in that  
scope. Your `has_child` inner query matches with all documents of type  
action. For that reason all action document are taken into account  
during the facet computation.

What might be worth trying out is to add a `has_parent` query inside  
the `has_child` query. The `has_parent` query should wrap same text  
query as is defined in the first must clause of your boolean query.

Martijn

On 21 November 2012 12:11, Gaurav Bansal [shehzada.gaurav@gmail.com](mailto:shehzada.gaurav@gmail.com) wrote:

> I have a confusion on the result of following gist :  
> [[ElasticSearch] Using facets when querying parent/child documents · GitHub](https://gist.github.com/1551176)  
> The result correctly gives me 1 hit with '{"title": "One"}' having id =1  
> ,  
> but looking at the facet count , it seems that children with parent=2 are  
> also included !!
> 
> I was expecting only children having parent with property "title": "One"  
> will be included in facets.  
> Is there any way to achieve the same?
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

### Author: ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)
#### Post date: [November 21, 2012, 8:33pm UTC](https://discuss.elastic.co/t/confused-when-faceting-on-scope-of-has-child-query/9775/3 "2012-11-21T20:33:03Z")

</div>

On 11/21/2012 7:29 AM, Martijn v Groningen wrote:

> Hi Gaurav,
> 
> That is not how the scoped facets work. When associating a scope with  
> a facet, it will take into account all documents that match in that  
> scope. Your `has_child` inner query matches with all documents of type  
> action. For that reason all action document are taken into account  
> during the facet computation.

Apparently, how does (if at all) the query about the parent combines  
with the query about the child that is in a has\_child to produce a scope  
of the children is so obvious to many that the documentation and the  
various posts seem to ignore any discussion about the role of the parent  
query. But it is not as obvious as it seems, judging by the number of  
people who ask about finding parents that match x and have children that  
match y then create a scope for faceting of these children thinking they  
ought to get children limited by both criteria.

"When associating a scope with a facet, it will take into account all  
documents that match in that scope. "

In the above "All documents that match" looks like a tautology or no  
useful information to me, but I think you meant that to describe the  
input even though match and scope are about the results of a match.  
Maybe it is clearer to say "When associating a scope with a facet, the  
query will consider all documents of the child type when matching." In  
fact, it doesn't depend on you defining a scope, so I think it is better  
to start over and say something to effect.

A has\_child query will consider _all documents of the child type_  
matching them against the query within the has\_child. This is without  
consideration to any query on the parent type that are outside the  
has\_child query. The parent query does not "pre-filter" which child  
documents to consider. Therefore, when you define a scope within a  
child query you may find in the scope more child items than are children  
of the matched parents.

When you want to match child types that match some criteria and facet on  
them, but also find some hits of parents that have at least one of the  
matching children, you can do the the following:  
search for parents types using a query that has two parts,  
(1) a has\_child query with a scope which will be used to build the  
facet, and  
(2) additional queries about the parent.

When you want to facet on _only_ the matching child types that match  
queries about the child types \*and also \*expressions about the parent type,  
search for the child type  
(1) use a has\_parent to limit the children, and  
(2) additional queries about the children

Since the search request is for the child type you do not need to  
introduce a new scope to identify these children.

Is the above accurate?

Just to be explicit since I have seen this several times since this past  
summer, if I now understand correctly, my opening sentence "how does (if  
at all) the query about the parent combine with the expression about the  
child /within/ the has\_child query to produce a resulting scope"? The  
answer is that the parent query has NO effect on the items in the  
resulting scope of the has\_child.

-Paul

> What might be worth trying out is to add a `has_parent` query inside  
> the `has_child` query. The `has_parent` query should wrap same text  
> query as is defined in the first must clause of your boolean query.
> 
> Martijn
> 
> On 21 November 2012 12:11, Gaurav Bansal [shehzada.gaurav@gmail.com](mailto:shehzada.gaurav@gmail.com) wrote:
> 
> > I have a confusion on the result of following gist :  
> > [[ElasticSearch] Using facets when querying parent/child documents · GitHub](https://gist.github.com/1551176)  
> > The result correctly gives me 1 hit with '{"title": "One"}' having id =1  
> > ,  
> > but looking at the facet count , it seems that children with parent=2 are  
> > also included !!
> > 
> > I was expecting only children having parent with property "title": "One"  
> > will be included in facets.  
> > Is there any way to achieve the same?
> > 
> > --

--

---

<div class="post-metadata">

### Author: ![Gaurav\_Bansal](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Gaurav\_Bansal](https://discuss.elastic.co/u/Gaurav_Bansal)
#### Post date: [November 22, 2012, 7:01am UTC](https://discuss.elastic.co/t/confused-when-faceting-on-scope-of-has-child-query/9775/4 "2012-11-22T07:01:28Z")

</div>

Thanks Martijn , Paul for the explanation, I got a better understanding of  
the above said features.

Martijn , your suggestion worked as i guessed the same , but unfortunately  
the query becomes more complex ( and inefficient also?? , I guess ) when i  
have a schema which has 1 parent which has 3-4 children of different  
types,  
and , I have a query on 1 children type ( say type\_a ) which has some props  
of its peer ( i.e child of type\_b ) , which i successfully wrote using a  
boolean query on type\_a properties and has\_parent query having has\_child  
(on type\_b) query wrapped inside.

Query just works fine , i get the correct result , but i was looking for a  
facet on the peer properties as well and the count of the same won't be  
taking the effect of parent query as Paul rightly said.

On Thu, Nov 22, 2012 at 2:05 AM, P. Hill [parehill1@gmail.com](mailto:parehill1@gmail.com) wrote:

> A has\_child query will consider _all documents of the child type_  
> matching them against the query within the has\_child

--

---

<div class="post-metadata">

### Author: ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)
#### Post date: [November 27, 2012, 8:46am UTC](https://discuss.elastic.co/t/confused-when-faceting-on-scope-of-has-child-query/9775/5 "2012-11-27T08:46:18Z")

</div>

On 11/21/2012 11:01 PM, Gaurav Bansal wrote:

> Paul for the explanation, .

I really hope I got it right because I'm just understanding it myself.

-Paul

--

---

<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:02am UTC](https://discuss.elastic.co/t/confused-when-faceting-on-scope-of-has-child-query/9775/6 "2017-07-06T03:02:50Z")

</div>


