Problem encountered in the cosine similarity test sample

With the ES 7.2.0 environment,I wanted to use cosineSimilarity function to calculate cosine similarity.But I got this error.

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "cosineSimilarity(params.q ...",
          "^---- HERE"
        ],
        "script": "cosineSimilarity(params.queryVector, doc['my_vector'])",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "dindex",
        "node": "IL-rn7MaS_S1oe2j0A5DZA",
        "reason": {
          "type": "query_shard_exception",
          "reason": "script_score: the script could not be loaded",
          "index_uuid": "HxMQYoQWQwiQ0Lrlp3xVNg",
          "index": "dindex",
          "caused_by": {
            "type": "script_exception",
            "reason": "compile error",
            "script_stack": [
              "cosineSimilarity(params.q ...",
              "^---- HERE"
            ],
            "script": "cosineSimilarity(params.queryVector, doc['my_vector'])",
            "lang": "painless",
            "caused_by": {
              "type": "illegal_argument_exception",
              "reason": "Unknown call [cosineSimilarity] with [2] arguments."
            }
          }
        }
      }
    ],
    "caused_by": {
      "type": "script_exception",
      "reason": "compile error",
      "script_stack": [
        "cosineSimilarity(params.q ...",
        "^---- HERE"
      ],
      "script": "cosineSimilarity(params.queryVector, doc['my_vector'])",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Unknown call [cosineSimilarity] with [2] arguments."
      }
    }
  },
  "status": 400
}

My code as follows:

PUT dindex
{
  "mappings": {
    "properties": {
      "my_vector": {
        "type": "dense_vector"
      },
      "my_text": {
        "type": "keyword"
      }
    }
  }
}
PUT dindex/_doc/1
{
  "my_text": "text1",
  "my_vector": [5.5, 100.8, -12]
}
GET dindex/_search
{
  "query": {
    "script_score": {
      "query": {
        "match_all": {}
      },
      "script": {
        "source": "cosineSimilarity(params.queryVector, doc['my_vector'])",
        "params": {
          "queryVector": [ 4.5, 3.4, -1.2]
        }
      }
    }
  }
}

Thanks for answering.

@forheroes1994 Sorry, cosineSimilarity and dotProduct functions will be available from 7.3

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