Painless - cosineSimilarity - How use the static reference

Hello,

I'm using painless for a scoring function and I'm encountering a problem like that:

"caused_by": {
"type": "illegal_state_exception",
"reason": "no 'this' pointer within static method"
}

because my script use the java stream API like this:

    return Optional.of(doc['sections']
    .stream()
    .map(section -> section['vector'])
    .filter(vector -> vector != null)
    .map(vector -> cosineSimilarity(params['queryVector'], vector) +1.0)
    .sorted()
    .collect(Collectors.toList()))
    .filter(scores -> !scores.isEmpty())
    .map(scores -> scores.get(scores.size() -1))
    .orElse(0);

The problem is with the reference of the cosineSimilarity function. Like this painless try to use the
this keyword, I want to reference explicitly this function with a static class but in the documentation we are : "The following methods are directly callable without a class/instance qualifier."

Have you an idea ? I like my stream syntax :slight_smile:

Ok it was just the beginning of my trip inside script_score. Finally the code
cosineSimilarity(params['queryVector'], doc['sections.vector']) + 1.0

Why.
Because the function execution is at the nested field level, see my query:

    "query": {
        "bool": {
            "must": [
                {
                    "nested": {
                        "query": {
                            "function_score": {
                                "query": {
                                    "exists": {
                                        "field": "sections.vector",
                                        "boost": 1
                                    }
                                },
                                "functions": [
                                    {
                                        "filter": {
                                            "exists": {
                                                "field": "sections.vector",
                                                "boost": 1
                                            }
                                        },
                                        "script_score": {
                                            "script": {
                                                "source": "cosineSimilarity(params['queryVector'], doc['sections.vector']) + 1.0",
                                                "lang": "painless",
                                                "params": {
                                                    "queryVector": [
                                                       ...
                                                    ]
                                                }
                                            }
                                        }
                                    }
                                ],
                                "score_mode": "multiply",
                                "max_boost": 3.4028235e+38,
                                "boost": 1
                            }
                        },
                        "path": "sections",
                        "ignore_unmapped": false,
                        "score_mode": "max",
                        "boost": 1
                    }
                }
            ]
    }

The check for null value for my vector field is handled by the filter clause.
Thanks Elastic Team for this product.

I know it's complicate to find the Java code linked to the above request. My contribution

Map<String, Object> params = Maps.newHashMap();
params.put("queryVector", request.getVector());
Script script = new Script(ScriptType.INLINE, "painless",
    "cosineSimilarity(params['queryVector'], doc['sections.vector'])", params);
FilterFunctionBuilder[] functions = {
    new FunctionScoreQueryBuilder.FilterFunctionBuilder(
        existsQuery("sections.vector"),
        ScoreFunctionBuilders.scriptFunction(script))
};
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders
    .functionScoreQuery(existsQuery("sections.vector"), functions);
NestedQueryBuilder nqb = nestedQuery("sections", functionScoreQueryBuilder,
    ScoreMode.Max);

Of course prefer Stored script instead Inline (to avoid compilation time)

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.