# Unable to search for parent and child documents using Elasticsearch?

**URL:** <https://discuss.elastic.co/t/unable-to-search-for-parent-and-child-documents-using-elasticsearch/10215>\
**Category:** Elasticsearch\
**Created:** [January 2, 2013, 10:41am UTC](https://discuss.elastic.co/t/unable-to-search-for-parent-and-child-documents-using-elasticsearch/10215 "2013-01-02T10:41:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Moo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_moo/32/2562_2.png) [@Jonathan\_Moo](https://discuss.elastic.co/u/Jonathan_Moo)\
**Post date:** [January 2, 2013, 10:41am UTC](https://discuss.elastic.co/t/unable-to-search-for-parent-and-child-documents-using-elasticsearch/10215/1 "2013-01-02T10:41:22Z")

</div>

I have indexed a child document in Elasticsearch as follows:

{  
\_parent: chow-demo  
\_index: prototype\_2013.01.02  
\_type: chow-clfg  
\_id: l7jvEgD-SrG-Ws\_9ab52IA  
\_score: 11.600378  
\_source: {  
chow-clfg: {  
@type: chow-clfg  
clfg: Cg5iUlAvU9IIAAAA8Rg  
@timestamp: 2013-01-02T10:15:00.000Z  
count: 1  
}  
}  
}

The parent has at least 2 records that has _clfg_ as a common identifier as  
follows:

{  
\_index: "prototype\_2013.01.02",  
\_type: chow-demo,  
clfg: Cg5iUlAvU9IIAAAA8Rg,  
obid: 97066795  
}

However when I query using the "has\_child" query, I get no results:

curl -XGET "localhost:9200:/prototype\_2013.01.02/chow-demo/\_search"  
{  
"query": {  
"has\_child": {  
"type": "chow-clfg",  
"query": {  
"term": {  
"clfg": "Cg5iUlAvU9IIAAAA8Rg"  
}  
}  
}  
}  
}

However when I query individually for the documents, there were correct  
results returned. Why is that so? Isn't the parent routing supposed to be  
able to identify results using the child using the parent field?

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [January 2, 2013, 12:28pm UTC](https://discuss.elastic.co/t/unable-to-search-for-parent-and-child-documents-using-elasticsearch/10215/2 "2013-01-02T12:28:54Z")

</div>

Hi,

What were the exact index curl commands and did you use a the \_parent  
mapping? Can you perhaps create a gist, it would be easier  
to see what is going wrong.

Martijn

On 2 January 2013 11:41, Jonathan Moo [webmusing@gmail.com](mailto:webmusing@gmail.com) wrote:

> I have indexed a child document in Elasticsearch as follows:
> 
> {  
> \_parent: chow-demo  
> \_index: prototype\_2013.01.02  
> \_type: chow-clfg  
> \_id: l7jvEgD-SrG-Ws\_9ab52IA  
> \_score: 11.600378  
> \_source: {  
> chow-clfg: {  
> @type: chow-clfg  
> clfg: Cg5iUlAvU9IIAAAA8Rg  
> @timestamp: 2013-01-02T10:15:00.000Z  
> count: 1  
> }  
> }  
> }
> 
> The parent has at least 2 records that has clfg as a common identifier as  
> follows:
> 
> {  
> \_index: "prototype\_2013.01.02",  
> \_type: chow-demo,  
> clfg: Cg5iUlAvU9IIAAAA8Rg,  
> obid: 97066795  
> }
> 
> However when I query using the "has\_child" query, I get no results:
> 
> curl -XGET "localhost:9200:/prototype\_2013.01.02/chow-demo/\_search"  
> {  
> "query": {  
> "has\_child": {  
> "type": "chow-clfg",  
> "query": {  
> "term": {  
> "clfg": "Cg5iUlAvU9IIAAAA8Rg"  
> }  
> }  
> }  
> }  
> }
> 
> However when I query individually for the documents, there were correct  
> results returned. Why is that so? Isn't the parent routing supposed to be  
> able to identify results using the child using the parent field?
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Jonathan\_Moo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_moo/32/2562_2.png) [@Jonathan\_Moo](https://discuss.elastic.co/u/Jonathan_Moo)\
**Post date:** [January 3, 2013, 3:11am UTC](https://discuss.elastic.co/t/unable-to-search-for-parent-and-child-documents-using-elasticsearch/10215/3 "2013-01-03T03:11:12Z")

</div>

Hi Martijn,

Thanks for helping, I have created the gist  
at [Unable to search for parent and child documents using Elasticsearch · GitHub](https://gist.github.com/4440320).

My curl command structure is as follows:

curl -XPOST "localhost:9200/prototype\_2013.01.03/chow-demo/\_search" -d  
'{  
"query": {  
"has\_child": {  
"type": "chow-clfg",  
"query": {  
"term": {  
"clfg": "Cg5iU1BHZ88kAAAAYAE"  
}  
}  
}  
}  
}'

And I have used the \_parent mapping as "chow-demo", but I am unable to get  
results.

Jonathan

On Wednesday, 2 January 2013 20:28:54 UTC+8, Martijn v Groningen wrote:

> Hi,
> 
> What were the exact index curl commands and did you use a the \_parent  
> mapping? Can you perhaps create a gist, it would be easier  
> to see what is going wrong.
> 
> Martijn
> 
> On 2 January 2013 11:41, Jonathan Moo \<[webm...@gmail.com](mailto:webm...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > I have indexed a child document in Elasticsearch as follows:
> > 
> > {  
> > \_parent: chow-demo  
> > \_index: prototype\_2013.01.02  
> > \_type: chow-clfg  
> > \_id: l7jvEgD-SrG-Ws\_9ab52IA  
> > \_score: 11.600378  
> > \_source: {  
> > chow-clfg: {  
> > @type: chow-clfg  
> > clfg: Cg5iUlAvU9IIAAAA8Rg  
> > @timestamp: 2013-01-02T10:15:00.000Z  
> > count: 1  
> > }  
> > }  
> > }
> > 
> > The parent has at least 2 records that has clfg as a common identifier  
> > as  
> > follows:
> > 
> > {  
> > \_index: "prototype\_2013.01.02",  
> > \_type: chow-demo,  
> > clfg: Cg5iUlAvU9IIAAAA8Rg,  
> > obid: 97066795  
> > }
> > 
> > However when I query using the "has\_child" query, I get no results:
> > 
> > curl -XGET "localhost:9200:/prototype\_2013.01.02/chow-demo/\_search"  
> > {  
> > "query": {  
> > "has\_child": {  
> > "type": "chow-clfg",  
> > "query": {  
> > "term": {  
> > "clfg": "Cg5iUlAvU9IIAAAA8Rg"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > However when I query individually for the documents, there were correct  
> > results returned. Why is that so? Isn't the parent routing supposed to  
> > be  
> > able to identify results using the child using the parent field?
> > 
> > --
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [January 3, 2013, 9:16am UTC](https://discuss.elastic.co/t/unable-to-search-for-parent-and-child-documents-using-elasticsearch/10215/4 "2013-01-03T09:16:27Z")

</div>

Hi Jonathan,

I created a gist based on your gist:

> <https://gist.github.com/martijnvg/4442042>

I couldn't find your index curl commands in your gist. I added these  
to my gist. The search request does return hits in this gist.  
I also removed the @type and @timestamp fields. I think you just want  
to use the timestamp mapping, right?:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Martijn

On 3 January 2013 04:11, Jonathan Moo [webmusing@gmail.com](mailto:webmusing@gmail.com) wrote:

> Hi Martijn,
> 
> Thanks for helping, I have created the gist at  
> [Unable to search for parent and child documents using Elasticsearch · GitHub](https://gist.github.com/4440320).
> 
> My curl command structure is as follows:
> 
> curl -XPOST "localhost:9200/prototype\_2013.01.03/chow-demo/\_search" -d  
> '{  
> "query": {  
> "has\_child": {  
> "type": "chow-clfg",  
> "query": {  
> "term": {  
> "clfg": "Cg5iU1BHZ88kAAAAYAE"  
> }  
> }  
> }  
> }  
> }'
> 
> And I have used the \_parent mapping as "chow-demo", but I am unable to get  
> results.
> 
> Jonathan
> 
> On Wednesday, 2 January 2013 20:28:54 UTC+8, Martijn v Groningen wrote:
> 
> > Hi,
> > 
> > What were the exact index curl commands and did you use a the \_parent  
> > mapping? Can you perhaps create a gist, it would be easier  
> > to see what is going wrong.
> > 
> > Martijn
> > 
> > On 2 January 2013 11:41, Jonathan Moo [webm...@gmail.com](mailto:webm...@gmail.com) wrote:
> > 
> > > I have indexed a child document in Elasticsearch as follows:
> > > 
> > > {  
> > > \_parent: chow-demo  
> > > \_index: prototype\_2013.01.02  
> > > \_type: chow-clfg  
> > > \_id: l7jvEgD-SrG-Ws\_9ab52IA  
> > > \_score: 11.600378  
> > > \_source: {  
> > > chow-clfg: {  
> > > @type: chow-clfg  
> > > clfg: Cg5iUlAvU9IIAAAA8Rg  
> > > @timestamp: 2013-01-02T10:15:00.000Z  
> > > count: 1  
> > > }  
> > > }  
> > > }
> > > 
> > > The parent has at least 2 records that has clfg as a common identifier  
> > > as  
> > > follows:
> > > 
> > > {  
> > > \_index: "prototype\_2013.01.02",  
> > > \_type: chow-demo,  
> > > clfg: Cg5iUlAvU9IIAAAA8Rg,  
> > > obid: 97066795  
> > > }
> > > 
> > > However when I query using the "has\_child" query, I get no results:
> > > 
> > > curl -XGET "localhost:9200:/prototype\_2013.01.02/chow-demo/\_search"  
> > > {  
> > > "query": {  
> > > "has\_child": {  
> > > "type": "chow-clfg",  
> > > "query": {  
> > > "term": {  
> > > "clfg": "Cg5iUlAvU9IIAAAA8Rg"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > However when I query individually for the documents, there were correct  
> > > results returned. Why is that so? Isn't the parent routing supposed to  
> > > be  
> > > able to identify results using the child using the parent field?
> > > 
> > > --
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Jonathan\_Moo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_moo/32/2562_2.png) [@Jonathan\_Moo](https://discuss.elastic.co/u/Jonathan_Moo)\
**Post date:** [January 4, 2013, 4:06am UTC](https://discuss.elastic.co/t/unable-to-search-for-parent-and-child-documents-using-elasticsearch/10215/5 "2013-01-04T04:06:53Z")

</div>

Hi Martijn,

I think I have a misunderstanding about parent/child mappings. I thought  
when I specified \_parent as the chow-demo type, it is able to match exact  
fields in the parent document as well as the child documents automatically.  
Hence I assumed that the term "clfg" in every parent document in the type  
"chow-demo" can be queried against with the child document's "clfg" field.

However the actual "\_parent" field maps the child document to the parent's  
ID, hence since there is no mapping to a specific ID but instead it is  
mapped to a parent's type, i get no results.

I probably need to restructure my architecture a bit to fit the  
parent/child mapping in Elasticsearch. Thanks so much for your help!

Regards,  
Jonathan

On Thursday, 3 January 2013 17:16:27 UTC+8, Martijn v Groningen wrote:

> Hi Jonathan,
> 
> I created a gist based on your gist:  
> [test.sh · GitHub](https://gist.github.com/4442042)
> 
> I couldn't find your index curl commands in your gist. I added these  
> to my gist. The search request does return hits in this gist.  
> I also removed the @type and @timestamp fields. I think you just want  
> to use the timestamp mapping, right?:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/timestamp-field.html)
> 
> Martijn
> 
> On 3 January 2013 04:11, Jonathan Moo \<[webm...@gmail.com](mailto:webm...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Hi Martijn,
> > 
> > Thanks for helping, I have created the gist at  
> > [Unable to search for parent and child documents using Elasticsearch · GitHub](https://gist.github.com/4440320).
> > 
> > My curl command structure is as follows:
> > 
> > curl -XPOST "localhost:9200/prototype\_2013.01.03/chow-demo/\_search" -d  
> > '{  
> > "query": {  
> > "has\_child": {  
> > "type": "chow-clfg",  
> > "query": {  
> > "term": {  
> > "clfg": "Cg5iU1BHZ88kAAAAYAE"  
> > }  
> > }  
> > }  
> > }  
> > }'
> > 
> > And I have used the \_parent mapping as "chow-demo", but I am unable to  
> > get  
> > results.
> > 
> > Jonathan
> > 
> > On Wednesday, 2 January 2013 20:28:54 UTC+8, Martijn v Groningen wrote:
> > 
> > > Hi,
> > > 
> > > What were the exact index curl commands and did you use a the \_parent  
> > > mapping? Can you perhaps create a gist, it would be easier  
> > > to see what is going wrong.
> > > 
> > > Martijn
> > > 
> > > On 2 January 2013 11:41, Jonathan Moo [webm...@gmail.com](mailto:webm...@gmail.com) wrote:
> > > 
> > > > I have indexed a child document in Elasticsearch as follows:
> > > > 
> > > > {  
> > > > \_parent: chow-demo  
> > > > \_index: prototype\_2013.01.02  
> > > > \_type: chow-clfg  
> > > > \_id: l7jvEgD-SrG-Ws\_9ab52IA  
> > > > \_score: 11.600378  
> > > > \_source: {  
> > > > chow-clfg: {  
> > > > @type: chow-clfg  
> > > > clfg: Cg5iUlAvU9IIAAAA8Rg  
> > > > @timestamp: 2013-01-02T10:15:00.000Z  
> > > > count: 1  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > The parent has at least 2 records that has clfg as a common  
> > > > identifier  
> > > > as  
> > > > follows:
> > > > 
> > > > {  
> > > > \_index: "prototype\_2013.01.02",  
> > > > \_type: chow-demo,  
> > > > clfg: Cg5iUlAvU9IIAAAA8Rg,  
> > > > obid: 97066795  
> > > > }
> > > > 
> > > > However when I query using the "has\_child" query, I get no results:
> > > > 
> > > > curl -XGET "localhost:9200:/prototype\_2013.01.02/chow-demo/\_search"  
> > > > {  
> > > > "query": {  
> > > > "has\_child": {  
> > > > "type": "chow-clfg",  
> > > > "query": {  
> > > > "term": {  
> > > > "clfg": "Cg5iUlAvU9IIAAAA8Rg"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > However when I query individually for the documents, there were  
> > > > correct  
> > > > results returned. Why is that so? Isn't the parent routing supposed  
> > > > to  
> > > > be  
> > > > able to identify results using the child using the parent field?
> > > > 
> > > > --
> > > 
> > > --  
> > > Met vriendelijke groet,
> > > 
> > > Martijn van Groningen
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--

---

<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, 2:57am UTC](https://discuss.elastic.co/t/unable-to-search-for-parent-and-child-documents-using-elasticsearch/10215/6 "2017-07-06T02:57:52Z")

</div>


