Fuzzy search count running very slow

const totalcount = await model.aggregate([                      
                        {
                            $search: {
                                index: 'keywordsearch',
                                text: {
                                    path: 'Keywords',
                                    query: req.query.searchdes,
                                    fuzzy: {
                                        maxEdits: 2,
                                        maxExpansions: 20,
                                    },
                                },
                            },
                        },
                        {
                            $count: 'total_count',
                        },
                    ])

This query running very slow and takes almost 1-2 minutes when ran over a collection of 2 Million data. Keywords field is a comma seperated string values in database.

Is there any way to optimize this query to get faster count results?

Stages after $search will consume all the matching documents, and thus $count will take some time. A more performant option is to use when all you want is the count is to use $searchMeta with the count option with no other stages.

Hi @Aparna_Veer, welcome to the community.

The fuzzy operation is very CPU intensive by nature, if you think of how it’s executed, it approximates a regex expression, which is even more CPU intensive.

You can improve the performance by including another field in the query and index that could reduce the number of documents that need to be checked by the fuzzy search. Another option is to look at your search index and try to reduce its size, reducing the fuzzy comparison when execution a query.

Do I need to redefine or update indexes in someway to use $searchMeta. Can you update my code accordingly? Thanks!

No index changes needed. $searchMeta is similar to $search, but only returns metadata rather than actual search results documents. Check out the last example on the $searchMeta docs to see how to do this: https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/searchMeta/#example