# Parent/child relations in elasticsearch

**URL:** https://discuss.elastic.co/t/parent-child-relations-in-elasticsearch/9772
**Category:** Elasticsearch
**Created:** [November 20, 2012, 11:25pm UTC](https://discuss.elastic.co/t/parent-child-relations-in-elasticsearch/9772 "2012-11-20T23:25:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![T\_Vinod\_Gupta](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@T\_Vinod\_Gupta](https://discuss.elastic.co/u/T_Vinod_Gupta)
#### Post date: [November 20, 2012, 11:25pm UTC](https://discuss.elastic.co/t/parent-child-relations-in-elasticsearch/9772/1 "2012-11-20T23:25:00Z")

</div>

hi,  
i have some questions regarding parent/child relationships in ES.

1. does 0.18.7 version support this feature? i mean both has\_child,  
has\_parent filter/queries. is top\_children a workaround for this?

2. it looks like the support is best for a simple parent/child relation but  
not a multi-generational case (  
[http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html](http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html)).  
in my case, we have 4 levels of hierarchy and the bottommost type can have  
multiple parents. so if the parents of the child node exist in different  
shards, then isnt there a conflict?

3. can the parent/child relation be established across indices?

thanks

--

---

<div class="post-metadata">

### Author: ![T\_Vinod\_Gupta](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@T\_Vinod\_Gupta](https://discuss.elastic.co/u/T_Vinod_Gupta)
#### Post date: [November 20, 2012, 11:53pm UTC](https://discuss.elastic.co/t/parent-child-relations-in-elasticsearch/9772/2 "2012-11-20T23:53:15Z")

</div>

Even in a simple parent/child relationship, if a child has many parents,  
how does ES ensure that when a new parent is indexed, both the new parent  
and existing child (and all its parents) get routed to the same shard?

thanks

On Tue, Nov 20, 2012 at 3:25 PM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)wrote:

> hi,  
> i have some questions regarding parent/child relationships in ES.
> 
> 1. does 0.18.7 version support this feature? i mean both has\_child,  
> has\_parent filter/queries. is top\_children a workaround for this?
> 
> 2. it looks like the support is best for a simple parent/child relation  
> but not a multi-generational case (  
> [http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html](http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html)).  
> in my case, we have 4 levels of hierarchy and the bottommost type can have  
> multiple parents. so if the parents of the child node exist in different  
> shards, then isnt there a conflict?
> 
> 3. can the parent/child relation be established across indices?
> 
> thanks

--

---

<div class="post-metadata">

### Author: ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)
#### Post date: [November 21, 2012, 6:56am UTC](https://discuss.elastic.co/t/parent-child-relations-in-elasticsearch/9772/3 "2012-11-21T06:56:56Z")

</div>

Hi,

multiple parents are not supported. Just single parent to many children.

Regards,  
Lukas

On Wed, Nov 21, 2012 at 12:53 AM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)wrote:

> Even in a simple parent/child relationship, if a child has many parents,  
> how does ES ensure that when a new parent is indexed, both the new parent  
> and existing child (and all its parents) get routed to the same shard?
> 
> thanks
> 
> On Tue, Nov 20, 2012 at 3:25 PM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)wrote:
> 
> > hi,  
> > i have some questions regarding parent/child relationships in ES.
> > 
> > 1. does 0.18.7 version support this feature? i mean both has\_child,  
> > has\_parent filter/queries. is top\_children a workaround for this?
> > 
> > 2. it looks like the support is best for a simple parent/child relation  
> > but not a multi-generational case (  
> > [http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html](http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html)).  
> > in my case, we have 4 levels of hierarchy and the bottommost type can have  
> > multiple parents. so if the parents of the child node exist in different  
> > shards, then isnt there a conflict?
> > 
> > 3. can the parent/child relation be established across indices?
> > 
> > thanks
> 
> --

--

---

<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, 8:45am UTC](https://discuss.elastic.co/t/parent-child-relations-in-elasticsearch/9772/4 "2012-11-21T08:45:56Z")

</div>

Hi,

1. Version 0.18.7 has parent child support, but this version doesn't  
support the has\_parent filter/query all other child like  
filters/queries are supported. The latest 0.19 and 0.20 versions have  
a more efficient parent/child implementation.

2. As is described in the referred email, you will need to use the top  
parent id as routing value for its sub parents and children documents  
during indexing. As Lukas wrote, a child document can only point to  
one parent document. That parent can point to a grand parent.

3. Parent and child document relation can only be established in the  
same index. The relation is established between types in the same  
index. Each document type (parent, child) has its own mapping  
describing the relation with the \_parent mapping.

Martijn

On 21 November 2012 07:56, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:

> Hi,
> 
> multiple parents are not supported. Just single parent to many children.
> 
> Regards,  
> Lukas
> 
> On Wed, Nov 21, 2012 at 12:53 AM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)  
> wrote:
> 
> > Even in a simple parent/child relationship, if a child has many parents,  
> > how does ES ensure that when a new parent is indexed, both the new parent  
> > and existing child (and all its parents) get routed to the same shard?
> > 
> > thanks
> > 
> > On Tue, Nov 20, 2012 at 3:25 PM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)  
> > wrote:
> > 
> > > hi,  
> > > i have some questions regarding parent/child relationships in ES.
> > > 
> > > 1. does 0.18.7 version support this feature? i mean both has\_child,  
> > > has\_parent filter/queries. is top\_children a workaround for this?
> > > 
> > > 2. it looks like the support is best for a simple parent/child relation  
> > > but not a multi-generational case  
> > > ([http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html](http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html)).  
> > > in my case, we have 4 levels of hierarchy and the bottommost type can have  
> > > multiple parents. so if the parents of the child node exist in different  
> > > shards, then isnt there a conflict?
> > > 
> > > 3. can the parent/child relation be established across indices?
> > > 
> > > thanks
> > 
> > --
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

### Author: ![T\_Vinod\_Gupta](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@T\_Vinod\_Gupta](https://discuss.elastic.co/u/T_Vinod_Gupta)
#### Post date: [November 21, 2012, 10:49pm UTC](https://discuss.elastic.co/t/parent-child-relations-in-elasticsearch/9772/5 "2012-11-21T22:49:49Z")

</div>

thanks all for the replies, it really helps!

On Wed, Nov 21, 2012 at 12:45 AM, Martijn v Groningen \<  
[martijn.v.groningen@gmail.com](mailto:martijn.v.groningen@gmail.com)\> wrote:

> Hi,
> 
> 1. Version 0.18.7 has parent child support, but this version doesn't  
> support the has\_parent filter/query all other child like  
> filters/queries are supported. The latest 0.19 and 0.20 versions have  
> a more efficient parent/child implementation.
> 
> 2. As is described in the referred email, you will need to use the top  
> parent id as routing value for its sub parents and children documents  
> during indexing. As Lukas wrote, a child document can only point to  
> one parent document. That parent can point to a grand parent.
> 
> 3. Parent and child document relation can only be established in the  
> same index. The relation is established between types in the same  
> index. Each document type (parent, child) has its own mapping  
> describing the relation with the \_parent mapping.
> 
> Martijn
> 
> On 21 November 2012 07:56, Lukáš Vlček [lukas.vlcek@gmail.com](mailto:lukas.vlcek@gmail.com) wrote:
> 
> > Hi,
> > 
> > multiple parents are not supported. Just single parent to many children.
> > 
> > Regards,  
> > Lukas
> > 
> > On Wed, Nov 21, 2012 at 12:53 AM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)  
> > wrote:
> > 
> > > Even in a simple parent/child relationship, if a child has many parents,  
> > > how does ES ensure that when a new parent is indexed, both the new  
> > > parent  
> > > and existing child (and all its parents) get routed to the same shard?
> > > 
> > > thanks
> > > 
> > > On Tue, Nov 20, 2012 at 3:25 PM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)  
> > > wrote:
> > > 
> > > > hi,  
> > > > i have some questions regarding parent/child relationships in ES.
> > > > 
> > > > 1. does 0.18.7 version support this feature? i mean both has\_child,  
> > > > has\_parent filter/queries. is top\_children a workaround for this?
> > > > 
> > > > 2. it looks like the support is best for a simple parent/child relation  
> > > > but not a multi-generational case  
> > > > (  
> > > > [http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html](http://elasticsearch-users.115913.n3.nabble.com/Querying-a-parent-type-that-is-also-a-child-type-for-another-type-td4023130.html)  
> > > > ).  
> > > > in my case, we have 4 levels of hierarchy and the bottommost type can  
> > > > have  
> > > > multiple parents. so if the parents of the child node exist in  
> > > > different  
> > > > shards, then isnt there a conflict?
> > > > 
> > > > 3. can the parent/child relation be established across indices?
> > > > 
> > > > thanks
> > > 
> > > --
> > 
> > --
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen
> 
> --

--

---

<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:03am UTC](https://discuss.elastic.co/t/parent-child-relations-in-elasticsearch/9772/6 "2017-07-06T03:03:20Z")

</div>


