Parent/Child: Find children based on parent properties

Hi guys. With a parent/child mapping, is there a way to search for children
whose parents have certain property values?

For example, in the curl test in this gist, messages belong to users. How
can I find all messages belonging to a user whose firstname is "Nick"?

Thanks,
Nick

The way i employ for such problems is as follows

  1. Run the query on the parent and get all doc id's of the parent.
  2. Take all the matching parent ID's as an array and match it on _parent
    field (using terms query) over the child type.

Thanks
Vineeth

On Fri, Jun 29, 2012 at 10:33 AM, Nick Hoffman nick@deadorange.com wrote:

Hi guys. With a parent/child mapping, is there a way to search for
children whose parents have certain property values?

For example, in the curl test in this gist, messages belong to users. How
can I find all messages belonging to a user whose firstname is "Nick"?
ElasticSearch: Parent/Child: Find children based on parent properties · GitHub

Thanks,
Nick

Hey Vineeth. Thanks for the suggestion. That works most of the time.
However, it prevents you from being able to sort on the parent's fields.
Also, I'm not sure how well it'd work if a very large number of parent
documents match...say >10,000.

Is there no way to find children based on parent properties?

Thanks,
Nick

On Saturday, 30 June 2012 15:22:31 UTC-4, Vineeth Mohan wrote:

The way i employ for such problems is as follows

  1. Run the query on the parent and get all doc id's of the parent.
  2. Take all the matching parent ID's as an array and match it on _parent
    field (using terms query) over the child type.

Thanks
Vineeth

Hello Nick ,

I dont think you need to worry about the number of parent documents.
But then sorting based on parents field is really gonna be a problem.
My suggetion would be that if the parent field you wanna sort is small ,
duplicate it to the children too.
But again finding all parent ID and then going through the children is not
a good idea.

Meanwhile , you can check following links. This problem is pretty actively
discussed.

Thread -
http://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html
Issue - Add a has_parent query · Issue #792 · elastic/elasticsearch · GitHub

Thanks
Vineeth

On Sun, Jul 1, 2012 at 7:36 PM, Nick Hoffman nick@deadorange.com wrote:

Hey Vineeth. Thanks for the suggestion. That works most of the time.
However, it prevents you from being able to sort on the parent's fields.
Also, I'm not sure how well it'd work if a very large number of parent
documents match...say >10,000.

Is there no way to find children based on parent properties?

Thanks,
Nick

On Saturday, 30 June 2012 15:22:31 UTC-4, Vineeth Mohan wrote:

The way i employ for such problems is as follows

  1. Run the query on the parent and get all doc id's of the parent.
  2. Take all the matching parent ID's as an array and match it on _parent
    field (using terms query) over the child type.

Thanks
Vineeth

Hi Vineeth. Thanks for the links. They're very helpful indeed.

So it wouldn't matter if 20,000 parent docs match, and I use those 20,000
IDs in the query on the children? I figured the HTTP request for that query
would be so large that it wouldn't be accepted, ES would take a long time
to parse it, etc.

You said:
"finding all parent ID and then going through the children is not a good
idea"
There isn't an alternative at the moment though, is there?

Thanks,
Nick

On Sunday, 1 July 2012 11:13:57 UTC-4, Vineeth Mohan wrote:

Hello Nick ,

I dont think you need to worry about the number of parent documents.
But then sorting based on parents field is really gonna be a problem.
My suggetion would be that if the parent field you wanna sort is small ,
duplicate it to the children too.
But again finding all parent ID and then going through the children is not
a good idea.

Meanwhile , you can check following links. This problem is pretty actively
discussed.

Thread -
http://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html
Issue - Add a has_parent query · Issue #792 · elastic/elasticsearch · GitHub

Thanks
Vineeth

On Sun, Jul 1, 2012 at 7:36 PM, Nick Hoffman nick@deadorange.com wrote:

Hey Vineeth. Thanks for the suggestion. That works most of the time.
However, it prevents you from being able to sort on the parent's fields.
Also, I'm not sure how well it'd work if a very large number of parent
documents match...say >10,000.

Is there no way to find children based on parent properties?

Thanks,
Nick

On Saturday, 30 June 2012 15:22:31 UTC-4, Vineeth Mohan wrote:

The way i employ for such problems is as follows

  1. Run the query on the parent and get all doc id's of the parent.
  2. Take all the matching parent ID's as an array and match it on _parent
    field (using terms query) over the child type.

Thanks
Vineeth

Instances where 20,000 documents matches and going tru the children is not
actually possible or a good idea.
I am afraid there is not an alternative at the moment.

We also have a similar situation but then on our search result , each
search result was the parent document and on request we would show matching
children too.

Between if you want to have result based on your children and not parent ,
what is the point in creating parent/child relation ?

Thanks
Vineeth

On Wed, Jul 4, 2012 at 7:50 PM, Nick Hoffman nick@deadorange.com wrote:

Hi Vineeth. Thanks for the links. They're very helpful indeed.

So it wouldn't matter if 20,000 parent docs match, and I use those 20,000
IDs in the query on the children? I figured the HTTP request for that query
would be so large that it wouldn't be accepted, ES would take a long time
to parse it, etc.

You said:
"finding all parent ID and then going through the children is not a good
idea"
There isn't an alternative at the moment though, is there?

Thanks,
Nick

On Sunday, 1 July 2012 11:13:57 UTC-4, Vineeth Mohan wrote:

Hello Nick ,

I dont think you need to worry about the number of parent documents.
But then sorting based on parents field is really gonna be a problem.
My suggetion would be that if the parent field you wanna sort is small ,
duplicate it to the children too.
But again finding all parent ID and then going through the children is
not a good idea.

Meanwhile , you can check following links. This problem is pretty
actively discussed.

Thread - http://elasticsearch-users.115913.n3.nabble.com/Sorting-
by-sum-of-matching-children-**td3877730.htmlhttp://elasticsearch-users.115913.n3.nabble.com/Sorting-by-sum-of-matching-children-td3877730.html
Issue - https://github.com/**elasticsearch/elasticsearch/**issues/792https://github.com/elasticsearch/elasticsearch/issues/792

Thanks
Vineeth

On Sun, Jul 1, 2012 at 7:36 PM, Nick Hoffman nick@deadorange.com wrote:

Hey Vineeth. Thanks for the suggestion. That works most of the time.
However, it prevents you from being able to sort on the parent's fields.
Also, I'm not sure how well it'd work if a very large number of parent
documents match...say >10,000.

Is there no way to find children based on parent properties?

Thanks,
Nick

On Saturday, 30 June 2012 15:22:31 UTC-4, Vineeth Mohan wrote:

The way i employ for such problems is as follows

  1. Run the query on the parent and get all doc id's of the parent.
  2. Take all the matching parent ID's as an array and match it on
    _parent field (using terms query) over the child type.

Thanks
Vineeth

It's not possible to go through the children of 20,000 parents, eh? Might
you know what the upper limit is, or is it completely dependent upon the
resources that're allocated to ES?

My use case involves products and sales, where Product is the parent, and
Sale is the child. Each product can have zero or more copies for sale. I'm
trying to query for all sales whose parent matches various criteria.

Since the ratio of products-to-sales is way beyond 1:1, it'd be very
inefficient to store product fields in each sale.

Thanks for your continued advice, Vineeth. I really appreciate it.
Nick

On Wednesday, 4 July 2012 13:56:12 UTC-4, Vineeth Mohan wrote:

Instances where 20,000 documents matches and going tru the children is not
actually possible or a good idea.
I am afraid there is not an alternative at the moment.

We also have a similar situation but then on our search result , each
search result was the parent document and on request we would show matching
children too.

Between if you want to have result based on your children and not parent ,
what is the point in creating parent/child relation ?

Thanks
Vineeth