First, ensure that you have a search index created… and that you’re referring to it correctly in your search query. Next, you can use grouping by duplicateGroupId to get a count… something like this should work:

const pipeline = [
  // Step 1: Perform the search query
  {
    $search: {
      index: "yourSearchIndex", // Specify your search index name
      text: {
        query: "your search term", // Your search term
        path: "your searchable fields" // Fields to search in
      }
    }
  },
  // Step 2: Group by duplicateGroupId to get distinct documents
  {
    $group: {
      _id: "$duplicateGroupId",
      doc: { $first: "$$ROOT" }
    }
  },
  // Step 3: Unwind the grouped documents to process them in a flat structure
  {
    $replaceRoot: { newRoot: "$doc" }
  },
  // Step 4: Facet to count total documents and unique duplicateGroupId, and return the distinct documents
  {
    $facet: {
      totalDocs: [{ $count: "count" }],
      uniqueDuplicateGroups: [
        { $group: { _id: "$duplicateGroupId" } },
        { $count: "count" }
      ],
      results: [
        { $project: { _id: 1, duplicateGroupId: 1, other_fields: 1 } } // Specify the fields you want in the results
      ]
    }
  },
  // Step 5: Project the results to include counts and the distinct documents
  {
    $project: {
      totalDocs: { $arrayElemAt: ["$totalDocs.count", 0] },
      uniqueDuplicateGroups: { $arrayElemAt: ["$uniqueDuplicateGroups.count", 0] },
      results: 1
    }
  }
];

db.articles.aggregate(pipeline).toArray((err, result) => {
  if (err) throw err;
  console.log(result);
});

The output should look like this:

db.articles.aggregate(pipeline).toArray((err, result) => {
  if (err) throw err;
  console.log(result);
});

[
  {
    results: [ [Object], [Object], [Object], [Object], [Object] ],
    totalDocs: 5,
    uniqueDuplicateGroups: 5
  }
]

Hope this helps… let us know how you make out.

1 Like