Using dense_vector with script params

Hello!

I am currently developing a script that utilizes text embedding vectors. The relevant portion of the mapping is shown below (please disregard the remaining parts, as this is intended solely for testing purposes).

    "searchVector": { "type": "dense_vector","dims": 1536}
{
    "script": {
        "lang": "mustache",
        "source": {
            "_source": false,
            "min_score": 1,
            "sort": [
                "_score"
            ],
            "size": "{{size}}",
            "from": "{{from}}",
            "query": {
                "script_score": {
                    "query": {
                        "match_all": {}
                    },
                    "script": {
                        "source": "cosineSimilarity(params.queryVector, 'searchVector') + 1.0",
                        "params": {
                            "queryVector": "{{query_vector}}"
                        }
                    }
                }
            }
        }
    }
}

But when I want to use it in the _search/template I have the following errors:

{
    "id": "product_search_vector",
    "params": {

        "searchVector": [
            0.022199785,
            0.026075596,
            ... 1536 more vectors...
        ],
        "size": 24,
        "from": 0
    },
    "explain": false
}

And it return the following:

{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "org.elasticsearch.server@8.18.4/org.elasticsearch.script.VectorScoreScriptUtils$CosineSimilarity.<init>(VectorScoreScriptUtils.java:513)",
                    "cosineSimilarity(params.queryVector, 'searchVector') + 1.0",
                    "                       ^---- HERE"
                ],
                "script": "cosineSimilarity(params.queryVector, 'searchVector') + 1.0",
                "lang": "painless",
                "position": {
                    "offset": 23,
                    "start": 0,
                    "end": 58
                }
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "joellocal_products",
                "node": "RLS_DsE_Q_mqxbkdX675WA",
                "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "org.elasticsearch.server@8.18.4/org.elasticsearch.script.VectorScoreScriptUtils$CosineSimilarity.<init>(VectorScoreScriptUtils.java:513)",
                        "cosineSimilarity(params.queryVector, 'searchVector') + 1.0",
                        "                       ^---- HERE"
                    ],
                    "script": "cosineSimilarity(params.queryVector, 'searchVector') + 1.0",
                    "lang": "painless",
                    "position": {
                        "offset": 23,
                        "start": 0,
                        "end": 58
                    },
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "Unsupported input object for float vectors: java.lang.String"
                    }
                }
            }
        ]
    },
    "status": 400
}

hey @joellupfer !

For passing an array, you will need to use {{tojson}}. That means that you will have to escape your template into the string format as well. :sad_but_relieved_face:

Check out the docs for more details.

Hope that helps!