Aggregation with a minimum searchScore

Hi, we’re using Atlas Search in some sort of this way:

[
  {
    $search: {
      index: "search_person",
      compound: {
        should: [
          {
            text: {
              query: "robin",
              path: "name",
            },
          },
          {
            text: {
              query: "robin",
              path: ["iban", "email"],
              score: {
                boost: {
                  value: 10,
                },
              },
            },
          },
        ],
      },
      sort: {
        _score: {
          $meta: "searchScore",
          order: -1,
        },
        createdAt: 1,
        _id: -1,
      },
    },
  },
  {
    $addFields: {
      _score: {
        $meta: "searchScore",
      },
    },
  },
]

But now we want to filter on searchScore greater than 10.

Adding a $match stage after the $search inside the aggregation makes it (obviously) very slow:

  {
    $match: {
      _score: {
        $gte: 10
      },
    },
  }

The documentation recommends to use filter instead of $match inside the compound, but I think I don’t have searchScore available at that point? Otherwise I could have tried with a range.

Thank you in advance.

Kinds regards,
Robin van Wijngaarden

Hi, Search PM here. We don’t support this out of the box. Can you explain why you are trying to do this?

Hi Elle,

Thanks for your swift reply!

A lot of applications use offset pagination, but in our product we’re using cursor-based pagination. That means we need to have a non-deterministic way of generating the results.

For example: on page 1 with 10 results, the top one has the highest searchScore and the 10th result will have something like 87.48239. Then the first result on page 2 should start with 87.48239 and lower. That’s the reason I’m asking for a $gt or $lt option on searchScore without using $match (because that’s slow).

I hope this makes our usecase more clear.

Ah ok, we are delivering an in-search cursor based pagination solution early next year that when used with sorting by score I believe would do exactly as you’re describing. I wonder if there is some sort of solution in the meantime that can be used just leveraging our sort by score functionality.

FYI in-search cursor based pagination is now available. See docs here.