# Painless - cosineSimilarity - How use the static reference

**URL:** <https://discuss.elastic.co/t/painless-cosinesimilarity-how-use-the-static-reference/217928>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 5, 2020, 8:44am UTC](https://discuss.elastic.co/t/painless-cosinesimilarity-how-use-the-static-reference/217928 "2020-02-05T08:44:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Remy\_Letient](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remy_letient/32/48128_2.png) [@Remy\_Letient](https://discuss.elastic.co/u/Remy_Letient)\
**Post date:** [February 5, 2020, 8:44am UTC](https://discuss.elastic.co/t/painless-cosinesimilarity-how-use-the-static-reference/217928/1 "2020-02-05T08:44:51Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Remy\_Letient](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remy_letient/32/48128_2.png) [@Remy\_Letient](https://discuss.elastic.co/u/Remy_Letient)\
**Post date:** [February 5, 2020, 10:09am UTC](https://discuss.elastic.co/t/painless-cosinesimilarity-how-use-the-static-reference/217928/2 "2020-02-05T10:09:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Remy\_Letient](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remy_letient/32/48128_2.png) [@Remy\_Letient](https://discuss.elastic.co/u/Remy_Letient)\
**Post date:** [February 5, 2020, 10:13am UTC](https://discuss.elastic.co/t/painless-cosinesimilarity-how-use-the-static-reference/217928/3 "2020-02-05T10:13:45Z")

</div>

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)

---

<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:** [March 4, 2020, 10:13am UTC](https://discuss.elastic.co/t/painless-cosinesimilarity-how-use-the-static-reference/217928/4 "2020-03-04T10:13:52Z")

</div>

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