# Parent Child

**URL:** <https://discuss.elastic.co/t/parent-child/135666>\
**Category:** Elasticsearch\
**Created:** [June 13, 2018, 9:31am UTC](https://discuss.elastic.co/t/parent-child/135666 "2018-06-13T09:31:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arya\_Saha](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Arya\_Saha](https://discuss.elastic.co/u/Arya_Saha)\
**Post date:** [June 13, 2018, 9:31am UTC](https://discuss.elastic.co/t/parent-child/135666/1 "2018-06-13T09:31:15Z")

</div>

Say I have two indices like following  
(Say Parent)  
PUT dept?pretty  
POST dept/department/\_bulk  
{"index":{"\_id":"1"}}  
{"Dept\_ID":1,"Dept\_name":"Engineering"}  
{"index":{"\_id":"2"}}  
{"Dept\_ID":2,"Dept\_name":"Medical"}

(Say Child)  
PUT dept\_student?pretty  
POST dept\_student/dept/\_bulk  
{"index":{"\_id":"1"}}  
{"Emp\_name":"Arya","Dept\_ID":1,"Marks":150}  
{"index":{"\_id":"2"}}  
{"Emp\_name":"Banashree","Dept\_ID":1,"Marks":200}  
{"index":{"\_id":"3"}}  
{"Emp\_name":"Arnab","Dept\_ID":2,"Marks":120}  
{"index":{"\_id":"4"}}  
{"Emp\_name":"Abhirup","Dept\_ID":2,"Marks":120}

I want to perform Parent child relationship.Please help me out..

Actually I want to build a report in kibana like following--  
[Dept\_Name] [Marks]  
Engineering 350  
Medical 240

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [June 13, 2018, 9:34am UTC](https://discuss.elastic.co/t/parent-child/135666/2 "2018-06-13T09:34:15Z")

</div>

Kibana does currently not support parent-child, so I would recommend denomalizing the model and store the department name on each employee record.

---

<div class="post-metadata">

**Author:** ![Arya\_Saha](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Arya\_Saha](https://discuss.elastic.co/u/Arya_Saha)\
**Post date:** [June 13, 2018, 9:45am UTC](https://discuss.elastic.co/t/parent-child/135666/3 "2018-06-13T09:45:23Z")

</div>

```
PUT dept?pretty
POST /dept/department/_bulk
{"index":{"_id":"1"}}
{"Dept_ID":1,"Dept_name":"Engineering"}
{"index":{"_id":"2"}}
{"Dept_ID":2,"Dept_name":"Medical"}

PUT dept_student?pretty
POST /dept/dept_student/_bulk
{"index":{"_id":"100","parent":"1"}}
{"Emp_name":"Arya","Dept_ID":1,"Marks":150}
{"index":{"_id":"200","parent":"1"}}
{"Emp_name":"Banashree","Dept_ID":1,"Marks":200}
{"index":{"_id":"300","parent":"2"}}
{"Emp_name":"Arnab","Dept_ID":2,"Marks":120}
{"index":{"_id":"400","parent":"2"}}
{"Emp_name":"Abhirup","Dept_ID":2,"Marks":120}

```

Getting the below error msg...  
"index": {  
"\_index": "dept",  
"\_type": "dept\_student",  
"\_id": "200",  
"status": 400,  
"error": {  
"type": "illegal\_argument\_exception",  
"reason": "can't specify parent if no parent field has been configured"  
}  
}  
}been configured"  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [June 13, 2018, 10:02am UTC](https://discuss.elastic.co/t/parent-child/135666/4 "2018-06-13T10:02:34Z")

</div>

Try this instead:

```auto
POST /dept/dept_student/_bulk
{"index":{"_id":"100","parent":"1"}}
{"Emp_name":"Arya","Dept_ID":1,"Dept_name":"Engineering","Marks":150}
{"index":{"_id":"200","parent":"1"}}
{"Emp_name":"Banashree","Dept_ID":1,"Dept_name":"Engineering","Marks":200}
{"index":{"_id":"300","parent":"2"}}
{"Emp_name":"Arnab","Dept_ID":2,"Dept_name":"Medical","Marks":120}
{"index":{"_id":"400","parent":"2"}}
{"Emp_name":"Abhirup","Dept_ID":2,"Dept_name":"Medical","Marks":120}

```

As Elasticsearch is not a relational system, the best way to work with it is often to denormalise data rather than try using parent-child or nested document to emulate a relational model.

---

<div class="post-metadata">

**Author:** ![Arya\_Saha](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@Arya\_Saha](https://discuss.elastic.co/u/Arya_Saha)\
**Post date:** [June 13, 2018, 10:08am UTC](https://discuss.elastic.co/t/parent-child/135666/5 "2018-06-13T10:08:03Z")

</div>

I got your point,it's better to denormalise data.  
But if I want to perform parent child relationship then could you please tell me the mistakes that I have made in my code?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [June 13, 2018, 10:20am UTC](https://discuss.elastic.co/t/parent-child/135666/6 "2018-06-13T10:20:20Z")

</div>

Have you specified an [appropriate mapping for you data](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/parent-join.html#_multiple_children_per_parent)? You will also need to make sure you have indexed data using routing as shown in the link I provided. You can not perform parent-child queries if this is not in place.

---

<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 11, 2018, 10:20am UTC](https://discuss.elastic.co/t/parent-child/135666/7 "2018-07-11T10:20:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
