Vector-Based search using cosineSimilarity

Hi, I am indexing text and vector in Elasticsearch, working on an use case where I am using cosineSimilarity to get the result. The query search is returning the list of documents with decreasing Confidence Scores with the most similar ones being at the top. For laptop query I am getting laptop/computer related document at top and other non laptop related document after that. Requirement for the use case is If user search by Laptop then the response would be laptop/computer/PC related document only, Not not laptop related document.

Query that I am using :

  "query": {
    "script_score": {
      "query" : {
        "match_all": {}
      "script": {
        "source": "cosineSimilarity(params.query_vector, 'vector') + 1.0", 
        "params": {
          "query_vector": [vsctors]  

Can someone please help on that.


I am also looking for this.

There are multiple ways you can do that:

  1. script_score query has a min_score parameter. If provided results will be restricted only to those with scores >= min_score

  2. Inside query part you can provide a bool filter to restrict results to specific category (e.g. Laptops category)

  3. For approximate KNN search, we are working (still work in progress) on a new type of query: radius that allows to restrict returned results to only those that lie within the provided radius.

Thanks @mayya for your help.

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