# Queries on has\_parent / has\_child don't return any hits

**URL:** <https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587>\
**Category:** Elasticsearch\
**Created:** [December 4, 2019, 7:49pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587 "2019-12-04T19:49:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chari](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@chari](https://discuss.elastic.co/u/chari)\
**Post date:** [December 4, 2019, 7:49pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587/1 "2019-12-04T19:49:58Z")

</div>

I have a very simple join relationship that has been defined as :  
es.indices.create( index= "docpage", body=

```
{
     "mappings": {
            "properties": {
                 "my_document": {
                    "type": "text"
                 },
                "my_page": {
                    "type": "text"
                 },
                "my_join_field": { 
                   "type": "join",
                   "relations": {
                     "my_document": "my_page" 
                   }
                }
            }
          }
    }

```

)

I am indexing a parent document using  
es\_d = es.index(index="docpage",doc\_type="\_doc",id = 1, body={"text": "I am the parent", "my\_join\_field": "my\_document"})

I am indexing child documents with  
pages[0]='I am child 1'  
pages[1]='I am child 2'  
pages[2]='I am child 3'  
pages[3]='I am child 4'  
pages[4]='I am child 5'  
for page in pages:

```
 res_c = es.index(index="docpage",doc_type="_doc",routing=1,body=

       {
           "text": page,
           "my-join-field": {
                "name": "my_page",
                "parent": "1"
            }
        }) 

```

When I look for all entries in the index using  
res\_doc = es.search(index="docpage", body=  
{

```
    "query" : {       
        "match_all": {}
    }

```

})  
I see the document and 5 pages in the index.  
{'took': 3, 'timed\_out': False, '\_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 6, 'relation': 'eq'}, 'max\_score': 1.0, 'hits': [{'\_index': 'docpage', '\_type': '\_doc', '\_id': '1', '\_score': 1.0, '\_source': {'text': 'I am the parent', 'my\_join\_field': 'my\_document'}}, {'\_index': 'docpage', '\_type': '\_doc', '\_id': 'QGxe0m4BLD2e5CPzVs6X', '\_score': 1.0, '\_routing': '1', '\_source': {'text': 'I am child 1', 'my-join-field': {'name': 'my\_page', 'parent': '1'}}}, {'\_index': 'docpage', '\_type': '\_doc', '\_id': 'QWxe0m4BLD2e5CPzVs79', '\_score': 1.0, '\_routing': '1', '\_source': {'text': 'I am child 2', 'my-join-field': {'name': 'my\_page', 'parent': '1'}}}, {'\_index': 'docpage', '\_type': '\_doc', '\_id': 'Qmxe0m4BLD2e5CPzV84L', '\_score': 1.0, '\_routing': '1', '\_source': {'text': 'I am child 3', 'my-join-field': {'name': 'my\_page', 'parent': '1'}}}, {'\_index': 'docpage', '\_type': '\_doc', '\_id': 'Q2xe0m4BLD2e5CPzV84X', '\_score': 1.0, '\_routing': '1', '\_source': {'text': 'I am child 4', 'my-join-field': {'name': 'my\_page', 'parent': '1'}}}, {'\_index': 'docpage', '\_type': '\_doc', '\_id': 'RGxe0m4BLD2e5CPzV84m', '\_score': 1.0, '\_routing': '1', '\_source': {'text': 'I am child 5', 'my-join-field': {'name': 'my\_page', 'parent': '1'}}}]}}

But when I try to use has\_parent or has\_child, I get no hits. This basic query doesn't return any hits.  
res\_doc = es.search(index="docpage", body=  
{

```
   "query" : {

        "has_parent" : {         
            "parent_type": "my_document",
            "query": {
                "match": {
                   "id": "1"
                 }
             }
        } 
    }

```

}  
)

Any ideas will be appreciated.  
Thanks.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 6, 2019, 12:23pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587/2 "2019-12-06T12:23:20Z")

</div>

not sure your query is the right one, as there is no field named `id` in that document from what I have seen in the snippets?

---

<div class="post-metadata">

**Author:** ![chari](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@chari](https://discuss.elastic.co/u/chari)\
**Post date:** [December 6, 2019, 3:04pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587/3 "2019-12-06T15:04:20Z")

</div>

Thanks for your reply. Yes, it should be \_id and not id.  
I tried "\_id": "1" but it didn't work.

---

<div class="post-metadata">

**Author:** ![chari](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@chari](https://discuss.elastic.co/u/chari)\
**Post date:** [December 6, 2019, 5:43pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587/4 "2019-12-06T17:43:26Z")

</div>

The weird thing is that an equivalent curl sequence of commands seems to work.

---

<div class="post-metadata">

**Author:** ![chari](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@chari](https://discuss.elastic.co/u/chari)\
**Post date:** [December 6, 2019, 8:06pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587/5 "2019-12-06T20:06:15Z")

</div>

Narrowed it down to an issue with invoking es.index for each page.  
When I do  
!curl -XPOST 'localhost:9200/docpage/\_doc/?routing=1' -H 'Content-Type: application/json' -d '{  
"text": "I am child 1",  
"my\_join\_field": {  
"name": "my\_page",  
"parent": "1"  
}  
}'  
I can see this page in the has\_parent query(see above in thread).  
When I do  
res\_c = es.index(index="docpage",doc\_type="\_doc",routing=1,  
body={ "text": "I am child 2",  
"my-join-field": {  
"name": "my\_page",  
"parent": 1  
}  
}  
)  
I don't see this page in has\_parent query.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [December 9, 2019, 1:42pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587/6 "2019-12-09T13:42:16Z")

</div>

hm. I am just speculating here, but this could mean, that the routing parameter is not applied properly?

when you execute a search (one that finds documents) and you add `explain: true` to it, you can see what shard the document is in, that might help to rule this out.

---

<div class="post-metadata">

**Author:** ![chari](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@chari](https://discuss.elastic.co/u/chari)\
**Post date:** [December 9, 2019, 4:03pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587/7 "2019-12-09T16:03:45Z")

</div>

Thanks for your reply. It turned out to be a typo : I was using my-join-field while I had created the parent-child mapping using the name my\_join\_field !

---

<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:** [January 6, 2020, 4:03pm UTC](https://discuss.elastic.co/t/queries-on-has-parent-has-child-dont-return-any-hits/210587/8 "2020-01-06T16:03:46Z")

</div>

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