# Has\_child does not work if own parent does not exist before

**URL:** <https://discuss.elastic.co/t/has-child-does-not-work-if-own-parent-does-not-exist-before/82282>\
**Category:** Elasticsearch\
**Created:** [April 13, 2017, 11:48am UTC](https://discuss.elastic.co/t/has-child-does-not-work-if-own-parent-does-not-exist-before/82282 "2017-04-13T11:48:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mattias\_Helmer](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@Mattias\_Helmer](https://discuss.elastic.co/u/Mattias_Helmer)\
**Post date:** [April 13, 2017, 11:48am UTC](https://discuss.elastic.co/t/has-child-does-not-work-if-own-parent-does-not-exist-before/82282/1 "2017-04-13T11:48:39Z")

</div>

Hi,  
in short, when I query for all mothers having any childern it only returns the mothers that also is a child of a mother. Even if the grandmother is created AFTER the mother, this mother will not be included in the result.

The below Sense commands describes the problem.

I am on ElasticSearch 2.3.1 by the way.

UPDATE: Noticed same behaviour on 5.3.0.

Regards / Mattias

DELETE test  
PUT test  
PUT test/kid/\_mapping  
{  
"kid": {  
"\_parent": {  
"type": "mom"  
},  
"\_routing": {  
"required": true  
},  
"properties": {  
"Name": {  
"type": "string"  
}  
}  
}  
}

PUT test/mom/\_mapping  
{  
"mom": {  
"\_parent": {  
"type": "grandma"  
},  
"\_routing": {  
"required": true  
},  
"properties": {  
"Name": {  
"type": "string"  
}  
}  
}  
}

PUT test/grandma/\_mapping  
{  
"grandma": {  
"properties": {  
"Name": {  
"type": "string"  
}  
}  
}  
}

// Create a grand mother  
PUT test/grandma/1?refresh=true  
{  
"Name": "Sue the grand mother"  
}

// Create an unreferenced grand mother  
PUT test/grandma/50?refresh=true  
{  
"Name": "Renate unreferenced"  
}

// Create a mother that is child of Sue  
PUT test/mom/1?parent=1&refresh=true  
{  
"Name": "Emma daugther of the grand mother"  
}

// Create a mother-less mother  
PUT test/mom/2?parent=100&refresh=true  
{  
"Name": "Mary mother-less mother"  
}

// Create a son of mother-less Mary  
PUT test/kid/1?parent=2&refresh=true  
{  
"Name": "Lester child of mother-less mother"  
}

// Create a son of Emma (tha has a mother)  
PUT test/kid/2?parent=1&refresh=true  
{  
"Name": "Sven the grand child"  
}

// Search for all mothers having children...returns only the mom (Emma) having a grandma  
GET test/mom/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"has\_child": {  
"type": "kid",  
"min\_children": 0,  
"query": {  
"match\_all": {}  
},  
"inner\_hits": {  
"size": 0  
}  
}  
}  
]  
}  
}  
}

// Create the missing grand mother (mother of Mary)  
PUT test/grandma/100?refresh=true  
{  
"Name": "Late grand mother"  
}

// Search for all mothers having children...still returns only the mom having a grandma created before itself  
GET test/mom/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"has\_child": {  
"type": "kid",  
"min\_children": 0,  
"query": {  
"match\_all": {}  
},  
"inner\_hits": {  
"size": 0  
}  
}  
}  
]  
}  
}  
}

---

<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:** [May 11, 2017, 11:49am UTC](https://discuss.elastic.co/t/has-child-does-not-work-if-own-parent-does-not-exist-before/82282/2 "2017-05-11T11:49:25Z")

</div>

Closed on May 11, 2017, 11:49am UTC

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