# Custom\_score query within has\_child query

**URL:** https://discuss.elastic.co/t/custom-score-query-within-has-child-query/12634
**Category:** Elasticsearch
**Created:** [July 3, 2013, 8:01am UTC](https://discuss.elastic.co/t/custom-score-query-within-has-child-query/12634 "2013-07-03T08:01:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![aditya\_pasumarthi](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aditya\_pasumarthi](https://discuss.elastic.co/u/aditya_pasumarthi)
#### Post date: [July 3, 2013, 8:01am UTC](https://discuss.elastic.co/t/custom-score-query-within-has-child-query/12634/1 "2013-07-03T08:01:53Z")

</div>

I want to use a custom\_score query within has\_child query, so that I can  
control the score of the child documents. However when I reference a child  
document field in has\_child custom\_score query, ES is trying to map the  
field to parent document and is throwing error.

Can anyone please throw some light on this issue and any workarounds?

Thanks & Regards,  
Aditya.

--  
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: ![aditya\_pasumarthi](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aditya\_pasumarthi](https://discuss.elastic.co/u/aditya_pasumarthi)
#### Post date: [July 3, 2013, 9:23am UTC](https://discuss.elastic.co/t/custom-score-query-within-has-child-query/12634/2 "2013-07-03T09:23:36Z")

</div>

Can someone please help me with this? It is quite important for one of our  
product use cases.

Thanks & Regards,  
Aditya.

On Wednesday, July 3, 2013 1:31:53 PM UTC+5:30, [aditya.p...@wagglebee.net](mailto:aditya.p...@wagglebee.net)  
wrote:

> I want to use a custom\_score query within has\_child query, so that I can  
> control the score of the child documents. However when I reference a child  
> document field in has\_child custom\_score query, ES is trying to map the  
> field to parent document and is throwing error.
> 
> Can anyone please throw some light on this issue and any workarounds?
> 
> Thanks & Regards,  
> Aditya.

--  
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: ![aditya\_pasumarthi](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aditya\_pasumarthi](https://discuss.elastic.co/u/aditya_pasumarthi)
#### Post date: [July 3, 2013, 9:48am UTC](https://discuss.elastic.co/t/custom-score-query-within-has-child-query/12634/3 "2013-07-03T09:48:07Z")

</div>

Here is a more detailed version of issue I'm facing...

custom\_score query within has\_child query does not recognize child document  
fields within the score script.

I have two document types, "Forums" and "ForumStats". Forums is a parent  
type and ForumStats is a child type. I had set the mapping correctly.

When I issue query like the one below on forums type, ES complains that the  
field "avgPostsPerMinute" is not found in "Forums" type, which I feel is  
wrong as the script is supposed to look at child document fields.

{  
"size": 20,  
"sort": {  
"\_score": "desc"  
},  
"query": {  
"has\_child": {  
"type": "forumstats",  
"score\_type": "sum",  
"query": {  
"custom\_score": {  
"script": "doc['avgPostsPerMin'].value",  
"query": {  
"match\_all": {}  
}  
}  
}  
}  
}  
}

If I set the script to a value that doe snot refer to child document type,  
then the query runs and I get a score for the parent document correctly.

Please review this and let me know if this is by design or a bug. If it is  
by design, it looks odd as the custom score query is running on child  
documents and I would expect ES to recognize the child document fields  
within the script property.

Thanks & Regards,  
Aditya.

On Wednesday, July 3, 2013 2:53:36 PM UTC+5:30, [aditya.p...@wagglebee.net](mailto:aditya.p...@wagglebee.net)  
wrote:

> Can someone please help me with this? It is quite important for one of our  
> product use cases.
> 
> Thanks & Regards,  
> Aditya.
> 
> On Wednesday, July 3, 2013 1:31:53 PM UTC+5:30, aditya.p...@wagglebee.netwrote:
> 
> > I want to use a custom\_score query within has\_child query, so that I can  
> > control the score of the child documents. However when I reference a child  
> > document field in has\_child custom\_score query, ES is trying to map the  
> > field to parent document and is throwing error.
> > 
> > Can anyone please throw some light on this issue and any workarounds?
> > 
> > Thanks & Regards,  
> > Aditya.

--  
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: ![aditya\_pasumarthi](https://avatars.discourse-cdn.com/v4/letter/a/f08c70/32.png) [@aditya\_pasumarthi](https://discuss.elastic.co/u/aditya_pasumarthi)
#### Post date: [July 3, 2013, 11:15am UTC](https://discuss.elastic.co/t/custom-score-query-within-has-child-query/12634/4 "2013-07-03T11:15:42Z")

</div>

Finally I could resolve the issue myself.

For anyone looking for a solution for similar issue, here is what I did to  
solve.

In the query mentioned in this posts, specify the child document field with  
child document type prefix. So the below query will work as expected.

Note: "avgPostsPerMin" is a field in child document type, "forumstats", and  
the below query is run on the parent document type, "forums".

{  
"size": 20,  
"sort": {  
"\_score": "desc"  
},  
"query": {  
"has\_child": {  
"type": "forumstats",  
"score\_type": "sum",  
"query": {  
"custom\_score": {  
"script": "doc['forumstats.avgPostsPerMin'].value",  
"query": {  
"match\_all": {}  
}  
}  
}  
}  
}  
}

On Wednesday, July 3, 2013 1:31:53 PM UTC+5:30, [aditya.p...@wagglebee.net](mailto:aditya.p...@wagglebee.net)  
wrote:

> I want to use a custom\_score query within has\_child query, so that I can  
> control the score of the child documents. However when I reference a child  
> document field in has\_child custom\_score query, ES is trying to map the  
> field to parent document and is throwing error.
> 
> Can anyone please throw some light on this issue and any workarounds?
> 
> Thanks & Regards,  
> Aditya.

--  
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:28am UTC](https://discuss.elastic.co/t/custom-score-query-within-has-child-query/12634/5 "2017-07-06T02:28:27Z")

</div>


