summaryrefslogtreecommitdiff
path: root/jstests/core/fts_score_sort.js
blob: e074ca68ca23c0ba76f53048572f4884c5de5b40 (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
// Test sorting with text score metadata.
(function() {
    "use strict";

    var t = db.getSiblingDB("test").getCollection("fts_score_sort");
    t.drop();

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

    // $meta sort specification should be rejected if it has additional keys.
    assert.throws(function() {
        t.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() {
        t.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() {
        t.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() {
        t.find({$text: {$search: "textual content"}}, {score: {$meta: "textScore"}})
            .sort({score: "textScore"})
            .itcount();
    });

    // Sort by the text score.
    var results =
        t.find({$text: {$search: "textual content -irrelevant"}}, {score: {$meta: "textScore"}})
            .sort({score: {$meta: "textScore"}})
            .toArray();
    assert.eq(results.length, 2);
    assert.eq(results[0]._id, 0);
    assert.eq(results[1]._id, 1);
    assert.gt(results[0].score, results[1].score);

    // Sort by {_id descending, score} and verify the order is right.
    var results =
        t.find({$text: {$search: "textual content -irrelevant"}}, {score: {$meta: "textScore"}})
            .sort({_id: -1, score: {$meta: "textScore"}})
            .toArray();
    assert.eq(results.length, 2);
    assert.eq(results[0]._id, 1);
    assert.eq(results[1]._id, 0);
    // Note the reversal from above.
    assert.lt(results[0].score, results[1].score);
}());