Confused when faceting on scope of has_child query

I have a confusion on the result of following gist
: 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?

--

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 wrote:

I have a confusion on the result of following gist :
[ElasticSearch] Using facets when querying parent/child documents · GitHub
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

--

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 wrote:

I have a confusion on the result of following gist :
[ElasticSearch] Using facets when querying parent/child documents · GitHub
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?

--

--

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 wrote:

A has_child query will consider all documents of the child type
matching them against the query within the has_child

--

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

--