Sorting by $meta textScore takes 10s - is it possible to return a filtered & sorted text search more quickly?


So I have a sample collection Posts which I filled with 10 million documents for testing purposes. The schema is as follows


The text index is on (title, body, tags) - a simple search like the following returns in 0ms

db.posts.find({$text: {$search: “technology computer phone”}}).limit(50)

Of course I realise this is just taking the first 50 documents which match the search stems, then returning them. What I would want is for the more relevant/higher scoring matches to appear first though

db.posts.find({$text: {$search: “technology computer phone”}}).sort({score:{$meta:“textScore”}}).limit(50)

This query takes much longer, however - about 10 seconds. So I have a few questions:

  1. Is there anyway to speed this up in pure MongoDB?

  2. For a search like this on 10 million documents, where 1.25 million match the search terms (i.e. 1/8th of the total), for them to be sorted by relevance, what kind of execution time would you expect on Atlas? And what would be the cost of such a configuration? Let’s say if I wanted to reduce that query time down from 10s to 1s

Hi @Mon_Go,

Welcome to MongoDB community.

To understand the current query performance its best to get explain (“executionStats”) , but I believe it produces an in memory sort before fetching.

Have you tried first building the score and then sorting like here:

We strongly recommend considering Atlas search as a more robust alternative to text indexing. The metadata sorting and available options is also more efficient there: