# Impossible to implement real custom boost query when the weight is in the child document?

**URL:** <https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950>\
**Category:** Elasticsearch\
**Created:** [June 6, 2014, 9:37am UTC](https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950 "2014-06-06T09:37:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Csaba\_Dezsenyi](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@Csaba\_Dezsenyi](https://discuss.elastic.co/u/Csaba_Dezsenyi)\
**Post date:** [June 6, 2014, 9:37am UTC](https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950/1 "2014-06-06T09:37:26Z")

</div>

Hello Everyone,

I would like to implement a popularity-based boost in my elasticsearch  
engine. I calculate custom popularity boost factors for documents  
periodically, but I store these float numbers in a child document, because  
I want to avoid the full reindex of the main article documents.

The mapping of the child document is the following:

{

"document\_boost": {  
"\_parent": {  
"type": "document"  
},  
"popular\_boost\_total": {  
"type": "float"  
},  
"popular\_boost\_recent": {  
"type": "float"  
},  
"last\_updated": {  
"type": "date"  
}  
}  
}

I would like to create query that:

- executes the main query provided by the end users
- attach the child document (1-1 relation to the parent)
- boost the score of the main query by multiplying with the custom boost  
factors that are read from the child document (popular\_boost\_total,  
popular\_boost\_recent)

I have been struggling with this for a while, and could not find the real  
nice solution. The best solution that I could find is the following  
(simplified):

GET index/document/\_search  
{  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"title": "basketball"  
}  
}  
],  
"should": [  
{  
"has\_child": {  
"type": "document\_boost",  
"query": {  
"function\_score": {  
"script\_score": {  
"script":  
"doc['document\_boost.popular\_boost\_total'].value"  
}  
}  
}  
}  
}  
]  
}  
}  
}

However, this is not a real boost, because the second bool part is an  
additional score, not a multiplication on the primary query score! In this  
case, the amount of boost cannot be expressed as a clean percentage, but a  
noisy additional score and the real boosting factor is depends on the  
absolute score value of the particular query. So, I think it is wrong.  
I would be able to solve it, if the custom boost factors would not be in  
chid documents, but in the parent document fields:

GET index/document/\_search  
{  
"query": {  
"function\_score": {  
"query": {  
"match": {  
"title": "basketball"  
}  
},  
"script\_score": {  
"script": "doc['popular\_boost\_recent'].value"  
}  
}  
}  
}

Well, it i obvious, it the above case we do not need the has\_child query.  
I also tried without the bool query:

GET index/document/\_search  
{  
"query": {  
"function\_score": {  
"query": {  
"match": {  
"title": "basketball"  
}  
},  
"functions": [  
{  
"filter" : {  
"has\_child": {  
"type": "document\_boost",  
"query": {"match\_all": {}}  
}  
},  
"script\_score": {  
"script": "doc['document\_boost.popular\_boost\_recent'].value"  
}  
}  
]  
}  
}  
}

In the above case, the script reads the value from the parent document, not  
from the child! Well, anyway, it seems a bug, since I explicitly define the  
full qualified name.

I think - considering the possibilities of the query API syntax - the last  
query above would be the solution for the real multiplication boosting, but  
it simpli does not work.  
Another solution can be if I would be able to define the score mode for the  
bool query, i.e. to tell elastic search not to add, but multiply the scores  
of the parts.

Are there others who are facing with the same issue? I think it is a common  
request nowadays to have some kind of popularity and other kind of custom  
boosts.  
Can somebody give me a hint? I hope I just misunderstood something...

Thanks!

Regards,  
Csaba

--  
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/af4a19e4-1b1c-4702-a016-c88a6c76d04b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/af4a19e4-1b1c-4702-a016-c88a6c76d04b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Csaba\_Dezsenyi](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@Csaba\_Dezsenyi](https://discuss.elastic.co/u/Csaba_Dezsenyi)\
**Post date:** [June 6, 2014, 10:55am UTC](https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950/2 "2014-06-06T10:55:19Z")

</div>

I could find only one related post:  
[https://groups.google.com/forum/#!msg/elasticsearch/EGCeJZbhVtA/i32ROGVmFswJ](https://groups.google.com/forum/#!msg/elasticsearch/EGCeJZbhVtA/i32ROGVmFswJ)  
But this has different question...

--  
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/6152742a-4d32-47a4-890d-49cd6a4dd291%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6152742a-4d32-47a4-890d-49cd6a4dd291%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [June 6, 2014, 4:52pm UTC](https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950/3 "2014-06-06T16:52:32Z")

</div>

Did you change the boost\_mode of your function score script? The default  
should be "multiply", which is the behavior you want, not "sum", which is  
what you are experiencing.

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

I have never used it with nested documents, so perhaps it is a bug (or a  
feature 🙂 )

--  
Ivan

On Fri, Jun 6, 2014 at 3:55 AM, Csaba Dezsényi [csaba.dezsenyi@gmail.com](mailto:csaba.dezsenyi@gmail.com)  
wrote:

> I could find only one related post:
> 
> [Redirecting to Google Groups](https://groups.google.com/forum/#!msg/elasticsearch/EGCeJZbhVtA/i32ROGVmFswJ)  
> But this has different question...
> 
> --  
> 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/6152742a-4d32-47a4-890d-49cd6a4dd291%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6152742a-4d32-47a4-890d-49cd6a4dd291%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/6152742a-4d32-47a4-890d-49cd6a4dd291%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/6152742a-4d32-47a4-890d-49cd6a4dd291%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/CALY%3DcQBdTboEEGNeDSZZw%2B%2BwDhpeS6xA-1tZ-QrVsuFt-XzUSA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBdTboEEGNeDSZZw%2B%2BwDhpeS6xA-1tZ-QrVsuFt-XzUSA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Csaba\_Dezsenyi](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@Csaba\_Dezsenyi](https://discuss.elastic.co/u/Csaba_Dezsenyi)\
**Post date:** [June 10, 2014, 11:01am UTC](https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950/4 "2014-06-10T11:01:58Z")

</div>

Thanks Ivan for the tip, but I think the boost\_mode is just fine in my  
queries. The problem is that I only can access the field of the child  
document, if I have an additional bool part query with the has\_child query  
inside. This causes the sum. The custom score is multiplied with the  
has\_child query score that is correct.

I also think that this is a bug..

Thanks,  
Csaba

1. június 6., péntek 18:52:39 UTC+2 időpontban Ivan Brusic a következőt  
írta:

> Did you change the boost\_mode of your function score script? The default  
> should be "multiply", which is the behavior you want, not "sum", which is  
> what you are experiencing.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html)
> 
> I have never used it with nested documents, so perhaps it is a bug (or a  
> feature 🙂 )
> 
> --  
> Ivan
> 
> On Fri, Jun 6, 2014 at 3:55 AM, Csaba Dezsényi \<[csaba.d...@gmail.com](mailto:csaba.d...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > I could find only one related post:
> > 
> > [Redirecting to Google Groups](https://groups.google.com/forum/#!msg/elasticsearch/EGCeJZbhVtA/i32ROGVmFswJ)  
> > But this has different question...
> > 
> > --  
> > 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:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/6152742a-4d32-47a4-890d-49cd6a4dd291%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6152742a-4d32-47a4-890d-49cd6a4dd291%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/6152742a-4d32-47a4-890d-49cd6a4dd291%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/6152742a-4d32-47a4-890d-49cd6a4dd291%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/f5b5354c-9849-4e7b-a171-33fd63b907cd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f5b5354c-9849-4e7b-a171-33fd63b907cd%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![cdebry](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cdebry](https://discuss.elastic.co/u/cdebry)\
**Post date:** [July 10, 2014, 11:37pm UTC](https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950/5 "2014-07-10T23:37:00Z")

</div>

I have the exact same issue except that I need to boost a child query based on a value in the parent. Sadly, I went through the same exercise and came to the same conclusions.

I agree that the last query is the correct approach. At first, I assumed that the "has\_child" filter was out of scope with the function; however, it recognized the field name without throwing an error. The issue is that it's not returning the field value, so it defaults to 1 and effectively doesn't have any impact on the score.

This definitely seems to be a bug. Have you logged it [here](https://github.com/elasticsearch/elasticsearch/issues)?

---

<div class="post-metadata">

**Author:** ![cdebry](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@cdebry](https://discuss.elastic.co/u/cdebry)\
**Post date:** [July 11, 2014, 4:06am UTC](https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950/6 "2014-07-11T04:06:09Z")

</div>

I found a workaround using rescore. It's not ideal, but with a large enough window, it should yield good results. Here's your query again, rewritten with a rescore.

GET index/document/\_search  
{  
"query": {  
"match": {  
"title": "basketball"  
}  
},  
"rescore": {  
"window\_size": 100,  
"query": {  
"score\_mode": "multiply",  
"rescore\_query": {  
"has\_child": {  
"type": "document\_boost",  
"query": {  
"function\_score": {  
"script\_score": {  
"script": "doc['document\_boost.popular\_boost\_recent'].value"  
}  
}  
}  
}  
}  
}  
}  
}

---

<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:16am UTC](https://discuss.elastic.co/t/impossible-to-implement-real-custom-boost-query-when-the-weight-is-in-the-child-document/17950/7 "2017-07-06T01:16:37Z")

</div>


