Filtering using terms for function_score in elasticsearch?

I am trying to filter out results of a cosine similarity using terms . This is the query I am using?

{
    "size": 15,
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": {
                        "terms": {
                            "id_": [
                                "1","2", "999"
                            ]
                        }
                    }
                }
            }
        },
        "script_score": {
            "script": {
                "source": "cosineSimilarity(params.query_vector, 'emb_vector') + 1.0",
                "params": {
                    "query_vector": [

                    ]
                }
            }
        }
    },
    "_source": {
        "includes": [
            "title",
            "body",
            "id_",
            "text"
        ]
    }
}

What I want is only those docs with the id 1,2 or 999 be returned. But, it is throwing the following error:

"reason": "[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
                "line": 17,
                "col": 9

What am I doing wrong?

Here is the mapping:

{"mappings": {
  "properties": {
    "id_": {
      "type": "text"
    },
    "emb_vector": {
      "type": "dense_vector",
      "dims": 768
    },
    "title": {
      "type": "text"
    }, 
    "text": {
      "type": "text"
    }

  }
}
}

I would write your query like this:


  "query": {
    "script_score": {
      "query": {
        "terms": {
          "id_": [
            "1","2","999"
          ]
        },
        "script": {
          "source": "cosineSimilarity(params.query_vector, 'emb_vector') + 1.0",
          "params": {
            "query_vector": []
          }
        }
      }
    }
  }

The query you provided has incorrect syntax, as there are two query objects: function_score and script_score under a single query.

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