# Search child by parent field

**URL:** https://discuss.elastic.co/t/search-child-by-parent-field/6597
**Category:** Elasticsearch
**Created:** [February 5, 2012, 8:40pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597 "2012-02-05T20:40:35Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Igor\_Semenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_semenko/32/2911_2.png) [@Igor\_Semenko](https://discuss.elastic.co/u/Igor_Semenko)
#### Post date: [February 5, 2012, 8:40pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/1 "2012-02-05T20:40:35Z")

</div>

Hello, I'm exploring Parent-Child feature in elasticsearch, but can't  
understand whether it's possible to find child by parent's field name? Sort  
of reverse 'has\_child' query.

Here is example:

Author (parent)  
-name  
-country

Book (child of Author)

- title  
...

Can I search for all books author of which is from country:US for example?

Thanks.

---

<div class="post-metadata">

### Author: ![Radu\_Gheorghe1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe1/32/2688_2.png) [@Radu\_Gheorghe1](https://discuss.elastic.co/u/Radu_Gheorghe1)
#### Post date: [February 5, 2012, 9:40pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/2 "2012-02-05T21:40:02Z")

</div>

Would it be an option in these cases to do a query on books with a  
filter on author's country?

On 5 feb., 22:40, Igor Semenko [i...@semenko.com](mailto:i...@semenko.com) wrote:

> Hello, I'm exploring Parent-Child feature in elasticsearch, but can't  
> understand whether it's possible to find child by parent's field name? Sort  
> of reverse 'has\_child' query.
> 
> Here is example:
> 
> Author (parent)  
> -name  
> -country
> 
> Book (child of Author)
> 
> - title  
> ...
> 
> Can I search for all books author of which is from country:US for example?
> 
> Thanks.

---

<div class="post-metadata">

### Author: ![Igor\_Semenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_semenko/32/2911_2.png) [@Igor\_Semenko](https://discuss.elastic.co/u/Igor_Semenko)
#### Post date: [February 6, 2012, 8:31pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/3 "2012-02-06T20:31:59Z")

</div>

That would work, but I'm missing how to refer to the author in  
search/filter.

Mapping:

curl -XPUT '[http://localhost:9200/test/](http://localhost:9200/test/)'

> 

curl -XPUT '[http://localhost:9200/test/author/\_mapping](http://localhost:9200/test/author/_mapping)' -d '{

> ```
> "author" : {
> "properties" : {
> "name" : {"type" : "string"},
> "country" : {"type" : "string"}
> }
> }
> 
> ```
> 
> }  
> '

curl -XPUT '[http://localhost:9200/test/book/\_mapping](http://localhost:9200/test/book/_mapping)' -d '{

> ```
> "book" : {
> "_parent" : {"type" : "author"},
> "properties" : {
> "title" : {"type" : "string"}
> }
> }
> 
> ```
> 
> }

Query/Filter??

> curl -XGET '[http://localhost:9200/test/book/\_search](http://localhost:9200/test/book/_search)' -d '{  
> "explain" : true,  
> "query" : {  
> "bool" : {  
> "must" : {  
> "text" : {"title" : "book"}  
> },  
> "must" : {  
> "term" : _{"\_parent.country"_ : "us"}  
> }  
> }  
> }  
> }  
> ';

> curl -XGET '[http://localhost:9200/test/book/\_search](http://localhost:9200/test/book/_search)' -d '{  
> "explain" : true,  
> "query" : {  
> "text" : {"title" : "book"}  
> },  
> "filter" : {  
> "term": {"_author.country_" : "us"}  
> }  
> }  
> ';

Sample data

> curl -XPUT '[http://localhost:9200/test/author/1](http://localhost:9200/test/author/1)' -d '{  
> "name" : "author 1",  
> "country" : "us"  
> }'  
> curl -XPUT '[http://localhost:9200/test/author/2](http://localhost:9200/test/author/2)' -d '{  
> "name" : "author 2",  
> "country" : "uk"  
> }'  
> curl -XPUT '[http://localhost:9200/test/book/11?parent=1](http://localhost:9200/test/book/11?parent=1)' -d '  
> {  
> "title": "book 1 author 1"  
> }'  
> curl -XPUT '[http://localhost:9200/test/book/12?parent=1](http://localhost:9200/test/book/12?parent=1)' -d '  
> {  
> "title": "book 2 author 1"  
> }'  
> curl -XPUT '[http://localhost:9200/test/book/21?parent=2](http://localhost:9200/test/book/21?parent=2)' -d '  
> {  
> "title": "book 3 author 2"  
> }'

---

<div class="post-metadata">

### Author: ![Radu\_Gheorghe1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe1/32/2688_2.png) [@Radu\_Gheorghe1](https://discuss.elastic.co/u/Radu_Gheorghe1)
#### Post date: [February 7, 2012, 8:00am UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/4 "2012-02-07T08:00:45Z")

</div>

I'm so sorry, I totally misunderstood your question. I thought you  
were talking about nested documents, not parent/child types.

Now that I got it, I tried to find a solution, but failed miserably. I  
guess I would just use nested documents if I would need such  
functionality.

Apparently, you can refer to the parent's ID from within the child:  
[http://elasticsearch-users.115913.n3.nabble.com/Parent-child-search-td2166479.html](http://elasticsearch-users.115913.n3.nabble.com/Parent-child-search-td2166479.html)

I just tried that and I couldn't make it work, but I guess it's just  
my lack of knowledge. So, provided that you could make it work, you  
can search for the authors in "country":"us" or whatever, then get the  
IDs. Then search for books by adding an OR filter (or a bool query)  
for the parent's IDs that you just found.

---

<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: [February 7, 2012, 12:08pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/5 "2012-02-07T12:08:15Z")

</div>

No, there isn't a way to search based on parent data and get back children, unless you embed the parent data in the children...

On Sunday, February 5, 2012 at 10:40 PM, Igor Semenko wrote:

> Hello, I'm exploring Parent-Child feature in elasticsearch, but can't understand whether it's possible to find child by parent's field name? Sort of reverse 'has\_child' query.
> 
> Here is example:
> 
> Author (parent)  
> -name  
> -country
> 
> Book (child of Author)
> 
> - title  
> ...
> 
> Can I search for all books author of which is from country:US for example?
> 
> Thanks.

---

<div class="post-metadata">

### Author: ![Ludovic](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@Ludovic](https://discuss.elastic.co/u/Ludovic)
#### Post date: [February 7, 2012, 1:50pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/6 "2012-02-07T13:50:32Z")

</div>

HI,

I have a question about ES and Parent/Child :

is it possible to have nested has\_child filter (or query) ? now, we are  
trying to post a query with 2 levels of child (parent/child/grandchild).

_example of mapping :_  
{  
"settings":{  
"analysis":{  
"analyzer":{  
"key\_lowercase":{  
"tokenizer":"keyword",  
"filter":"lowercase"  
}  
}  
},  
"index":{  
"number\_of\_shards":5,  
"number\_of\_replicas":1  
}  
},  
"mappings":{  
"type0":{  
"properties":{...}  
},  
"type1":{  
"\_parent":{  
"type":"type0"  
},  
"properties":{...}  
}  
"type2":{  
"\_parent":{  
"type":"type1"  
},  
"properties":{...}  
}  
}  
}

\*example of query :  
\*  
{  
"query" : {  
"filtered" : {  
"query" : {  
"bool" : {  
"must" : [{...}, {...}]  
}  
},  
"filter" : {  
"has\_child" : {  
"type" : "type1",  
"query" : {  
"filtered" : {  
"query" : {  
"bool" : {  
"must" : [{...}]  
}  
},  
"filter" : {  
"has\_child" : {  
"type" : "type2",  
"query" : {  
"term" : {...}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}

Thanks.

---

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [July 23, 2013, 3:24pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/7 "2013-07-23T15:24:22Z")

</div>

Hi Shay. Is this still the case?

On Tuesday, 7 February 2012 07:08:15 UTC-5, kimchy wrote:

> No, there isn't a way to search based on parent data and get back  
> children, unless you embed the parent data in the children...

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)
#### Post date: [July 23, 2013, 3:48pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/8 "2013-07-23T15:48:10Z")

</div>

Are you looking for the has\_parent query/filter

> **[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.

> **[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.

Thanks,  
Matt Weber

On Tue, Jul 23, 2013 at 8:24 AM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:

> Hi Shay. Is this still the case?
> 
> On Tuesday, 7 February 2012 07:08:15 UTC-5, kimchy wrote:
> 
> > No, there isn't a way to search based on parent data and get back  
> > children, unless you embed the parent data in the children...
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [July 23, 2013, 3:57pm UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/9 "2013-07-23T15:57:41Z")

</div>

Indeed, I am. Thanks, Matt!

On Tuesday, 23 July 2013 11:48:10 UTC-4, Matt Weber wrote:

> Are you looking for the has\_parent query/filter
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-filter/)  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/has-parent-query/)
> 
> Thanks,  
> Matt Weber
> 
> On Tue, Jul 23, 2013 at 8:24 AM, Nick Hoffman \<[ni...@deadorange.com](mailto:ni...@deadorange.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi Shay. Is this still the case?
> > 
> > On Tuesday, 7 February 2012 07:08:15 UTC-5, kimchy wrote:
> > 
> > > No, there isn't a way to search based on parent data and get back  
> > > children, unless you embed the parent data in the children...
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:25am UTC](https://discuss.elastic.co/t/search-child-by-parent-field/6597/10 "2017-07-06T02:25:00Z")

</div>


