# Function\_score with decay functions and script score

**URL:** https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307
**Category:** Elasticsearch
**Created:** [March 12, 2014, 2:03am UTC](https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307 "2014-03-12T02:03:56Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Philippe\_Modard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philippe_modard/32/1733_2.png) [@Philippe\_Modard](https://discuss.elastic.co/u/Philippe_Modard)
#### Post date: [March 12, 2014, 2:03am UTC](https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307/1 "2014-03-12T02:03:56Z")

</div>

Hey guys,

I'm looking to build this complex function\_score:  
function\_score: {  
functions: [  
{  
_script\_score_: {  
params: {  
origin : 1000000  
, scale : 1000000  
, offset : 1000000  
, decay : 0.5  
},  
script: "( (doc['stats.views'].value \>= offset) ? 1 : (  
Math.max(0, ((scale/(1-decay)) - Math.abs(doc['stats.views'].value -  
origin)) / (scale/(1-decay))) ) )"  
}  
},  
{  
_script\_score_: {  
params: {  
origin : 100  
, scale : 100  
, offset : 100  
, decay : 0.5  
},  
script: "( (doc['stats.likes'].value \>= offset) ? 1 : (  
Math.max(0, ((scale/(1-decay)) - Math.abs(doc['stats. likes'].value -  
origin)) / (scale/(1-decay))) ) )"  
}  
},  
{  
_exp_: {  
'date.published': {  
origin : new Date()  
, scale : '50d'  
, offset : 0  
, decay : 0.5  
}  
}  
},  
],  
boost\_mode: 'sum',  
_query_: {  
query\_string: {  
fields : ['title^6', 'description^3', '\_text']  
, query : q  
}  
},  
score\_mode: 'multiply'  
}

I replaced 2 decay functions by scripts scores, because I want these to be  
equal to 1 before a threshold.

_Few questions_:

1. I'm not seeing the result changing with my scripts scores parameters
2. I'm not sure when / if I have to use "\_score" in the scripts
3. Am I right using the boost\_mode = 'sum'?

Thank you for your help,  
Philmod

--  
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/ba8c997d-2838-493e-9f7c-6fad3d0a4b24%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ba8c997d-2838-493e-9f7c-6fad3d0a4b24%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 12, 2014, 1:41pm UTC](https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307/2 "2014-03-12T13:41:46Z")

</div>

1. Hard to tell, I'd probably just start simple with one function first and  
debug from there.
2. You can use \_score if you want to access the current computed score for  
the document based on your query\_string query. If you want to override that  
score, then you don't need \_score
3. boost\_mode='sum' means whatever score comes out of your functions will  
be added to the computed score for the document based on your query\_string  
query.

--  
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/557afbc6-4cd3-4ab2-ab0b-c14d713b878e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/557afbc6-4cd3-4ab2-ab0b-c14d713b878e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Philippe\_Modard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philippe_modard/32/1733_2.png) [@Philippe\_Modard](https://discuss.elastic.co/u/Philippe_Modard)
#### Post date: [March 12, 2014, 5:24pm UTC](https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307/3 "2014-03-12T17:24:18Z")

</div>

Thank you!

What I want is a multiplication of these 4 results/functions:

- query\_string \* exp(date.published) \* script\_score(stats.views) \*  
scripts\_score(stats.embeds)\*

And I want the last 3 to be independent of the query\_string.

Any help are very welcome!

Thanks a ton,  
Philippe

On Wednesday, 12 March 2014 06:41:46 UTC-7, Binh Ly wrote:

> 1. Hard to tell, I'd probably just start simple with one function first  
> and debug from there.
> 2. You can use \_score if you want to access the current computed score for  
> the document based on your query\_string query. If you want to override that  
> score, then you don't need \_score
> 3. boost\_mode='sum' means whatever score comes out of your functions will  
> be added to the computed score for the document based on your query\_string  
> query.

--  
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/be3ebdcb-1d3b-43bd-aa38-0a203d4f5fc2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/be3ebdcb-1d3b-43bd-aa38-0a203d4f5fc2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Philippe\_Modard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philippe_modard/32/1733_2.png) [@Philippe\_Modard](https://discuss.elastic.co/u/Philippe_Modard)
#### Post date: [March 12, 2014, 5:25pm UTC](https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307/4 "2014-03-12T17:25:23Z")

</div>

Thank you!

What I want is a multiplication of these 4 results/functions:

- query\_string \* exp(date.published) \* script\_score(stats.views) \*  
scripts\_score(stats.likes)\*

And I want the last 3 to be independent of the query\_string.

Any help are very welcome!

Thanks a ton,  
Philippe

On Wednesday, 12 March 2014 06:41:46 UTC-7, Binh Ly wrote:

> 1. Hard to tell, I'd probably just start simple with one function first  
> and debug from there.
> 2. You can use \_score if you want to access the current computed score for  
> the document based on your query\_string query. If you want to override that  
> score, then you don't need \_score
> 3. boost\_mode='sum' means whatever score comes out of your functions will  
> be added to the computed score for the document based on your query\_string  
> query.

--  
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/b5a2e479-cb39-46f3-bac4-7c5c4c716d70%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b5a2e479-cb39-46f3-bac4-7c5c4c716d70%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [March 12, 2014, 5:31pm UTC](https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307/5 "2014-03-12T17:31:02Z")

</div>

I've had a lot of luck stuffing script scores into the rescore. It helps  
if one the scripts is slower then you'd like. You do have to rely on the  
query string being "pretty good" at finding the results that you want  
though. Since all you want is to multiply you can do it by using a rescore  
in score\_mode: multiply. Like this:

```
rescore: {

  "window_size" : 1000,

  "query" : {
     "score_mode": "multiply",

     "rescore_query" : {
        "function_score" : {

           "script_score": {
              "script": "log10(doc['numeric'].value + 2)"

           },

                   more scripts here

        }

     }
  }

```

}

On Wed, Mar 12, 2014 at 1:25 PM, Philippe Modard  
[philippe.modard@gmail.com](mailto:philippe.modard@gmail.com)wrote:

> Thank you!
> 
> What I want is a multiplication of these 4 results/functions:
> 
> - query\_string \* exp(date.published) \* script\_score(stats.views) \*  
> scripts\_score(stats.likes)\*
> 
> And I want the last 3 to be independent of the query\_string.
> 
> Any help are very welcome!
> 
> Thanks a ton,  
> Philippe
> 
> On Wednesday, 12 March 2014 06:41:46 UTC-7, Binh Ly wrote:
> 
> > 1. Hard to tell, I'd probably just start simple with one function first  
> > and debug from there.
> > 2. You can use \_score if you want to access the current computed score  
> > for the document based on your query\_string query. If you want to override  
> > that score, then you don't need \_score
> > 3. boost\_mode='sum' means whatever score comes out of your functions will  
> > be added to the computed score for the document based on your query\_string  
> > query.
> 
> --  
> 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/b5a2e479-cb39-46f3-bac4-7c5c4c716d70%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b5a2e479-cb39-46f3-bac4-7c5c4c716d70%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/b5a2e479-cb39-46f3-bac4-7c5c4c716d70%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/b5a2e479-cb39-46f3-bac4-7c5c4c716d70%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/CAPmjWd1xaUupcvCkfX4Jh2kX%3DqTdS%2B4d1i---iA%3D4\_BvTB7gNg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd1xaUupcvCkfX4Jh2kX%3DqTdS%2B4d1i---iA%3D4_BvTB7gNg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Philippe\_Modard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/philippe_modard/32/1733_2.png) [@Philippe\_Modard](https://discuss.elastic.co/u/Philippe_Modard)
#### Post date: [March 12, 2014, 11:08pm UTC](https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307/6 "2014-03-12T23:08:21Z")

</div>

I found the issue:

I was using some javascript "Math.max" "Math.abs".

It totally works using "max()", "abs()".  
And I changed the boost\_mode = 'multiply' to get the result I wanted.

Thanks all,  
Philmod

On Tuesday, 11 March 2014 19:03:56 UTC-7, Philippe Modard wrote:

> Hey guys,
> 
> I'm looking to build this complex function\_score:  
> function\_score: {  
> functions: [  
> {  
> _script\_score_: {  
> params: {  
> origin : 1000000  
> , scale : 1000000  
> , offset : 1000000  
> , decay : 0.5  
> },  
> script: "( (doc['stats.views'].value \>= offset) ? 1 : (  
> Math.max(0, ((scale/(1-decay)) - Math.abs(doc['stats.views'].value -  
> origin)) / (scale/(1-decay))) ) )"  
> }  
> },  
> {  
> _script\_score_: {  
> params: {  
> origin : 100  
> , scale : 100  
> , offset : 100  
> , decay : 0.5  
> },  
> script: "( (doc['stats.likes'].value \>= offset) ? 1 : (  
> Math.max(0, ((scale/(1-decay)) - Math.abs(doc['stats. likes'].value -  
> origin)) / (scale/(1-decay))) ) )"  
> }  
> },  
> {  
> _exp_: {  
> 'date.published': {  
> origin : new Date()  
> , scale : '50d'  
> , offset : 0  
> , decay : 0.5  
> }  
> }  
> },  
> ],  
> boost\_mode: 'sum',  
> _query_: {  
> query\_string: {  
> fields : ['title^6', 'description^3', '\_text']  
> , query : q  
> }  
> },  
> score\_mode: 'multiply'  
> }
> 
> I replaced 2 decay functions by scripts scores, because I want these to be  
> equal to 1 before a threshold.
> 
> _Few questions_:
> 
> 1. I'm not seeing the result changing with my scripts scores parameters
> 2. I'm not sure when / if I have to use "\_score" in the scripts
> 3. Am I right using the boost\_mode = 'sum'?
> 
> Thank you for your help,  
> Philmod

--  
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/dfd05f83-f531-4fd6-a2cd-0fa3dac94f1c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dfd05f83-f531-4fd6-a2cd-0fa3dac94f1c%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, 1:43am UTC](https://discuss.elastic.co/t/function-score-with-decay-functions-and-script-score/16307/7 "2017-07-06T01:43:36Z")

</div>


