summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/group/text_score_grouping.js
blob: 1fa328a196eebe2301f96662e9d3edf36c44b3b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Tests that a user can group on the text score.
 */
(function() {
"use strict";
const coll = db.text_score_grouping;

coll.drop();

assert.commandWorked(coll.insert({"_id": 1, "title": "cakes"}));
assert.commandWorked(coll.insert({"_id": 2, "title": "cookies and cakes"}));

assert.commandWorked(coll.createIndex({title: "text"}));

// Make sure there are two distinct groups for a text search with no other dependencies.
var results = coll.aggregate([
                      {$match: {$text: {$search: "cake cookies"}}},
                      {$group: {_id: {$meta: "textScore"}, count: {$sum: 1}}}
                  ])
                  .toArray();
assert.eq(results.length, 2);

// Make sure there are two distinct groups if there are other fields required by the group.
results = coll.aggregate([
                  {$match: {$text: {$search: "cake cookies"}}},
                  {$group: {_id: {$meta: "textScore"}, firstId: {$first: "$_id"}}}
              ])
              .toArray();
assert.eq(results.length, 2);
}());