How to use knnBeta together with should operator

Noted from the document that https://www.mongodb.com/docs/atlas/atlas-search/knn-beta/ cannot be used inside compound operator, so I am wondering how I could use knnBeta and should/must operators together to get search results considering scores from both parts?

Hi @williamwjs,

The should and must options are used inside the compound operator. As you (and the documentation) has stated, the knnBeta operator cannot be used inside compound operator so this isn’t currently possible to my knowledge. In saying so, Atlas Vector Search is available as a Preview feature and it’s behaviour is subject to change in future. You can raise feedback regarding this if you would like.

I am also wondering if you’ve tested with the filter option (example linked) within the knnBeta operator to see if this might work for use case?

Regards,
Jason

Hi @Jason_Tran , thank you for the quick reply!

I’ve tested with the filter option inside knnBeta, but it would only work for filter, not for should/must, i.e., with or without them, the score does not change

Hi @williamwjs , you should be able to achieve this by using a compound operator nested within the filter option:

[
  {
    $search:
      {
        index: "default",
        knnBeta: {
          vector: <array-of-numbers-to-search>,
          path: <indexed-field-to-search>,
          k: <number of nearest neighbors to return>,
          filter: {
            compound: {
              must: [
                {
                   // must clause 
                  },
                },
              ],
              should: [
                {
                  // should clause
                  },
                },
              ],
            },
          },
        },
      },
  },
]

Hope this helps!

Thank you @amyjian for the suggestion!

I’ve tried that, but it does not affect the final score at all, with or without them

Hi @Jason_Tran @amyjian , may I ask if you have any other suggestions? Or is it a bug that is being fixed?

Thank you!

Hi @williamwjs, this is the expected behavior of filter. While we do not support combining the score across kNNbeta and should clauses within compound at this time, this is something that is in active consideration for our roadmap. Depending on your needs, something like this might work for you.

Let me know if this helps!

1 Like

@amyjian Thank you Amy! I will give that a try, and let me know when kNNbeta could support compound
Thank you!

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