summaryrefslogtreecommitdiff
path: root/jstests/core/index/fts/fts_score_sort.js
blob: b9103ad0c6bc26633b0a80d94fd1e76a86b39a97 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Test sorting with text score metadata.
(function() {
"use strict";

const kUnavailableMetadataErrCode = 40218;

const coll = db.getSiblingDB("test").getCollection("fts_score_sort");
coll.drop();

assert.commandWorked(coll.insert({_id: 0, a: "textual content"}));
assert.commandWorked(coll.insert({_id: 1, a: "additional content"}));
assert.commandWorked(coll.insert({_id: 2, a: "irrelevant content"}));
assert.commandWorked(coll.createIndex({a: "text"}));

// $meta sort specification should be rejected if it has additional keys.
assert.throws(function() {
    coll.find({$text: {$search: "textual content"}}, {score: {$meta: "textScore"}})
        .sort({score: {$meta: "textScore", extra: 1}})
        .itcount();
});

// $meta sort specification should be rejected if the type of meta sort is not known.
assert.throws(function() {
    coll.find({$text: {$search: "textual content"}}, {score: {$meta: "textScore"}})
        .sort({score: {$meta: "unknown"}})
        .itcount();
});

// Sort spefication should be rejected if a $-keyword other than $meta is used.
assert.throws(function() {
    coll.find({$text: {$search: "textual content"}}, {score: {$meta: "textScore"}})
        .sort({score: {$notMeta: "textScore"}})
        .itcount();
});

// Sort spefication should be rejected if it is a string, not an object with $meta.
assert.throws(function() {
    coll.find({$text: {$search: "textual content"}}, {score: {$meta: "textScore"}})
        .sort({score: "textScore"})
        .itcount();
});

// Sort by the text score.
let results =
    coll.find({$text: {$search: "textual content -irrelevant"}}, {score: {$meta: "textScore"}})
        .sort({score: {$meta: "textScore"}})
        .toArray();
assert.eq(
    results,
    [{_id: 0, a: "textual content", score: 1.5}, {_id: 1, a: "additional content", score: 0.75}]);

// Sort by {_id descending, score} and verify the order is right.
results =
    coll.find({$text: {$search: "textual content -irrelevant"}}, {score: {$meta: "textScore"}})
        .sort({_id: -1, score: {$meta: "textScore"}})
        .toArray();
assert.eq(
    results,
    [{_id: 1, a: "additional content", score: 0.75}, {_id: 0, a: "textual content", score: 1.5}]);

// Can $meta sort by text score without a meta projection using either find or agg.
let expectedResults = [{_id: 0, a: "textual content"}, {_id: 1, a: "additional content"}];
results = coll.find({$text: {$search: "textual content -irrelevant"}})
              .sort({score: {$meta: "textScore"}})
              .toArray();
assert.eq(results, expectedResults);
results = coll.aggregate([
                  {$match: {$text: {$search: "textual content -irrelevant"}}},
                  {$sort: {score: {$meta: "textScore"}}}
              ])
              .toArray();
assert.eq(results, expectedResults);

// $meta-sort by text score fails if there is no $text predicate, for both find and agg.
let error = assert.throws(() => coll.find().sort({score: {$meta: "textScore"}}).itcount());
assert.commandFailedWithCode(error, kUnavailableMetadataErrCode);
error = assert.throws(() => coll.aggregate([{$sort: {score: {$meta: "textScore"}}}]).itcount());
assert.commandFailedWithCode(error, kUnavailableMetadataErrCode);

// Test a sort pattern like {<field>: {$meta: "textScore"}} is legal even if <field> is explicitly
// included by the projection. Test that this is true for both the find and aggregate commands.
expectedResults = [{a: "textual content"}, {a: "additional content"}];
results = coll.find({$text: {$search: "textual content -irrelevant"}}, {_id: 0, a: 1})
              .sort({a: {$meta: "textScore"}})
              .toArray();
assert.eq(results, expectedResults);
results = coll.aggregate([
                  {$match: {$text: {$search: "textual content -irrelevant"}}},
                  {$project: {_id: 0, a: 1}},
                  {$sort: {a: {$meta: "textScore"}}}
              ])
              .toArray();
assert.eq(results, expectedResults);

// Test that both find and agg can $meta-project the textScore with a non-$meta sort on the same
// field. The semantics of find are that the sort logically occurs before the projection, so we
// expect the data to be sorted according the values that were present prior to the $meta
// projection.
expectedResults = [{_id: 0, a: 0.75}, {_id: 1, a: 1.5}];
results = coll.find({$text: {$search: "additional content -irrelevant"}},
                    {_id: 1, a: {$meta: "textScore"}})
              .sort({a: -1})
              .toArray();
assert.eq(results, expectedResults);
results = coll.aggregate([
                  {$match: {$text: {$search: "additional content -irrelevant"}}},
                  {$sort: {a: -1}},
                  {$project: {_id: 1, a: {$meta: "textScore"}}}
              ])
              .toArray();
assert.eq(results, expectedResults);

// Test that an aggregate command with a $project-then-$sort pipeline can sort on the
// $meta-projected data without repeating the $meta operator in the $sort.
results = coll.aggregate([
                  {$match: {$text: {$search: "textual content -irrelevant"}}},
                  {$project: {a: 1, score: {$meta: "textScore"}}},
                  {$sort: {score: -1}}
              ])
              .toArray();
assert.eq(
    results,
    [{_id: 0, a: "textual content", score: 1.5}, {_id: 1, a: "additional content", score: 0.75}]);
}());