Multiple filters in knnBeta $search

Hi, I have a collection where each document has a vector embedding, a “type”, and some other metadata.
I use the knnBeta operator in a $search to get the documents with the closest embedding I input, but I also need these documents to be filtered by some fields in their metadata.
The issue is that using the filter field in the knnBeta operator, I can only use each filter operator once. For example, if I want to filter all the documents where “meta.duration” is between 2 and 10 I can do this.

{
      knnBeta: {
        vector: [-0.08771466463804245,0.040466126054525375,...],
        path: "embedding",
        "filter":{
          "range":{
            "path":"meta.duration",
            "gt":2,
            "lte":10
          }
        },
        k: 10,
      },
}

But this way I won’t be able to filter using the range operator on another field at the same time.
I tried to use an array of filters instead but I get the following error: “knnBeta.filter” must be a document.
Is there a way I could get this to work ?

You should be able to put a compound within the knnBeta.filter field.

Here is an example:

      "query": {
        "knnBeta": {
          "path": "description",
          "k": 10,
          "vector": [
            8,
            8,
            8
          ],
          "filter": {
            "compound": {
              "mustNot": {
                "text": {
                  "path": "title",
                  "query": "world"
                }
              },
              "must": [
                {
                  "text": {
                    "path": "title",
                    "query": "hello"
                  }
                }
              ]
            }
          }
        }
      },
2 Likes

Works perfectly, thanks :slight_smile:

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