# Parent child relation to 2nd level and more - has\_child query

**URL:** https://discuss.elastic.co/t/parent-child-relation-to-2nd-level-and-more-has-child-query/20994
**Category:** Elasticsearch
**Created:** [December 1, 2014, 4:24am UTC](https://discuss.elastic.co/t/parent-child-relation-to-2nd-level-and-more-has-child-query/20994 "2014-12-01T04:24:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Madhav\_Ayyagari](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Madhav\_Ayyagari](https://discuss.elastic.co/u/Madhav_Ayyagari)
#### Post date: [December 1, 2014, 4:24am UTC](https://discuss.elastic.co/t/parent-child-relation-to-2nd-level-and-more-has-child-query/20994/1 "2014-12-01T04:24:32Z")

</div>

I had partial success implementing parent child relation. I was able to  
create the mappings correctly (I presume), and post documents without any  
issue.  
In the example below, "family" index has types "dad", "child",  
"grandchild". "child" has parent type as "dad", and "grandchild" has parent  
type "child".

While using "has\_child" query, I could only get documents 1 level deep (dad  
to child), but not to the 2nd level (child to grandchild). See the last  
POST at the bottom that didn't return grandchild as expected.

- Can you please review whats wrong here (if any) ?
- Any idea on whats going on would be greatly appreciated!

_POST [http://localhost:9200/family](http://localhost:9200/family)_  
{  
"mappings": {  
"dad": {  
"properties": {  
"name": {  
"type": "string", "index": "not\_analyzed"  
}  
}  
},  
"child": {  
"\_parent": {  
"type": "dad"  
},  
"properties": {  
"name": {  
"type": "string", "index": "not\_analyzed"  
}  
}  
},  
"grandchild": {  
"\_parent": {  
"type": "child"  
},  
"properties": {  
"name": {  
"type": "string", "index": "not\_analyzed"  
}  
}  
}  
}  
}

_POST [http://localhost:9200/family/dad/1](http://localhost:9200/family/dad/1)_  
{  
"name" : "Dad1"  
}

_POST [http://localhost:9200/family/child/10?parent=1](http://localhost:9200/family/child/10?parent=1)_  
{  
"name" : "Child1",  
"\_parent" : "1"  
}

_POST [http://localhost:9200/family/grandchild/100?parent=10](http://localhost:9200/family/grandchild/100?parent=10)_  
{  
"name" : "GrandChild1",  
"\_parent" : "10"  
}

_POST [http://localhost:9200/family/dad/\_search](http://localhost:9200/family/dad/_search)_  
{  
"query" : {  
"has\_child" : {  
"type" : "child",  
"query" : {  
"match" : {  
"name" : "Child1"  
}  
}  
}  
}  
}

Response to the above POST:  
{  
"took": 2,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 1,  
"hits": [  
{  
"\_index": "family",  
"\_type": "dad",  
"\_id": "1",  
"\_score": 1,  
"\_source": {  
"name": "Dad1"  
}  
}  
]  
}  
}

_POST [http://localhost:9200/family/child/\_search](http://localhost:9200/family/child/_search)_  
{  
"query" : {  
"has\_child" : {  
"type" : "grandchild",  
"query" : {  
"match" : {  
"name" : "GrandChild1"  
}  
}  
}  
}  
}

Response to the above POST (it doesn't return "child" type with name  
"Child1"):  
{  
"took": 2,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 0,  
"max\_score": null,  
"hits": []  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d2cdb28d-1c8e-4054-b5a2-9d58cf811aa1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d2cdb28d-1c8e-4054-b5a2-9d58cf811aa1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Madhav\_Ayyagari](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Madhav\_Ayyagari](https://discuss.elastic.co/u/Madhav_Ayyagari)
#### Post date: [December 2, 2014, 5:03pm UTC](https://discuss.elastic.co/t/parent-child-relation-to-2nd-level-and-more-has-child-query/20994/2 "2014-12-02T17:03:06Z")

</div>

I solved the above problem by adding "routing" parameter to the POST to  
create grandchild. By default ES uses parent id of grandchild unless  
routing is explicitly specified (for example in URL). Here I specified  
routing to be the id of the dad (or grandparent from the perspective of the  
grandchild). After the routing is specified in creation, last POST to  
search child having the grandchild with name 'GrandChild1' succeeds.

FYI Relevant StackOverflow question & answer here.

> <https://stackoverflow.com/questions/15783420/elasticsearch-deeper-level-parent-child-relationship-grandchild>

POST [http://localhost:9200/family/grandchild/100?parent=10&routing=1](http://localhost:9200/family/grandchild/100?parent=10&routing=1)  
{  
"name" : "GrandChild1",  
"\_parent" : "10"  
}

On Sunday, November 30, 2014 10:24:32 PM UTC-6, Madhav A wrote:

> I had partial success implementing parent child relation. I was able to  
> create the mappings correctly (I presume), and post documents without any  
> issue.  
> In the example below, "family" index has types "dad", "child",  
> "grandchild". "child" has parent type as "dad", and "grandchild" has parent  
> type "child".
> 
> While using "has\_child" query, I could only get documents 1 level deep  
> (dad to child), but not to the 2nd level (child to grandchild). See the  
> last POST at the bottom that didn't return grandchild as expected.
> 
> - Can you please review whats wrong here (if any) ?
> - Any idea on whats going on would be greatly appreciated!
> 
> _POST [http://localhost:9200/family](http://localhost:9200/family) [http://localhost:9200/family](http://localhost:9200/family)_  
> {  
> "mappings": {  
> "dad": {  
> "properties": {  
> "name": {  
> "type": "string", "index": "not\_analyzed"  
> }  
> }  
> },  
> "child": {  
> "\_parent": {  
> "type": "dad"  
> },  
> "properties": {  
> "name": {  
> "type": "string", "index": "not\_analyzed"  
> }  
> }  
> },  
> "grandchild": {  
> "\_parent": {  
> "type": "child"  
> },  
> "properties": {  
> "name": {  
> "type": "string", "index": "not\_analyzed"  
> }  
> }  
> }  
> }  
> }
> 
> _POST [http://localhost:9200/family/dad/1](http://localhost:9200/family/dad/1)  
> [http://localhost:9200/family/dad/1](http://localhost:9200/family/dad/1)_  
> {  
> "name" : "Dad1"  
> }
> 
> _POST [http://localhost:9200/family/child/10?parent=1](http://localhost:9200/family/child/10?parent=1)  
> [http://localhost:9200/family/child/10?parent=1](http://localhost:9200/family/child/10?parent=1)_  
> {  
> "name" : "Child1",  
> "\_parent" : "1"  
> }
> 
> _POST [http://localhost:9200/family/grandchild/100?parent=10](http://localhost:9200/family/grandchild/100?parent=10)  
> [http://localhost:9200/family/grandchild/100?parent=10](http://localhost:9200/family/grandchild/100?parent=10)_  
> {  
> "name" : "GrandChild1",  
> "\_parent" : "10"  
> }
> 
> _POST [http://localhost:9200/family/dad/\_search](http://localhost:9200/family/dad/_search)  
> [http://localhost:9200/family/dad/\_search](http://localhost:9200/family/dad/_search)_  
> {  
> "query" : {  
> "has\_child" : {  
> "type" : "child",  
> "query" : {  
> "match" : {  
> "name" : "Child1"  
> }  
> }  
> }  
> }  
> }
> 
> Response to the above POST:  
> {  
> "took": 2,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 5,  
> "successful": 5,  
> "failed": 0  
> },  
> "hits": {  
> "total": 1,  
> "max\_score": 1,  
> "hits": [  
> {  
> "\_index": "family",  
> "\_type": "dad",  
> "\_id": "1",  
> "\_score": 1,  
> "\_source": {  
> "name": "Dad1"  
> }  
> }  
> ]  
> }  
> }
> 
> _POST [http://localhost:9200/family/child/\_search](http://localhost:9200/family/child/_search)  
> [http://localhost:9200/family/child/\_search](http://localhost:9200/family/child/_search)_  
> {  
> "query" : {  
> "has\_child" : {  
> "type" : "grandchild",  
> "query" : {  
> "match" : {  
> "name" : "GrandChild1"  
> }  
> }  
> }  
> }  
> }
> 
> Response to the above POST (it doesn't return "child" type with name  
> "Child1"):  
> {  
> "took": 2,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 5,  
> "successful": 5,  
> "failed": 0  
> },  
> "hits": {  
> "total": 0,  
> "max\_score": null,  
> "hits":   
> }  
> }

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/122083d0-0feb-43a3-9835-3519ca8ff2f6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/122083d0-0feb-43a3-9835-3519ca8ff2f6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:46am UTC](https://discuss.elastic.co/t/parent-child-relation-to-2nd-level-and-more-has-child-query/20994/3 "2017-07-06T00:46:33Z")

</div>


