3

I'm using Elasticsearch 5.5 API FunctionScoreQueries with filter. The goal here is to mimic LinearDecayFunction, but because the values that I want to use are contained in the document and not available in my java application, I'm creating my own script functions.

In my use case, I'm manipulating dates, and want to score my documents from, let's say, 0 to 1 if they're contained between a certain date, and this date minus 36 months.

Here is the java code:

FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
    new FunctionScoreQueryBuilder.FilterFunctionBuilder(mydateFilter, 
    ScoreFunctionBuilders.scriptFunction(
        "...
        [Some painless logic;] 
        ..." +
        "return monthBetween/36.0;"))
};

QueryBuilder fqb = QueryBuilders.functionScoreQuery(functions);
qb.must(fqb);

At the end I obtained the number of months between two dates (this number is from 0 to 36) and want to get this number divided by 36.

First question:

I can't make it work if the return value of my script is lower than 1, is that normal?

output :

"value": 1,                               
"description": "No function matched",
"details": []

ES juste fixes the value to 1, ignoring my script function.

Second question:

(As a workaround for the 1st question, I returned 1 + monthBetween/36.0 In my example, then I'll have an offset of 1 in my global score)

If I return the variable monthBetween, I get 35, which is right because the offset between my dates is really 35 months.

When I return 1+monthBetween/36.0 the result is correct (1,972222222), but the logic is bad, because the date is really far from the expected one, therefore I want the score to get closer from 1 .Then I returned 2-monthBetween/36.0 for the sake of testing. Elasticsearch returns 1.9444444 as a result. Which is mathematically not correct and I can't understand why.

EDIT: I have to add that if I set my return statement to 2-35.0/36.0 (removing my variable monthBetween), I got the correct value.

Rlarroque
  • 1,900
  • 1
  • 17
  • 27

0 Answers0