Text search with mongodb taking more than 30 seconds

I am using this simple query. When I use it without sort it runs right away but when I use sort it takes above 30 seconds. Can anyone please explain how can I optimise the query?

db.getCollection('calls').find({
    "$text": { "$search": "hello", "$language": "en" }}
).sort({"score":{"$meta":"textScore"}})
1 Like

Hi @Ashish_Lal - Welcome to the community :slight_smile:

When I use it without sort it runs right away but when I use sort it takes above 30 seconds.

The sort that is being performed is on a computed field score, due to this, MongoDB cannot make use of indexes to speed up the sort. If you run a .explain(“executionStats”) on the command, you should see that a SORT stage is part of the output. The SORT stage reads all input documents before returning any output documents. e.g.

db.calls.explain("executionStats").find({
    "$text": { "$search": "hello", "$language": "en" }}
).sort({"score":{"$meta":"textScore"}})

You may find the information on the use indexes to sort query results document useful regarding the above.

Can anyone please explain how can I optimise the query?

As I presume you wish to leave the sort stage, a possible alternative would be to add additional conditions in the query portion to return fewer documents as this would also lead to fewer documents being read and sorted. However, if your query is returning all documents that are required without any redundant documents then there would be no further optimizations that I can think of with regards to the sort on the computed score field.

if it suits your use case, I would also recommend you take a look at Atlas Search as Atlas Search returns the documents from highest score to lowest by default. Please note that Atlas Search is only available in Atlas although it is available to try on free tier M0 clusters with the following limitations.

Regards,
Jason

4 Likes