# Has\_parent

**URL:** https://discuss.elastic.co/t/has-parent/51943
**Category:** Elasticsearch
**Created:** [June 6, 2016, 12:29pm UTC](https://discuss.elastic.co/t/has-parent/51943 "2016-06-06T12:29:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![webdevsHub](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@webdevsHub](https://discuss.elastic.co/u/webdevsHub)
#### Post date: [June 6, 2016, 12:29pm UTC](https://discuss.elastic.co/t/has-parent/51943/1 "2016-06-06T12:29:01Z")

</div>

Hi, I have problems understanding how that "has\_parent" query works.

Situation:  
Index with 2 types: "parent\_type" and "child\_type" with simple parent-child mapping:

"parent\_type" =\> {"properties": {...}}  
"child\_type" =\> {"\_parent": {"type": "parent\_type"},"properties": {...}}

Every parent document has exact one child document. Elasticsearch version 1.3.

Now I run two count queries on "child\_type" ([http://localhost:9200/index/child\_type/\_count](http://localhost:9200/index/child_type/_count)):

{  
"query": {  
"has\_parent": {  
"type": "parent\_type",  
"query": {  
"match\_all": {}  
}  
}  
}  
}

and

{  
"query": {  
"match\_all": {}  
}  
}

The first query counts less documents then the second query. I do not understand why there is a difference?

---

<div class="post-metadata">

### Author: ![sanzhiyuan](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@sanzhiyuan](https://discuss.elastic.co/u/sanzhiyuan)
#### Post date: [June 6, 2016, 12:40pm UTC](https://discuss.elastic.co/t/has-parent/51943/2 "2016-06-06T12:40:11Z")

</div>

1, some child documents don't have a parent document?  
2, missing \_routing?

---

<div class="post-metadata">

### Author: ![webdevsHub](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@webdevsHub](https://discuss.elastic.co/u/webdevsHub)
#### Post date: [June 6, 2016, 2:51pm UTC](https://discuss.elastic.co/t/has-parent/51943/3 "2016-06-06T14:51:45Z")

</div>

1: This is not the error. I used that query to move documents to a new index and some of them were not moved. Later I checked if there are child-docs with no parents and all of them had valid parents in old index. As far as I know it is not possible to index documents into a type with parent mapping but without providing parent-id in the index-request.

2: I do not have '\_routing' mapping. Is this mandatory when using parent-child relationship?

---

<div class="post-metadata">

### Author: ![sanzhiyuan](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@sanzhiyuan](https://discuss.elastic.co/u/sanzhiyuan)
#### Post date: [June 7, 2016, 2:10am UTC](https://discuss.elastic.co/t/has-parent/51943/4 "2016-06-07T02:10:16Z")

</div>

[https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-routing-field.html)  
[https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html)

and it says,  
Elasticsearch maintains a map of which parents are associated with which children. It is thanks to this map that query-time joins are fast, but it does place a limitation on the parent-child relationship: the parent document and all of its children must live on the same shard.

---

<div class="post-metadata">

### Author: ![webdevsHub](https://avatars.discourse-cdn.com/v4/letter/w/5fc32e/32.png) [@webdevsHub](https://discuss.elastic.co/u/webdevsHub)
#### Post date: [June 7, 2016, 3:59pm UTC](https://discuss.elastic.co/t/has-parent/51943/5 "2016-06-07T15:59:38Z")

</div>

I did additional research and found out that parents are always used as default routing value. So you do not need to care about routing in parent-child relations.

Turns out your very first guess was true: some child documents don't have a parent document. I mixed up something during my tests...

Thanks for your replies sanzhiyuan!

---

<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 5, 2017, 10:45pm UTC](https://discuss.elastic.co/t/has-parent/51943/6 "2017-07-05T22:45:33Z")

</div>


