Atlas search filter on multiple values

I have created a collection which contains documents from other collections to enable a sort of global search. It works fine but now I’d like to be able to filter out a set of types that should be included in the search results.

I have a compound query that I’m expanding with a filter operator which works fine when I just what to filter one specific type like this

      $search: {
        index: "global",
        compound: {
          should: [
            {
              autocomplete: {
                path: "name",
                query: searchTerm,
                score: {
                  boost: {
                    value: 3,
                  },
                },
              },
            },
            {
              text: {
                path: "name",
                query: searchTerm,
                fuzzy: {
                  maxEdits: 1,
                },
              },
            },
          ],
          minimumShouldMatch: 1,
          filter: [
            {
              equals: {
                value: 0,
                path: "type",
              },
            },
          ],
        },
      },

What I really want is to have the the filter accept an array of accepted values but equals don’t work like that. I need something like $in but I’m not sure if that exists in search.

I could do the filtering afterwords in the pipeline but that seems inefficient. Any ideas?

Hey @Daniel_Reuterwall,

Welcome to the MongoDB Community Forums! :leaves:

You’re correct. Equals does not accept multiple values or arrays. I understand this may not be optimal when there are a large amount of values to be filtered. This behavior may change in the future but for now, the only workaround you can try is to use compound with multiple should clauses inside the filter to achieve what you want to do.

Please feel free to reach out for anything else as well.

Regards,
Satyam

1 Like

Works great, thanks for the guidance!

Ended up with a filter like this:

          filter: [
            {
              compound: {
                should: [
                  {
                    equals: {
                      value: 0,
                      path: "type",
                    },
                  },
                  {
                    equals: {
                      value: 4,
                      path: "type",
                    },
                  },
                ],
              },
            },
          ],
1 Like

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