# Parent child search

**URL:** <https://discuss.elastic.co/t/parent-child-search/3711>\
**Category:** Elasticsearch\
**Created:** [December 30, 2010, 7:26am UTC](https://discuss.elastic.co/t/parent-child-search/3711 "2010-12-30T07:26:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arulkumar](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Arulkumar](https://discuss.elastic.co/u/Arulkumar)\
**Post date:** [December 30, 2010, 7:26am UTC](https://discuss.elastic.co/t/parent-child-search/3711/1 "2010-12-30T07:26:28Z")

</div>

Hi all

1). I have created a parent table named "dbparent" with 3 child  
tables(child1, child2 child3).

2). In parent i have created 3 rows, and the same i have added the  
rows for child table.

1. If i search like below

'{"query" : {"has\_child" : {"type" : "child", "query" :{"term" :  
{"tag" : "something"} } } } }'

Result of this query is parent of the child.

Questions:

1. Here i need a result as both parent and child record.?

2. Is it possible to get child's of a parent?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [December 30, 2010, 11:21am UTC](https://discuss.elastic.co/t/parent-child-search/3711/2 "2010-12-30T11:21:44Z")

</div>

1. Still not implemented (getting the children per "parent" hit).
2. You can do a term query: "term" : { "child\_type.\_parent" : "parent\_id" }

On Thu, Dec 30, 2010 at 9:26 AM, Arulkumar [arulkumarcs@gmail.com](mailto:arulkumarcs@gmail.com) wrote:

> Hi all
> 
> 1). I have created a parent table named "dbparent" with 3 child  
> tables(child1, child2 child3).
> 
> 2). In parent i have created 3 rows, and the same i have added the  
> rows for child table.
> 
> 1. If i search like below
> 
> '{"query" : {"has\_child" : {"type" : "child", "query" :{"term" :  
> {"tag" : "something"} } } } }'
> 
> Result of this query is parent of the child.
> 
> Questions:
> 
> 1. Here i need a result as both parent and child record.?
> 
> 2. Is it possible to get child's of a parent?

---

<div class="post-metadata">

**Author:** ![Arulkumar](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Arulkumar](https://discuss.elastic.co/u/Arulkumar)\
**Post date:** [December 30, 2010, 11:26am UTC](https://discuss.elastic.co/t/parent-child-search/3711/3 "2010-12-30T11:26:00Z")

</div>

thank you shay

On Dec 30, 4:21 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> 1. Still not implemented (getting the children per "parent" hit).
> 2. You can do a term query: "term" : { "child\_type.\_parent" : "parent\_id" }
> 
> On Thu, Dec 30, 2010 at 9:26 AM, Arulkumar [arulkuma...@gmail.com](mailto:arulkuma...@gmail.com) wrote:
> 
> > Hi all
> 
> > 1). I have created a parent table named "dbparent" with 3 child  
> > tables(child1, child2 child3).
> 
> > 2). In parent i have created 3 rows, and the same i have added the  
> > rows for child table.
> 
> > 1. If i search like below
> 
> > ```
> > curl -XGET 'http://192.168.0.12:9200/dbparent1/_search'-d
> > 
> > ```
> > 
> > '{"query" : {"has\_child" : {"type" : "child", "query" :{"term" :  
> > {"tag" : "something"} } } } }'
> 
> > Result of this query is parent of the child.
> 
> > Questions:
> 
> > 1. Here i need a result as both parent and child record.?
> 
> > 1. Is it possible to get child's of a parent?

---

<div class="post-metadata">

**Author:** ![Arulkumar](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Arulkumar](https://discuss.elastic.co/u/Arulkumar)\
**Post date:** [December 30, 2010, 11:41am UTC](https://discuss.elastic.co/t/parent-child-search/3711/4 "2010-12-30T11:41:53Z")

</div>

can you explain in detail with sample query about "child\_type.\_parent"  
and about parent\_id?

On Dec 30, 4:26 pm, Arulkumar [arulkuma...@gmail.com](mailto:arulkuma...@gmail.com) wrote:

> thank you shay
> 
> On Dec 30, 4:21 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > 1. Still not implemented (getting the children per "parent" hit).
> > 2. You can do a term query: "term" : { "child\_type.\_parent" : "parent\_id" }
> 
> > On Thu, Dec 30, 2010 at 9:26 AM, Arulkumar [arulkuma...@gmail.com](mailto:arulkuma...@gmail.com) wrote:
> > 
> > > Hi all
> 
> > > 1). I have created a parent table named "dbparent" with 3 child  
> > > tables(child1, child2 child3).
> 
> > > 2). In parent i have created 3 rows, and the same i have added the  
> > > rows for child table.
> 
> > > 1. If i search like below
> 
> > > ```
> > > curl -XGET 'http://192.168.0.12:9200/dbparent1/_search'-d
> > > 
> > > ```
> > > 
> > > '{"query" : {"has\_child" : {"type" : "child", "query" :{"term" :  
> > > {"tag" : "something"} } } } }'
> 
> > > Result of this query is parent of the child.
> 
> > > Questions:
> 
> > > 1. Here i need a result as both parent and child record.?
> 
> > > 1. Is it possible to get child's of a parent?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [December 30, 2010, 10:55pm UTC](https://discuss.elastic.co/t/parent-child-search/3711/5 "2010-12-30T22:55:51Z")

</div>

What don't you understand specifically? the child\_type.\_parent is the  
ability to use type\_name.field\_name notation. Something like tweet.message  
(so the result is automatically filtered to tweet types). The "parent\_id" in  
question is simply the value of the parent id you want to find childs for.  
Wrap it in a term query and execute a search request with it, and you will  
get the children of a specific parent.

On Thu, Dec 30, 2010 at 1:41 PM, Arulkumar [arulkumarcs@gmail.com](mailto:arulkumarcs@gmail.com) wrote:

> can you explain in detail with sample query about "child\_type.\_parent"  
> and about parent\_id?
> 
> On Dec 30, 4:26 pm, Arulkumar [arulkuma...@gmail.com](mailto:arulkuma...@gmail.com) wrote:
> 
> > thank you shay
> > 
> > On Dec 30, 4:21 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > 1. Still not implemented (getting the children per "parent" hit).
> > > 2. You can do a term query: "term" : { "child\_type.\_parent" :  
> > > "parent\_id" }
> > 
> > > On Thu, Dec 30, 2010 at 9:26 AM, Arulkumar [arulkuma...@gmail.com](mailto:arulkuma...@gmail.com)  
> > > wrote:
> > > 
> > > > Hi all
> > 
> > > > 1). I have created a parent table named "dbparent" with 3 child  
> > > > tables(child1, child2 child3).
> > 
> > > > 2). In parent i have created 3 rows, and the same i have added the  
> > > > rows for child table.
> > 
> > > > 1. If i search like below
> > 
> > > > ```
> > > > curl -XGET 'http://192.168.0.12:9200/dbparent1/_search'-d
> > > > 
> > > > ```
> > > > 
> > > > '{"query" : {"has\_child" : {"type" : "child", "query" :{"term" :  
> > > > {"tag" : "something"} } } } }'
> > 
> > > > Result of this query is parent of the child.
> > 
> > > > Questions:
> > 
> > > > 1. Here i need a result as both parent and child record.?
> > 
> > > > 1. Is it possible to get child's of a parent?

---

<div class="post-metadata">

**Author:** ![byuva](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@byuva](https://discuss.elastic.co/u/byuva)\
**Post date:** [January 27, 2011, 6:23pm UTC](https://discuss.elastic.co/t/parent-child-search/3711/6 "2011-01-27T18:23:41Z")

</div>

Can this also be accomplished through a StringQuery? I would like users to be able to find all children with a certain parent (without having to store the parent\_id independently). I am not having much luck with a StringQuery "\_parent:ID1234567" when applied to the the child\_type.

-b

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 28, 2011, 9:27pm UTC](https://discuss.elastic.co/t/parent-child-search/3711/7 "2011-01-28T21:27:26Z")

</div>

Can you gist a curl recreation of this? It should work.  
On Thursday, January 27, 2011 at 8:23 PM, byuva wrote:

> Can this also be accomplished through a StringQuery? I would like users to  
> be able to find all children with a certain parent (without having to store  
> the parent\_id independently). I am not having much luck with a StringQuery  
> "\_parent:ID1234567" when applied to the the child\_type.
> 
> ## -b
> 
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Parent-child-search-tp2166479p2363405.html](http://elasticsearch-users.115913.n3.nabble.com/Parent-child-search-tp2166479p2363405.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![byuva](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@byuva](https://discuss.elastic.co/u/byuva)\
**Post date:** [January 30, 2011, 5:49pm UTC](https://discuss.elastic.co/t/parent-child-search/3711/8 "2011-01-30T17:49:06Z")

</div>

I have it figured out now. I had been unable to see the actual content of the \_parent field (per another thread here) and hadn't realized that the name was slightly mangled (eg "my\_parent\_type#my\_id). My apologies if I missed this in the documentation.

-brian

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 30, 2011, 7:19pm UTC](https://discuss.elastic.co/t/parent-child-search/3711/9 "2011-01-30T19:19:48Z")

</div>

That should not be the case, opened an issue for this: [Search: When fetching \_parent, only the \_id should be returned, and not type#id · Issue #658 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/issue/658) and pushed a fix.

As for matching on \_parent field, you can simple do a term query on child\_type.\_parent:parent\_id.  
On Sunday, January 30, 2011 at 7:49 PM, byuva wrote:

> I have it figured out now. I had been unable to see the actual content of  
> the \_parent field (per another thread here) and hadn't realized that the  
> name was slightly mangled (eg "my\_parent\_type#my\_id). My apologies if I  
> missed this in the documentation.
> 
> -brian
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Parent-child-search-tp2166479p2384355.html](http://elasticsearch-users.115913.n3.nabble.com/Parent-child-search-tp2166479p2384355.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 4:13am UTC](https://discuss.elastic.co/t/parent-child-search/3711/10 "2017-07-06T04:13:09Z")

</div>


