Skip to content

Pre-filter nested fields in knn queries #106994

@ezorita

Description

@ezorita

Description

By reading some of the latest bugfixes in 8.13.0 and the documentation it seems it is still not possible to pre-filter a knn search on nested mappings, such as:

{
   "mappings":{
      "properties":{
         "title":{
            "type":"keyword"
         },
         "paragraphs":{
            "type":"nested",
            "properties":{
               "language":{
                  "type":"keyword"
               },
               "vector":{
                  "type":"dense_vector",
                  "dims":3,
                  "similarity":"cosine",
                  "index":true
               }
            }
         }
      }
   }
}

then searching the following would never match any document:

{
   "query":{
      "nested":{
         "path":"paragraphs",
         "query":{
            "knn":{
               "field":"paragraphs.vector",
               "query_vector":[
                  0.5,
                  0.5,
                  0.5
               ],
               "num_candidates":5,
               "filter":{
                  "bool":{
                     "must":[
                        {
                           "match":{
                              "paragraphs.language":"EN"
                           }
                        }
                     ]
                  }
               }
            }
         }
      }
   }
}

or

{
   "query":{
      "nested":{
         "path":"paragraphs",
         "query":{
            "knn":{
               "field":"paragraphs.vector",
               "query_vector":[
                  0.5,
                  0.5,
                  0.5
               ],
               "num_candidates":5,
               "filter":{
                  "nested":{
                     "path":"paragraphs",
                     "query":{
                        "bool":{
                           "must":[
                              {
                                 "match":{
                                    "paragraphs.language":"EN"
                                 }
                              }
                           ]
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

In the current implementation the filter scope is limited to the parent document, thus limiting other functionalities of nested documents.

I wonder how hard it would be to extend the filter context to nested documents?

Thank you!

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions