Compile error when using sparse vector and cosine similarity

You need to add quotes around the field name: doc['embedding']. You should also add 1 to your cosine similarity measure to prevent the score to be negative like advised in this documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/master/query-dsl-script-score-query.html#vector-functions
cosineSimilaritySparse(params.queryVector, doc[embedding]) + 1.0

1 Like