$searchMeta with $match filters to get totalcount

Hello I am using below query to get total count of document and its working fine

{
                $searchMeta: {
                    index: indexName,
                    compound: {
                        should: [
                            {
                                text: {
                                    path: path,
                                    query: s,
                                    fuzzy: {
                                        maxEdits: 2,
                                        maxExpansions: 20,
                                    },
                                },
                            },
                            {
                                text: {
                                    query: Soundex(s),
                                    path: 'phoneticCode',
                                },
                            },
                        ],
                    },
                    count: {
                        type: 'total',
                    },
                },
            }

But I also want to add $match filters here like

 {
                $match: {
                    CountryNo: 14,
                    Population:10000,
                },
            }

I tried doing like this but it doesn’t worked

[
            {
                $searchMeta: {
                    index: indexName,
                    compound: {
                        should: [
                            {
                                text: {
                                    path: path,
                                    query: s,
                                    fuzzy: {
                                        maxEdits: 2,
                                        maxExpansions: 20,
                                    },
                                },
                            },
                            {
                                text: {
                                    query: Soundex(s),
                                    path: 'phoneticCode',
                                },
                            },
                        ],
                    },
                    count: {
                        type: 'total',
                    },
                },
            },
            {
                $match: {
                    CountryNo: 14,
                    Population:10000
                },
            },
        ]

Can someone please suggest me ways so that I can get the $searchMeta count that also include filters?

Rather than do any matching after $searchMeta, incorporate the matching into your compound operator under a filter array - so that the criteria is considered in parallel to the search.

In general, it’s best not to have any stages after a search stage. In this case, $searchMeta is not returning documents, but rather metadata, so there’s nothing to $match after. If you had used $search instead, it would have worked but not been as performant as putting the criteria into the search/searchMeta search operator criteria.

Actually I need to get the total count of documents that is returned after the fuzzy search with some conditions. Counting normally took a lot of time for me like 1-2 minutes so I had to make the count query faster. I found $searchMeta which can count search fuzzy results very fast but I also had other conditions to match which was not working with $searchMeta. What should I do?

We should figure out what was not working with your filtering/matching within $searchMeta. It’s always best to incorporate all search/filtering/exclusion conditions inside $search or $searchMeta, rather than using $match afterwards. Look at using a dynamic mapped index so that CountryNo and Population are indexed as numeric types automatically, then using equals operator within a compound.filter array.