# Painless: bug using for loops and vector functions

**URL:** <https://discuss.elastic.co/t/painless-bug-using-for-loops-and-vector-functions/267263>\
**Category:** Elasticsearch\
**Created:** [March 15, 2021, 12:34pm UTC](https://discuss.elastic.co/t/painless-bug-using-for-loops-and-vector-functions/267263 "2021-03-15T12:34:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![coreation](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/coreation/32/10165_2.png) [@coreation](https://discuss.elastic.co/u/coreation)\
**Post date:** [March 15, 2021, 12:34pm UTC](https://discuss.elastic.co/t/painless-bug-using-for-loops-and-vector-functions/267263/1 "2021-03-15T12:34:17Z")

</div>

Hello everyone,

I ran into what seems to be a bug in Painless where if a vector function is used, say l2norm(), the outcome remains the same outcome as the first iteration. I'm using the painless script in a function score, I hope the query below sheds some light. I'm using the "exception" to see what the value is in each of the iteration, and it's every time the score of the first vector. I know this because I cycled the parameters a couple of times, and the score is everytime "stuck" on the first thing. So what I think is happening is that the function l2norm() (and all vector functions?!) are object instances that can only be instantiated one time? If that would be the case, what would be a work around?

```auto
    {
        "query": {
                "nested": {
                        "path": "media",
                        "query": {
                                "function_score": {
                                        "boost_mode": "replace",
                                        "query": {
                                                "bool": {                                                       
                                                        "filter": [{
                                                                "exists": {
                                                                        "field": "media.full_body_dense_vector"
                                                                }
                                                        }]
                                                }
                                        },
                                        "functions": [{
                                                "script_score": {
                                                        "script": {
                                                                "source": "if (params.filterVectors.size() > 0 && params.filterCutOffScore >= 0) {\n for (int i=0; i < params.filterVectors.size();i++) {\n def c = params.filterVectors[i]; double euDistance = l2norm(c, doc['media.full_body_dense_vector']);\n if (i==1) { throw new Exception(euDistance + ''); } \n }\n return 1.0f;",
                                                                "params": {
                                                                      "filterVectors":[
[1.0,2.0,3.0],[0.1,0.4,0.5]
                                                                        ],
                                                                        "filterCutOffScore": 1.04
                                                                },
                                                                "lang": "painless"
                                                        }
                                                }
                                        }]
                                }
                        }
                }
        },
        "size": 500,
        "from": 0,
        "track_scores": true
}

```

---

<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:** [April 12, 2021, 12:34pm UTC](https://discuss.elastic.co/t/painless-bug-using-for-loops-and-vector-functions/267263/2 "2021-04-12T12:34:56Z")

</div>

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