Cosine similarity query in elasticsearch

I have documents saved inside an index x which is inside another index y

ie Maker/Ford and Maker/BMW, here documents are saved inside Ford which is inside Maker. Similarly for BMW. I need to find cosine similarity. I tried scripts,

{
  "query": {
    "script_score": {
      "query": {
        "match_all": {}
      },
      "script": {
        "source": "Ford['_vector'].size() == 0 ? 0 :cosineSimilarity(params.queryVector,'_vector')+1",
        "params": {
               "query_vector": [
                        0,1,12,4
                   
                    ]
            }
         }
      }
   }
}

{
  "script_score": {
    "query": {"match_all": {}},
    "script": {
      "source": "cosineSimilarity(params.query_vector, '_vector') + 1.0",
      "params": {"query_vector": query_vector}
    }
  }
}

None of them really worked. What would be the solution?

Back to Top