$limit and $count not working when using $seach & $match together - Atlas Search

52 Views Asked by At
let pipeline = [
    {
        $search: {
            index: indexes.CAREERS_SEARCH_INDEX,
            compound: {
                should: [
                    {
                        autocomplete: {
                            query: searchText,
                            path: 'role_title',
                            fuzzy: {
                                maxEdits: 1,
                            },
                        }
                    },
                    {
                        autocomplete: {
                            query: searchText,
                            path: 'industry',
                            fuzzy: {
                                maxEdits: 1,
                            },
                        }
                    },
                    {
                        autocomplete: {
                            query: searchText,
                            path: 'overview',
                            fuzzy: {
                                maxEdits: 1,
                            },
                        }
                    },
                ],
            },
        },
    },
];

if (industries && industries.length) {
    pipeline.push({
        $match: { industry: { $in: industries } },
    });
}

pipeline.push({
    $limit: limit
});

pipeline.push({
    $group: {
        _id: null,
        search_results: { $push: "$$ROOT" },
        matched_count: { $sum: 1 }
    }
});

pipeline.push({
    $project: {
        _id: 0,
        search_results: 1,
        matched_count: 1
    }
});

let results = await CareerModel.aggregate(pipeline);
results = results[0];
data.careers = results.search_results;
data.count = results.matched_count;
return data;

If the results are 40 then limiting the results to 10 changes the matched_count to 10 as well when it should be 40 actually. The mongoose documentation on atlas search is very limited and does not cover cases like these. If you have faced a similar issue, it would be great if you can just point me in the right direction. Thanks!

0

There are 0 best solutions below