# Querying a parent type that is also a child type for another type

**URL:** https://discuss.elastic.co/t/querying-a-parent-type-that-is-also-a-child-type-for-another-type/9138
**Category:** Elasticsearch
**Created:** [September 25, 2012, 5:25pm UTC](https://discuss.elastic.co/t/querying-a-parent-type-that-is-also-a-child-type-for-another-type/9138 "2012-09-25T17:25:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![andreasch](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@andreasch](https://discuss.elastic.co/u/andreasch)
#### Post date: [September 25, 2012, 5:25pm UTC](https://discuss.elastic.co/t/querying-a-parent-type-that-is-also-a-child-type-for-another-type/9138/1 "2012-09-25T17:25:41Z")

</div>

I have a case with 2 parent/child mappings where a contact is a parent of  
an order and an order is a parent of a product. I am using a search query  
and a has\_child filter that matches everything.  
Searching on contact returns all contacts as expected. However, when I run  
the same search query on the orders it only returns 1 of the orders.

Is there an issue when using has\_child on a parent type that is also a  
child of another type?

I am providing a gist that shows the mappings, the indexed data and the 2  
querying examples:

> <https://gist.github.com/andreasch/3783212>
>
> There are more than three files. show original

Thanks,  
Andreas

--

---

<div class="post-metadata">

### Author: ![andreasch](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@andreasch](https://discuss.elastic.co/u/andreasch)
#### Post date: [September 25, 2012, 9:24pm UTC](https://discuss.elastic.co/t/querying-a-parent-type-that-is-also-a-child-type-for-another-type/9138/2 "2012-09-25T21:24:52Z")

</div>

I forgot to mention that if I remove the parent mapping from order to  
contact then the query returns the correct results.

On Tuesday, September 25, 2012 10:25:41 AM UTC-7, Andreas Christoforides  
wrote:

> I have a case with 2 parent/child mappings where a contact is a parent of  
> an order and an order is a parent of a product. I am using a search query  
> and a has\_child filter that matches everything.  
> Searching on contact returns all contacts as expected. However, when I run  
> the same search query on the orders it only returns 1 of the orders.
> 
> Is there an issue when using has\_child on a parent type that is also a  
> child of another type?
> 
> I am providing a gist that shows the mappings, the indexed data and the 2  
> querying examples:
> 
> [Querying a parent type that is also a child type for another type · GitHub](https://gist.github.com/3783212)
> 
> Thanks,  
> Andreas

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [September 25, 2012, 9:53pm UTC](https://discuss.elastic.co/t/querying-a-parent-type-that-is-also-a-child-type-for-another-type/9138/3 "2012-09-25T21:53:39Z")

</div>

The has\_child request can only work if parents and children are indexed in  
the same shard. By default, shards for child documents are determined based  
on parent ids. In your case it works fine on the contact-orders level:  
orders are indexed correctly based on contact ids. But when you index  
products, the parent field contains the order id, so the products are  
getting indexed into wrong shards. You can switch to a single shard index  
or specify contact id in the routing parameter when you index  
products: [Querying a parent type that is also a child type for another type · GitHub](https://gist.github.com/3784684)

On Tuesday, September 25, 2012 5:24:52 PM UTC-4, Andreas Christoforides  
wrote:

> I forgot to mention that if I remove the parent mapping from order to  
> contact then the query returns the correct results.
> 
> On Tuesday, September 25, 2012 10:25:41 AM UTC-7, Andreas Christoforides  
> wrote:
> 
> > I have a case with 2 parent/child mappings where a contact is a parent of  
> > an order and an order is a parent of a product. I am using a search query  
> > and a has\_child filter that matches everything.  
> > Searching on contact returns all contacts as expected. However, when I  
> > run the same search query on the orders it only returns 1 of the orders.
> > 
> > Is there an issue when using has\_child on a parent type that is also a  
> > child of another type?
> > 
> > I am providing a gist that shows the mappings, the indexed data and the 2  
> > querying examples:
> > 
> > [Querying a parent type that is also a child type for another type · GitHub](https://gist.github.com/3783212)
> > 
> > Thanks,  
> > Andreas

--

---

<div class="post-metadata">

### Author: ![andreasch](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@andreasch](https://discuss.elastic.co/u/andreasch)
#### Post date: [September 25, 2012, 10:48pm UTC](https://discuss.elastic.co/t/querying-a-parent-type-that-is-also-a-child-type-for-another-type/9138/4 "2012-09-25T22:48:51Z")

</div>

Igor,

Thanks for the explanation. I was mistakenly under the impression that the  
appropriate routing of types with respect to parent/child relationships was  
done automatically.  
It seems a bit cumbersome to propagate the ID of the top parent all the way  
down to each indexing operation. This will be even harder to maintain when  
you have 4-5 levels in the parent/child hierarchy.

I wonder if it would be possible for Elasticsearch to determine the shard  
of a parent document at the time a child is being indexed.

On Tuesday, September 25, 2012 2:53:39 PM UTC-7, Igor Motov wrote:

> The has\_child request can only work if parents and children are indexed in  
> the same shard. By default, shards for child documents are determined based  
> on parent ids. In your case it works fine on the contact-orders level:  
> orders are indexed correctly based on contact ids. But when you index  
> products, the parent field contains the order id, so the products are  
> getting indexed into wrong shards. You can switch to a single shard index  
> or specify contact id in the routing parameter when you index products:  
> [Querying a parent type that is also a child type for another type · GitHub](https://gist.github.com/3784684)
> 
> On Tuesday, September 25, 2012 5:24:52 PM UTC-4, Andreas Christoforides  
> wrote:
> 
> > I forgot to mention that if I remove the parent mapping from order to  
> > contact then the query returns the correct results.
> > 
> > On Tuesday, September 25, 2012 10:25:41 AM UTC-7, Andreas Christoforides  
> > wrote:
> > 
> > > I have a case with 2 parent/child mappings where a contact is a parent  
> > > of an order and an order is a parent of a product. I am using a search  
> > > query and a has\_child filter that matches everything.  
> > > Searching on contact returns all contacts as expected. However, when I  
> > > run the same search query on the orders it only returns 1 of the orders.
> > > 
> > > Is there an issue when using has\_child on a parent type that is also a  
> > > child of another type?
> > > 
> > > I am providing a gist that shows the mappings, the indexed data and the  
> > > 2 querying examples:
> > > 
> > > [Querying a parent type that is also a child type for another type · GitHub](https://gist.github.com/3783212)
> > > 
> > > Thanks,  
> > > Andreas

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [September 26, 2012, 12:45pm UTC](https://discuss.elastic.co/t/querying-a-parent-type-that-is-also-a-child-type-for-another-type/9138/5 "2012-09-26T12:45:47Z")

</div>

I think it should be possible to implement something like this in  
elasticsearch, but I am not sure if that would be worth it considering how  
much complexity and limitations it would bring. It would require for each  
insert operation to lookup the parent by broadcasting to all shards and  
require the parent to exist when children are indexed. I think in most  
cases, it would be more efficient to just propagate top level ID or come up  
with some other routing value that would be consistently applied to the top  
parent and all its descendants.

On Tuesday, September 25, 2012 6:48:51 PM UTC-4, Andreas Christoforides  
wrote:

> Igor,
> 
> Thanks for the explanation. I was mistakenly under the impression that the  
> appropriate routing of types with respect to parent/child relationships was  
> done automatically.  
> It seems a bit cumbersome to propagate the ID of the top parent all the  
> way down to each indexing operation. This will be even harder to maintain  
> when you have 4-5 levels in the parent/child hierarchy.
> 
> I wonder if it would be possible for Elasticsearch to determine the shard  
> of a parent document at the time a child is being indexed.
> 
> On Tuesday, September 25, 2012 2:53:39 PM UTC-7, Igor Motov wrote:
> 
> > The has\_child request can only work if parents and children are indexed  
> > in the same shard. By default, shards for child documents are determined  
> > based on parent ids. In your case it works fine on the contact-orders  
> > level: orders are indexed correctly based on contact ids. But when you  
> > index products, the parent field contains the order id, so the products are  
> > getting indexed into wrong shards. You can switch to a single shard index  
> > or specify contact id in the routing parameter when you index products:  
> > [Querying a parent type that is also a child type for another type · GitHub](https://gist.github.com/3784684)
> > 
> > On Tuesday, September 25, 2012 5:24:52 PM UTC-4, Andreas Christoforides  
> > wrote:
> > 
> > > I forgot to mention that if I remove the parent mapping from order to  
> > > contact then the query returns the correct results.
> > > 
> > > On Tuesday, September 25, 2012 10:25:41 AM UTC-7, Andreas Christoforides  
> > > wrote:
> > > 
> > > > I have a case with 2 parent/child mappings where a contact is a parent  
> > > > of an order and an order is a parent of a product. I am using a search  
> > > > query and a has\_child filter that matches everything.  
> > > > Searching on contact returns all contacts as expected. However, when I  
> > > > run the same search query on the orders it only returns 1 of the orders.
> > > > 
> > > > Is there an issue when using has\_child on a parent type that is also a  
> > > > child of another type?
> > > > 
> > > > I am providing a gist that shows the mappings, the indexed data and the  
> > > > 2 querying examples:
> > > > 
> > > > [Querying a parent type that is also a child type for another type · GitHub](https://gist.github.com/3783212)
> > > > 
> > > > Thanks,  
> > > > Andreas

--

---

<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:11am UTC](https://discuss.elastic.co/t/querying-a-parent-type-that-is-also-a-child-type-for-another-type/9138/6 "2017-07-06T03:11:20Z")

</div>


