# Nested query inside the function\_score

**URL:** <https://discuss.elastic.co/t/nested-query-inside-the-function-score/321428>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [December 16, 2022, 6:28pm UTC](https://discuss.elastic.co/t/nested-query-inside-the-function-score/321428 "2022-12-16T18:28:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kankipati\_rajaram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kankipati_rajaram/32/114831_2.png) [@Kankipati\_rajaram](https://discuss.elastic.co/u/Kankipati_rajaram)\
**Post date:** [December 16, 2022, 6:28pm UTC](https://discuss.elastic.co/t/nested-query-inside-the-function-score/321428/1 "2022-12-16T18:28:07Z")

</div>

I have the following document schema

```auto

"properties": {
                    "doc_id": {"type": "long"},
                    "cortex_id": {"type": "long"},
                    "sentence_embedding": {
                        "type": "dense_vector",
                        "dims": 768,
                    }
                },

```

Explicitly we are updating the mappings with new nested field

```auto
"properties": {
                "my_vectors": {
                    "type": "nested",
                    "properties": {
                        "vector": {
                            "type": "dense_vector",
                            "dims": 768
                        }
                    }}}

```

After update mapping, some of the documents are created, which will have values in the my\_vectors

I am writing a query that can handle the following scenarios  
scenario1: document having only sentence\_embedding - score using sentence\_embedding , scenario2 : sentence\_embedding and my\_vectors are present - score using my\_vectors

I am getting the following error with the query:

```auto
"query": {
                "function_score": {
                    "score_mode":"max",
                    "query": {
                        "bool": {
                            "filter": {"term": {"cortex_id": es_query_dto.cortex_id}}
                        }
                    },
                    "functions": [
                        {
                            "filter": {
                                "exists": {
                                    "field": "my_vectors"
                                }
                            },
                            "query": {
                                "nested": {
                                    "path": "my_vectors",
                                    "score_mode": "max",
                                    "ignore_unmapped": "true",
                                    "query": {
                                        "function_score": {
                                            "script_score": {
                                                "script": {
                                                    "source": "(1.0+cosineSimilarity(params.queryVector, 'my_vectors.vector'))",
                                                    "params": {
                                                        "queryVector":
                                                            es_query_dto.sentence_vector.tolist()
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        {
                            "filter":{
                              "bool": {
                                  "must_not": [
                                    {"exists": {"field": "my_vectors"}}]}
                            },
                            "script_score": {
                                "script": {
                                    "source": "(1.0+cosineSimilarity(params.queryVector, 'sentence_embedding'))",
                                    "params": {
                                        "queryVector":
                                            es_query_dto.sentence_vector.tolist()
                                    }
                                }
                            }
                        }
                    ],
                }
            }

```

RequestError(400, 'x\_content\_parse\_exception', 'unknown query [function\_score] did you mean any of [random\_score, script\_score]?')

can anyone help ?

---

<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:** [January 13, 2023, 6:28pm UTC](https://discuss.elastic.co/t/nested-query-inside-the-function-score/321428/2 "2023-01-13T18:28:43Z")

</div>

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