# Filtered has\_child query?

**URL:** <https://discuss.elastic.co/t/filtered-has-child-query/22609>\
**Category:** Elasticsearch\
**Created:** [March 10, 2015, 11:23pm UTC](https://discuss.elastic.co/t/filtered-has-child-query/22609 "2015-03-10T23:23:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![asanderson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@asanderson](https://discuss.elastic.co/u/asanderson)\
**Post date:** [March 10, 2015, 11:23pm UTC](https://discuss.elastic.co/t/filtered-has-child-query/22609/1 "2015-03-10T23:23:26Z")

</div>

Is a filtered has\_child query possible where the filter is applied to the  
child document before the query of the has\_child?

e.g. Given the example below...

curl -X PUT "[http://localhost:9200/foobar](http://localhost:9200/foobar)" -d  
"{"mappings":{"Foo":{},"Bar":{"\_parent":{"type":"Foo"}}}}"

curl -X PUT "[http://localhost:9200/foobar/Foo/1](http://localhost:9200/foobar/Foo/1)"  
-d "{"foo":"abc","access":"yes"}"

curl -X PUT "[http://localhost:9200/foobar/Bar/2?parent=1](http://localhost:9200/foobar/Bar/2?parent=1)"  
-d "{"bar":"xyz","access":"no"}"

The following filtered query should not return a result, if the filter was  
being applied to the child document first.

{  
"query": {  
"filtered": {  
"query": {  
"has\_child": {  
"type": "Bar",  
"query": {  
"term": {  
"bar": "xyz"  
}  
}  
}  
},  
"filter": {  
"term": {  
"access": "yes"  
}  
}  
}  
}  
}

Please advise.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Perryn\_Fowler](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@Perryn\_Fowler](https://discuss.elastic.co/u/Perryn_Fowler)\
**Post date:** [March 11, 2015, 2:37am UTC](https://discuss.elastic.co/t/filtered-has-child-query/22609/2 "2015-03-11T02:37:47Z")

</div>

The query as written will return a result because you are querying for  
_Parent_ documents that 'have children' matching your has\_child query. You  
can tell because the type in the url will be 'Foo'.

Hence, the filter you have specified is not run against the children, but  
against the _parents_. In your example the parent document does indeed have  
a access:yes field and does not get filtered out.

It is probably possible to do what you want, but it depends on whether you  
are trying to retrieve parent or child documents.

To get parent documents, just add your filter criteria to the has\_child  
query.

To get child documents, use 'Bar' in the url and take a look into the  
has\_parent filter/query

On Wed, Mar 11, 2015 at 10:23 AM, asanderson [a.steven.anderson@gmail.com](mailto:a.steven.anderson@gmail.com)  
wrote:

> Is a filtered has\_child query possible where the filter is applied to the  
> child document before the query of the has\_child?
> 
> e.g. Given the example below...
> 
> curl -X PUT "[http://localhost:9200/foobar](http://localhost:9200/foobar)" -d  
> "{"mappings":{"Foo":{},"Bar":{"\_parent":{"type":"Foo"}}}}"
> 
> curl -X PUT "[http://localhost:9200/foobar/Foo/1](http://localhost:9200/foobar/Foo/1)"  
> -d "{"foo":"abc","access":"yes"}"
> 
> curl -X PUT "[http://localhost:9200/foobar/Bar/2?parent=1](http://localhost:9200/foobar/Bar/2?parent=1)"  
> -d "{"bar":"xyz","access":"no"}"
> 
> The following filtered query should not return a result, if the filter was  
> being applied to the child document first.
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "has\_child": {  
> "type": "Bar",  
> "query": {  
> "term": {  
> "bar": "xyz"  
> }  
> }  
> }  
> },  
> "filter": {  
> "term": {  
> "access": "yes"  
> }  
> }  
> }  
> }  
> }
> 
> Please advise.
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/575c3aa3-169d-4f00-b353-9e54291da432%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAFps6aCsz%2B%2B%3DduMCZcR--T9s2eczn0Yq1jCftma9ae0KdaR%3Ddw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAFps6aCsz%2B%2B%3DduMCZcR--T9s2eczn0Yq1jCftma9ae0KdaR%3Ddw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![asanderson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@asanderson](https://discuss.elastic.co/u/asanderson)\
**Post date:** [March 11, 2015, 10:15am UTC](https://discuss.elastic.co/t/filtered-has-child-query/22609/3 "2015-03-11T10:15:07Z")

</div>

Actually, I do want only parent documents returned, but I want the filter to be applied to both parent and child documents. Is there a way to specify that the filter is to be applied before the query, so that this would be possible? If not, how would I rewrite the query to do this?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/05a86e8c-9ef2-4028-b937-e6370202e677%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/05a86e8c-9ef2-4028-b937-e6370202e677%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Perryn\_Fowler](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@Perryn\_Fowler](https://discuss.elastic.co/u/Perryn_Fowler)\
**Post date:** [March 11, 2015, 10:23pm UTC](https://discuss.elastic.co/t/filtered-has-child-query/22609/4 "2015-03-11T22:23:06Z")

</div>

'apply the filter before the query' doesn't make any sense to me - what  
would it filter? I suspect I'm not really understanding you, can you tell  
me more? Why do you want to be able to do this? How would it help?

anyway, from what I thing I do understand there are several ways to get the  
results you want. Which one you choose probably depends on how you want the  
results scored, and possibly performance considerations.

here is one way to try. (If you want to filter both then you need to ...  
apply the filter to both :))  
something like:

{  
"query": {  
"filtered": {  
"query": {  
"has\_child": {  
"type": "Bar",  
"query": {  
"filtered": {  
"query": {  
"term": {  
"bar": "xyz"  
}  
},  
"filter": {  
"term": {  
"access": "yes"  
}  
}  
}  
}  
}  
},  
"filter": {  
"term": {  
"access": "yes"  
}  
}  
}  
}  
}

On Wed, Mar 11, 2015 at 9:15 PM, asanderson [a.steven.anderson@gmail.com](mailto:a.steven.anderson@gmail.com)  
wrote:

> Actually, I do want only parent documents returned, but I want the filter  
> to be applied to both parent and child documents. Is there a way to specify  
> that the filter is to be applied before the query, so that this would be  
> possible? If not, how would I rewrite the query to do this?
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/05a86e8c-9ef2-4028-b937-e6370202e677%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/05a86e8c-9ef2-4028-b937-e6370202e677%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAFps6aBXHyr67vCxHb6BYV42y6RS9QCzGQK4xKb6Qmh8jph8pA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAFps6aBXHyr67vCxHb6BYV42y6RS9QCzGQK4xKb6Qmh8jph8pA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![asanderson](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@asanderson](https://discuss.elastic.co/u/asanderson)\
**Post date:** [March 11, 2015, 10:35pm UTC](https://discuss.elastic.co/t/filtered-has-child-query/22609/5 "2015-03-11T22:35:26Z")

</div>

On Wednesday, March 11, 2015 at 6:23:40 PM UTC-4, Perryn Fowler wrote:

> 'apply the filter before the query' doesn't make any sense to me - what  
> would it filter? I suspect I'm not really understanding you, can you tell  
> me more? Why do you want to be able to do this? How would it help?

The scenario is actually a very common one. We have a common field to all  
document types (regardless of parent or child) whose purpose is to control  
access. In this simple example, the access field represents this common  
field. Ideally, it would be much simpler to be able to specify a _filter  
then query_ strategy in the filtered query which would apply the term  
filter to all documents before the query is executed regardless of whether  
or not the query included a has\_parent query.

> anyway, from what I thing I do understand there are several ways to get  
> the results you want. Which one you choose probably depends on how you want  
> the results scored, and possibly performance considerations.
> 
> here is one way to try. (If you want to filter both then you need to ...  
> apply the filter to both :))  
> something like:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "has\_child": {  
> "type": "Bar",  
> "query": {  
> "filtered": {  
> "query": {  
> "term": {  
> "bar": "xyz"  
> }  
> },  
> "filter": {  
> "term": {  
> "access": "yes"  
> }  
> }  
> }  
> }  
> }  
> },  
> "filter": {  
> "term": {  
> "access": "yes"  
> }  
> }  
> }  
> }  
> }

Thank you for your work-around solution. Unfortunately, it's not ideal  
though, since in practice our filters can be very large, so it really  
bloats the request to have to repeat the filter clause.

Thank you for your time though.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/7af03ba4-9654-49f3-98f0-b40ca297a56f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7af03ba4-9654-49f3-98f0-b40ca297a56f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:27am UTC](https://discuss.elastic.co/t/filtered-has-child-query/22609/6 "2017-07-06T00:27:21Z")

</div>


