Skip to content

Undocumented behaviour / Bug: ElasticSearch Painless - (Vector) Functions in (for) loops #70437

@coreation

Description

@coreation

Elasticsearch version (bin/elasticsearch --version): 7.11

Plugins installed: none outside the default ones coming with 7.11

JVM version (java -version): Java8

OS version (uname -a if on a Unix-like system): OS X

Description of the problem including expected versus actual behavior:

I'm currently experiencing something weird in the following script:

def scores =[]; def xt = params.filterVectors; for (int i=0; i< xt.size();i++) { scores.add(cosineSimilarity(xt[i], doc['media.full_body_dense_vector'].asList()));} throw new Exception(scores.toString());

The result that is added to the array, is always the cosineSimilarity outcome of the first iteration, however if you log "xt[i]", it is looping through the passed parameters of the script. It seems however that if a (vector) function is passed in a for loop, it's always the same outcome as in the first iteration. In other words it doesn't seem like you can dynamically re-use the function inside a for loop. (?)

**I have no issue with providing more detail If someone could tell me that this is a bug, or just undocumented behaviour. By undocumented I mean that there's documentation on using for loops, and documentation on a variety of vector functions (much appreciated btw!!), but there's nothing that states you can't use them inside a loop.

Steps to reproduce:

  1. mapping
"mappings": {
  "properties": {
    "title": {
      "type": "text"
    },
    "title_vector": {
      "type": "dense_vector",
      "dims": 512
    }
    "tags": {
      "type": "keyword"
    }
  }
}
  1. script / query
    {
    	"query": {
    		"function_score": {
    			"boost_mode": "replace",
    			"query": {
    				"bool": {
    					"filter": [{
    						"exists": {
    							"field": "title_vector"
    						}
    					}]
    				}
    			},
    			"functions": [{
    				"script_score": {
    					"script": {
    						"source": "def scores =[]; def xt = params.filterVectors; for (int i=0; i< xt.size();i++) { scores.add(cosineSimilarity(xt[i], doc['title_vector']));} throw new Exception(scores.toString());",
    						"params": {
    							"filterVectors": [
    								[1.0, 2.0, 3.0],
    								[0.1, 0.4, 0.5]
    							]
    						},
    						"lang": "painless"
    					}
    				}
    			}]
    		}
    	},
    	"size": 500,
    	"from": 0,
    	"track_scores": true
    }

Provide logs (if relevant):

No logs are produced, just the exception which prints 2 times the same calculation outcome.

Link to relevant StackOverflow ticket

https://stackoverflow.com/questions/66639279/elasticsearch-painless-using-vector-functions-in-for-loops-bug/66662131#66662131

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions