# Using dense\_vector with script params

**URL:** <https://discuss.elastic.co/t/using-dense-vector-with-script-params/380908>\
**Category:** Elasticsearch\
**Tags:** painless, runtime-fields, vector-search\
**Created:** [August 8, 2025, 2:39pm UTC](https://discuss.elastic.co/t/using-dense-vector-with-script-params/380908 "2025-08-08T14:39:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joellupfer](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@joellupfer](https://discuss.elastic.co/u/joellupfer)\
**Post date:** [August 8, 2025, 2:39pm UTC](https://discuss.elastic.co/t/using-dense-vector-with-script-params/380908/1 "2025-08-08T14:39:58Z")

</div>

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).

```json
    "searchVector": { "type": "dense_vector","dims": 1536}

```

```json
{
    "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:

```json
{
    "id": "product_search_vector",
    "params": {

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

```

And it return the following:

```json
{
    "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
}

```

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [August 8, 2025, 3:07pm UTC](https://discuss.elastic.co/t/using-dense-vector-with-script-params/380908/2 "2025-08-08T15:07:46Z")

</div>

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. 😥

Check out [the docs](https://www.elastic.co/docs/solutions/search/search-templates#search-template-convert-json) for more details.

Hope that helps!
