# Filtering parent documents in has\_child query

**URL:** <https://discuss.elastic.co/t/filtering-parent-documents-in-has-child-query/6803>\
**Category:** Elasticsearch\
**Created:** [February 24, 2012, 7:31pm UTC](https://discuss.elastic.co/t/filtering-parent-documents-in-has-child-query/6803 "2012-02-24T19:31:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkat](https://avatars.discourse-cdn.com/v4/letter/v/4491bb/32.png) [@venkat](https://discuss.elastic.co/u/venkat)\
**Post date:** [February 24, 2012, 7:31pm UTC](https://discuss.elastic.co/t/filtering-parent-documents-in-has-child-query/6803/1 "2012-02-24T19:31:29Z")

</div>

Hi,  
I am trying to understand how the parent-child model works in  
elasticsearch. I am able to create parent-child mapping and the  
documents. I was able to run the following query

curl-XGET "localhost:9200/test/\_search?pretty=true"-d'{  
"query": {  
"has\_child": {  
"type": "chld",  
"query": {  
"term": {  
"created": "today"  
}  
}  
}'

and it returns me list of parent documents for the matching children.  
I want to filter the matching parent docs further like

curl-XGET "localhost:9200/test/\_search?pretty=true"-d'{  
"query": {  
"has\_child": {  
"type": "chld",  
"query": {  
"term": {  
"created": "today"  
}  
}  
},  
"term": {  
"status": "free"  
}  
}'

Is this possible or is it my query that's not correct?

thanks

---

<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 26, 2012, 7:35pm UTC](https://discuss.elastic.co/t/filtering-parent-documents-in-has-child-query/6803/2 "2012-02-26T19:35:48Z")

</div>

You query is not formed correctly, you just put pasted two queries in the "query" element. You need to wrap it in a book query (probably placing both in a must clause).

On Friday, February 24, 2012 at 9:31 PM, venkat wrote:

> Hi,  
> I am trying to understand how the parent-child model works in  
> elasticsearch. I am able to create parent-child mapping and the  
> documents. I was able to run the following query
> 
> curl-XGET "localhost:9200/test/\_search?pretty=true"-d'{  
> "query": {  
> "has\_child": {  
> "type": "chld",  
> "query": {  
> "term": {  
> "created": "today"  
> }  
> }  
> }'
> 
> and it returns me list of parent documents for the matching children.  
> I want to filter the matching parent docs further like
> 
> curl-XGET "localhost:9200/test/\_search?pretty=true"-d'{  
> "query": {  
> "has\_child": {  
> "type": "chld",  
> "query": {  
> "term": {  
> "created": "today"  
> }  
> }  
> },  
> "term": {  
> "status": "free"  
> }  
> }'
> 
> Is this possible or is it my query that's not correct?
> 
> thanks

---

<div class="post-metadata">

**Author:** ![venkat](https://avatars.discourse-cdn.com/v4/letter/v/4491bb/32.png) [@venkat](https://discuss.elastic.co/u/venkat)\
**Post date:** [February 28, 2012, 9:23pm UTC](https://discuss.elastic.co/t/filtering-parent-documents-in-has-child-query/6803/3 "2012-02-28T21:23:12Z")

</div>

That works. thanks.

On Feb 26, 11:35 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> You query is not formed correctly, you just put pasted two queries in the "query" element. You need to wrap it in a book query (probably placing both in a must clause).
> 
> On Friday, February 24, 2012 at 9:31 PM, venkat wrote:
> 
> > Hi,  
> > I am trying to understand how the parent-child model works in  
> > elasticsearch. I am able to create parent-child mapping and the  
> > documents. I was able to run the following query
> 
> > curl-XGET "localhost:9200/test/\_search?pretty=true"-d'{  
> > "query": {  
> > "has\_child": {  
> > "type": "chld",  
> > "query": {  
> > "term": {  
> > "created": "today"  
> > }  
> > }  
> > }'
> 
> > and it returns me list of parent documents for the matching children.  
> > I want to filter the matching parent docs further like
> 
> > curl-XGET "localhost:9200/test/\_search?pretty=true"-d'{  
> > "query": {  
> > "has\_child": {  
> > "type": "chld",  
> > "query": {  
> > "term": {  
> > "created": "today"  
> > }  
> > }  
> > },  
> > "term": {  
> > "status": "free"  
> > }  
> > }'
> 
> > Is this possible or is it my query that's not correct?
> 
> > thanks

---

<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, 3:37am UTC](https://discuss.elastic.co/t/filtering-parent-documents-in-has-child-query/6803/4 "2017-07-06T03:37:57Z")

</div>


