# How to use nested object in Elasticsearch filter script

**URL:** <https://discuss.elastic.co/t/how-to-use-nested-object-in-elasticsearch-filter-script/272437>\
**Category:** Elasticsearch\
**Created:** [May 7, 2021, 4:03pm UTC](https://discuss.elastic.co/t/how-to-use-nested-object-in-elasticsearch-filter-script/272437 "2021-05-07T16:03:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![giovdb94](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@giovdb94](https://discuss.elastic.co/u/giovdb94)\
**Post date:** [May 7, 2021, 4:03pm UTC](https://discuss.elastic.co/t/how-to-use-nested-object-in-elasticsearch-filter-script/272437/1 "2021-05-07T16:03:46Z")

</div>

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

```auto
      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

```auto
   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

```auto
    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.

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 4, 2021, 4:04pm UTC](https://discuss.elastic.co/t/how-to-use-nested-object-in-elasticsearch-filter-script/272437/2 "2021-06-04T16:04:39Z")

</div>

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