Parent Child


(Arya Saha) #1

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


Parent Child Relation
(Christian Dahlqvist) #2

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


(Arya Saha) #3
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"
}
}
}


(Christian Dahlqvist) #4

Try this instead:

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.


(Arya Saha) #5

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?


(Christian Dahlqvist) #6

Have you specified an appropriate mapping for you data? 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.


(system) #7

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