How to use nested object in Elasticsearch filter script

I need some help. I have an index in ElasticSearch with the following mapping where I define two field for each document indexed:

      properties: {
        #field representing a document text
        "document_text": {
        "type": "text"

        # field containing a list of vector
        "nested_object": {
            "type": "nested",
           "properties" : {
             "vector": {
               "type": "dense_vector",
              "dims": 512  

And now I have the following two type of query:

query1 simple query where I can find all document and the score according to my input

   query1 = {
        "match": { "document_text": "something to find" } 

query2 where I define the score as the average of cosine similarity between the given query vector and all the vector present on nested_object of a given document

    query2 = {
        "nested": {
          "path": "nested_object",
          "score_mode": "avg", 
          "query": {
            "function_score": {
               "script_score": {
                 "script": {
                  "source":    "(1.0+cosineSimilarity(params.query_vector,'nested_object.vector'))",
          "params": {"query_vector": simple_vector} #simple_vector is a vector of length 512

Everything until now works. Now I want to "combine" the two query above in order to define a new query that do the following:

based on the result of first query modify the score as the following: pseudocode: (score_query1)*(score_query2) . To do that I have tried to use function_score . The query is something like this.

query3 = {
        "function_score": {
      "query": {"match": { "document_text": "something to find " }},
      "functions": [
          "filter":{query2} ,
          "script_score":{ "script": "_score_of_query_2"}
      "boost_mode": "multiply"

The problem is that I have no idea how define _score_of_query_2 according to query2 . I tried different solution but nothing. Can someone help me? Thanks

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