# Has\_child query does not support post\_filter?

**URL:** <https://discuss.elastic.co/t/has-child-query-does-not-support-post-filter/16154>\
**Category:** Elasticsearch\
**Created:** [March 5, 2014, 12:48am UTC](https://discuss.elastic.co/t/has-child-query-does-not-support-post-filter/16154 "2014-03-05T00:48:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![george\_monroe](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@george\_monroe](https://discuss.elastic.co/u/george_monroe)\
**Post date:** [March 5, 2014, 12:48am UTC](https://discuss.elastic.co/t/has-child-query-does-not-support-post-filter/16154/1 "2014-03-05T00:48:20Z")

</div>

Guys, I'm running the following query to test out one of the use cases:

curl -X GET '0:9200/segmentation/animal/\_search?pretty' -d

'{ "query" : {  
"has\_child" : { "type" : "visit",  
"query" : {  
"bool" : {  
"must" : [  
{"term" : { "\_parent" : "119000148-5661691" }},  
{ "range" : {  
"visit\_date" : {  
"from" : "2003-01-01T00:00:00.000Z",  
"to":"2011-11-18T00:00:00.000Z",  
"include\_lower" : false,  
"include\_upper" : true}}}]}},  
"post\_filter" : { "script": { "script": "true == true" }}}}}}}'  
\<==== Was hoping to use a custom native script to sum across the Visit docs  
to compare to sum threshold.

I have Animal parent with child Visit. I am trying to solve this very  
important use case for us: Give me all Animals that have had visits between  
a certain date range, and those visits must sum to a certain $ amount.

What I would like to do is filter out children records first by the date  
range, and then filter by the sum (which is the some across that date-range  
collection of docs).

In other words, I would like to return only those Animals in the result  
set, which have visits totaling a certain sum, and those visits being  
within a certain date range.

Is there a better way 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/82d03aa4-b003-4e8c-a7b6-8d7129fdfbe0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/82d03aa4-b003-4e8c-a7b6-8d7129fdfbe0%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 5, 2014, 1:48pm UTC](https://discuss.elastic.co/t/has-child-query-does-not-support-post-filter/16154/2 "2014-03-05T13:48:32Z")

</div>

If I understand you correctly, you what to return all parents where the the  
sum of a field (child.field\_to\_sum) of each parent's corresponding children  
exceeds some number (min\_sum\_value) and at the same time only for those  
children whose date falls in some range. You should be able to do something  
like this:

{  
"min\_score": \<min\_sum\_value\>,  
"query": {  
"has\_child": {  
"type": "child",  
"score\_type": "sum",  
"query": {  
"function\_score": {  
"filter": { + Whatever else }  
},  
"script\_score": {  
"script": "doc['child.field\_to\_sum'].value"  
}  
}  
}  
}  
}  
}

--  
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/9fe5307f-2b70-4be8-ac78-cf2c752c85e3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9fe5307f-2b70-4be8-ac78-cf2c752c85e3%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![george\_monroe](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@george\_monroe](https://discuss.elastic.co/u/george_monroe)\
**Post date:** [March 6, 2014, 6:40pm UTC](https://discuss.elastic.co/t/has-child-query-does-not-support-post-filter/16154/3 "2014-03-06T18:40:28Z")

</div>

Binh! Thanks so much!! It worked. But how do I limit it from the top  
based on max score??

I have an additional complication in that the parent has multiple children  
types. I figured out how to assign score of 0 to the children that should  
not contribute to the final score, but now I would like to say: Give me all  
the parents whose set of children fall within a certain date range, and  
whose corresponding sum of child.column\_to\_sum is at least MIN\_SCORE but  
not more than MAX\_SCORE.

How would I do that?

Here is the query that I've written based on your very helpful hint!

curl -X GET '0:9200/segmentation-cd/animal/\_search?pretty' -d  
'{ "min\_score" : 250,  
"query" : {  
"bool" : {  
"must" : [  
{ "function\_score" : {  
"query" : {  
"term" : { "animal.sex" : "neutered" }},  
"script\_score" : {"script" : "0"}}},  
{ "has\_child" : {  
"type" : "customer",  
"score\_type" : "sum",  
"query" : {  
"function\_score" : {  
"filter" : {  
"query" : {  
"term" : { "customer.first\_name" : "Michael"  
}}},  
"script\_score" : {"script" : "0"}}}}},  
{ "has\_child" : {  
"type" : "visit",  
"score\_type" : "sum",  
"query" : {  
"function\_score" : {  
"filter" : {  
"bool" : {  
"must" : [  
{ "range" : { "visit\_date" : {  
"from" : "2001-07-28T00:00:00.000Z", "to" : "2011-11-18T00:00:00.000Z",  
"include\_lower" : false, "include\_upper" : true}}}]}},  
"script\_score" : {"script" : "\_source.revenue"}}}}}]}}}'

On Wednesday, March 5, 2014 5:48:32 AM UTC-8, Binh Ly wrote:

> If I understand you correctly, you what to return all parents where the  
> the sum of a field (child.field\_to\_sum) of each parent's corresponding  
> children exceeds some number (min\_sum\_value) and at the same time only for  
> those children whose date falls in some range. You should be able to do  
> something like this:
> 
> {  
> "min\_score": \<min\_sum\_value\>,  
> "query": {  
> "has\_child": {  
> "type": "child",  
> "score\_type": "sum",  
> "query": {  
> "function\_score": {  
> "filter": { + Whatever else }  
> },  
> "script\_score": {  
> "script": "doc['child.field\_to\_sum'].value"  
> }  
> }  
> }  
> }  
> }  
> }

--  
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/151e2db0-9c6d-40bd-b41b-2f2f8df998bd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/151e2db0-9c6d-40bd-b41b-2f2f8df998bd%40googlegroups.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, 1:45am UTC](https://discuss.elastic.co/t/has-child-query-does-not-support-post-filter/16154/4 "2017-07-06T01:45:14Z")

</div>


