Nested query inside the function_score

I have the following document schema


"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

"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:

"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 ?

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