# Problem using the custom score query with a script

**URL:** <https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635>\
**Category:** Elasticsearch\
**Created:** [April 18, 2013, 8:16pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635 "2013-04-18T20:16:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![damien\_2](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@damien\_2](https://discuss.elastic.co/u/damien_2)\
**Post date:** [April 18, 2013, 8:16pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/1 "2013-04-18T20:16:47Z")

</div>

Hello everyone,

I tried to make a script and use it to calculate my own score but it does  
not act like it should be.  
Here is my script:

time = (param14 - doc['creationDate'].value) / 86400; // 1 day = 86400sec  
score = doc['timesViewed'].value \*  
param1 +  
doc['timesOlogized'].value

- param3 +  
doc['timesCommented'].value
- param2 +  
(doc['timesLoved'].value +  
doc['timesHated'].value +  
doc['timesHmm'].value) \*  
param4 +  
(doc['timesFacebook'].value

- doc['timesTwitter'].value +  
doc['timesTumblr'].value +  
doc['timesPinterest'].value +

doc['timesGoogleplus'].value + doc['timesReddit'].value) \* param5;  
if (time \> 365){  
score \* param13 \* \_score;  
}  
else if (time \> 279){  
score \* param12 \* \_score;  
}  
else if (time \> 186){  
score \* param11 \* \_score;  
}  
else if (time \> 93){  
score \* param10 \* \_score;  
}  
else if (time \> 31){  
score \* param9 \* \_score;  
}  
else if (time \> 7){  
score \* param8 \* \_score;  
}  
else if (time \> 1){  
score \* param7 \* \_score;  
}  
else {  
score \* param6 \* \_score;  
}  
With the following parameters:  
array(  
'param1' =\> self::SCORE\_NB\_POST\_VIEWS,  
'param2' =\> self::SCORE\_NB\_POST\_COMMENTED,  
'param3' =\> self::SCORE\_NB\_POST\_OLOGIZED,  
'param4' =\> self::SCORE\_NB\_POST\_LIKE,  
'param5' =\> self::SCORE\_NB\_EXTERNAL\_SHARES,  
'param6' =\> self::TM\_POST\_TODAY,  
'param7' =\> self::TM\_POST\_THIS\_WEEK,  
'param8' =\> self::TM\_POST\_THIS\_MONTH,  
'param9' =\> self::TM\_POST\_THREE\_MONTH,  
'param10' =\> self::TM\_POST\_SIX\_MONTH,  
'param11' =\> self::TM\_POST\_NINE\_MONTH,  
'param12' =\> self::TM\_POST\_THIS\_YEAR,  
'param13' =\> self::TM\_POST\_OLDER\_YEAR,  
'param14' =\> $this-\>today  
)  
Apparently $time is not set as it should be because it seems to go in the  
last else each time!  
$time should be the number of days since the document has been created with  
creationDate the creation timestamp and param14 is the actual timestamp  
calculate with time() (PHP). I also tried to return score at the end but  
it did not change anything...

Is there something wrong in my script? How can I test it? Is there a way to  
print variable?  
Thank you for your help!

--  
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:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 18, 2013, 8:58pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/2 "2013-04-18T20:58:03Z")

</div>

I typically test such scripts one step at a time using script\_field.  
script\_field makes it much easier to see what's getting returned for each  
record. You can print from the screen to stdout using  
System.out.println(....) but this is only useful if you run elasticsearch  
locally in foreground mode. Alternatively, you can write messages into the  
log file using this somewhat convoluted  
call: org.elasticsearch.common.logging.Loggers.getLogger("my\_logger").info("message  
from the script");

On Thursday, April 18, 2013 4:16:47 PM UTC-4, Damien wrote:

> Hello everyone,
> 
> I tried to make a script and use it to calculate my own score but it does  
> not act like it should be.  
> Here is my script:
> 
> time = (param14 - doc['creationDate'].value) / 86400; // 1 day = 86400sec  
> score = doc['timesViewed'].value \*  
> param1 +  
> doc['timesOlogized'].value
> 
> - param3 +
> 
> doc['timesCommented'].value \* param2 +  
> (doc['timesLoved'].value +  
> doc['timesHated'].value +  
> doc['timesHmm'].value) \*  
> param4 +
> 
> (doc['timesFacebook'].value + doc['timesTwitter'].value +  
> doc['timesTumblr'].value +  
> doc['timesPinterest'].value +
> 
> doc['timesGoogleplus'].value + doc['timesReddit'].value) \* param5;  
> if (time \> 365){  
> score \* param13 \* \_score;  
> }  
> else if (time \> 279){  
> score \* param12 \* \_score;  
> }  
> else if (time \> 186){  
> score \* param11 \* \_score;  
> }  
> else if (time \> 93){  
> score \* param10 \* \_score;  
> }  
> else if (time \> 31){  
> score \* param9 \* \_score;  
> }  
> else if (time \> 7){  
> score \* param8 \* \_score;  
> }  
> else if (time \> 1){  
> score \* param7 \* \_score;  
> }  
> else {  
> score \* param6 \* \_score;  
> }  
> With the following parameters:  
> array(  
> 'param1' =\> self::SCORE\_NB\_POST\_VIEWS,  
> 'param2' =\> self::SCORE\_NB\_POST\_COMMENTED,  
> 'param3' =\> self::SCORE\_NB\_POST\_OLOGIZED,  
> 'param4' =\> self::SCORE\_NB\_POST\_LIKE,  
> 'param5' =\> self::SCORE\_NB\_EXTERNAL\_SHARES,  
> 'param6' =\> self::TM\_POST\_TODAY,  
> 'param7' =\> self::TM\_POST\_THIS\_WEEK,  
> 'param8' =\> self::TM\_POST\_THIS\_MONTH,  
> 'param9' =\> self::TM\_POST\_THREE\_MONTH,  
> 'param10' =\> self::TM\_POST\_SIX\_MONTH,  
> 'param11' =\> self::TM\_POST\_NINE\_MONTH,  
> 'param12' =\> self::TM\_POST\_THIS\_YEAR,  
> 'param13' =\> self::TM\_POST\_OLDER\_YEAR,  
> 'param14' =\> $this-\>today  
> )  
> Apparently $time is not set as it should be because it seems to go in the  
> last else each time!  
> $time should be the number of days since the document has been created  
> with creationDate the creation timestamp and param14 is the actual  
> timestamp calculate with time() (PHP). I also tried to return score at the  
> end but it did not change anything...
> 
> Is there something wrong in my script? How can I test it? Is there a way  
> to print variable?  
> Thank you for your help!

--  
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:** ![damien\_2](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@damien\_2](https://discuss.elastic.co/u/damien_2)\
**Post date:** [April 18, 2013, 9:14pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/3 "2013-04-18T21:14:16Z")

</div>

Yeah log file is better than nothing. Thanks for the line but I'm using  
elasticsearch in PHP environment and I'm not sure there is log system like  
in Java.  
So I don't know how to call getLogger()... I'm going to search how to call  
it now.

--  
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:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 18, 2013, 9:19pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/4 "2013-04-18T21:19:16Z")

</div>

It will be logged in the elasticsearch log file. Here is a complete  
example: [https://github.com/imotov/elasticsearch-test-scripts/blob/master/logging\_from\_script.sh](https://github.com/imotov/elasticsearch-test-scripts/blob/master/logging_from_script.sh)

On Thursday, April 18, 2013 5:14:16 PM UTC-4, Damien wrote:

> Yeah log file is better than nothing. Thanks for the line but I'm using  
> elasticsearch in PHP environment and I'm not sure there is log system like  
> in Java.  
> So I don't know how to call getLogger()... I'm going to search how to call  
> it now.

--  
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:** ![damien\_2](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@damien\_2](https://discuss.elastic.co/u/damien_2)\
**Post date:** [April 18, 2013, 9:48pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/5 "2013-04-18T21:48:28Z")

</div>

It seems to work, I mean there is no error when executing the script.  
Now I just have to find where is this log file if it actually worked.  
Thanks again.

--  
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:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 18, 2013, 10:01pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/6 "2013-04-18T22:01:53Z")

</div>

Try this:

curl -s "_elasticsearch\_address_:9200/\_nodes/\_local?settings=true&pretty=true"  
| grep "path.logs"

On Thursday, April 18, 2013 5:48:28 PM UTC-4, Damien wrote:

> It seems to work, I mean there is no error when executing the script.  
> Now I just have to find where is this log file if it actually worked.  
> Thanks again.

--  
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:** ![damien\_2](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@damien\_2](https://discuss.elastic.co/u/damien_2)\
**Post date:** [April 18, 2013, 10:09pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/7 "2013-04-18T22:09:57Z")

</div>

Perfect! Found it, thanks.  
But apparently, that does not print something in the log file. I just found  
the exact same line with the logger call...

--  
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:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 18, 2013, 10:24pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/8 "2013-04-18T22:24:37Z")

</div>

Apparently, MVEL is getting confused if it gets no parameters in the info  
requests. I have updated the  
example: [https://github.com/imotov/elasticsearch-test-scripts/blob/master/logging\_from\_script.sh](https://github.com/imotov/elasticsearch-test-scripts/blob/master/logging_from_script.sh)  
It should work now

On Thursday, April 18, 2013 6:09:57 PM UTC-4, Damien wrote:

> Perfect! Found it, thanks.  
> But apparently, that does not print something in the log file. I just  
> found the exact same line with the logger call...

--  
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:** ![damien\_2](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@damien\_2](https://discuss.elastic.co/u/damien_2)\
**Post date:** [April 19, 2013, 2:24pm UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/9 "2013-04-19T14:24:04Z")

</div>

Yes! It works thank you. I can see now that my time variable is null each  
time. Apparently you cannot use the word 'time' as a variable, it might be  
a key word for MVEL. I just changed the name time in timeModifier and it  
works. This would have been impossible to figure it out without log. Thanks!

Le jeudi 18 avril 2013 18:24:37 UTC-4, Igor Motov a écrit :

> Apparently, MVEL is getting confused if it gets no parameters in the info  
> requests. I have updated the example:  
> [https://github.com/imotov/elasticsearch-test-scripts/blob/master/logging\_from\_script.shIt](https://github.com/imotov/elasticsearch-test-scripts/blob/master/logging_from_script.shIt) should work now
> 
> On Thursday, April 18, 2013 6:09:57 PM UTC-4, Damien wrote:
> 
> > Perfect! Found it, thanks.  
> > But apparently, that does not print something in the log file. I just  
> > found the exact same line with the logger call...

--  
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:40am UTC](https://discuss.elastic.co/t/problem-using-the-custom-score-query-with-a-script/11635/10 "2017-07-06T02:40:23Z")

</div>


