summaryrefslogtreecommitdiff
path: root/jstests/core/text_covered_matching.js
blob: f3fe9908d1b2fdeb93da57254ed025a07a8920b8 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// When a $text query includes an additional predicate that can be covered with a suffix of a $text
// index, we expect the query planner to attach that predicate as a "filter" to the TEXT_OR or OR
// stage, so that it can be used to filter non-matching documents without fetching them.
//
// SERVER-26833 changes how the text index is searched in the case when the projection does not
// include the 'textScore' meta field, so we are adding this test to ensure that we still get the
// same covered matching behavior with and without 'textScore' in the projection.
//
// @tags: [assumes_balancer_off]

load("jstests/libs/analyze_plan.js");

(function() {
    "use strict";
    const coll = db.text_covered_matching;

    coll.drop();
    assert.commandWorked(coll.createIndex({a: "text", b: 1}));
    assert.writeOK(coll.insert({a: "hello", b: 1, c: 1}));
    assert.writeOK(coll.insert({a: "world", b: 2, c: 2}));
    assert.writeOK(coll.insert({a: "hello world", b: 3, c: 3}));

    //
    // Test the query {$text: {$search: "hello"}, b: 1} with and without the 'textScore' in the
    // output.
    //

    // Expected result:
    //   - We examine two keys, for the two documents with "hello" in their text;
    //   - we examine only one document, because covered matching rejects the index entry for
    //     which b != 1;
    //   - we return exactly one document.
    let explainResult = coll.find({$text: {$search: "hello"}, b: 1}).explain("executionStats");
    assert.commandWorked(explainResult);
    assert(planHasStage(db, explainResult.queryPlanner.winningPlan, "OR"));
    assert.eq(explainResult.executionStats.totalKeysExamined,
              2,
              "Unexpected number of keys examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.totalDocsExamined,
              1,
              "Unexpected number of documents examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.nReturned,
              1,
              "Unexpected number of results returned: " + tojson(explainResult));

    // When we include the text score in the projection, we use a TEXT_OR instead of an OR in our
    // query plan, which changes how filtering is done. We should get the same result, however.
    explainResult = coll.find({$text: {$search: "hello"}, b: 1},
                              {a: 1, b: 1, c: 1, textScore: {$meta: "textScore"}})
                        .explain("executionStats");
    assert.commandWorked(explainResult);
    assert(planHasStage(db, explainResult.queryPlanner.winningPlan, "TEXT_OR"));
    assert.eq(explainResult.executionStats.totalKeysExamined,
              2,
              "Unexpected number of keys examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.totalDocsExamined,
              1,
              "Unexpected number of documents examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.nReturned,
              1,
              "Unexpected number of results returned: " + tojson(explainResult));

    //
    // Test the query {$text: {$search: "hello"}, c: 1} with and without the 'textScore' in the
    // output.
    //

    // Expected result:
    //   - We examine two keys, for the two documents with "hello" in their text;
    //   - we examine more than just the matching document, because we need to fetch documents in
    //     order to examine the non-covered 'c' field;
    //   - we return exactly one document.
    explainResult = coll.find({$text: {$search: "hello"}, c: 1}).explain("executionStats");
    assert.commandWorked(explainResult);
    assert(planHasStage(db, explainResult.queryPlanner.winningPlan, "OR"));
    assert.eq(explainResult.executionStats.totalKeysExamined,
              2,
              "Unexpected number of keys examined: " + tojson(explainResult));
    assert.gt(explainResult.executionStats.totalDocsExamined,
              1,
              "Unexpected number of documents examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.nReturned,
              1,
              "Unexpected number of results returned: " + tojson(explainResult));

    // As before, including the text score in the projection changes how filtering occurs, but we
    // still expect the same result.
    explainResult = coll.find({$text: {$search: "hello"}, c: 1},
                              {a: 1, b: 1, c: 1, textScore: {$meta: "textScore"}})
                        .explain("executionStats");
    assert.commandWorked(explainResult);
    assert.eq(explainResult.executionStats.totalKeysExamined,
              2,
              "Unexpected number of keys examined: " + tojson(explainResult));
    assert.gt(explainResult.executionStats.totalDocsExamined,
              1,
              "Unexpected number of documents examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.nReturned,
              1,
              "Unexpected number of results returned: " + tojson(explainResult));

    //
    // Test the first query again, but this time, use dotted fields to make sure they don't confuse
    // the query planner:
    //   {$text: {$search: "hello"}, "b.d": 1}
    //
    coll.drop();
    assert.commandWorked(coll.createIndex({a: "text", "b.d": 1}));
    assert.writeOK(coll.insert({a: "hello", b: {d: 1}, c: {e: 1}}));
    assert.writeOK(coll.insert({a: "world", b: {d: 2}, c: {e: 2}}));
    assert.writeOK(coll.insert({a: "hello world", b: {d: 3}, c: {e: 3}}));

    // Expected result:
    //   - We examine two keys, for the two documents with "hello" in their text;
    //   - we examine only one document, because covered matching rejects the index entry for
    //     which b != 1;
    //   - we return exactly one document.
    explainResult = coll.find({$text: {$search: "hello"}, "b.d": 1}).explain("executionStats");
    assert.commandWorked(explainResult);
    assert(planHasStage(db, explainResult.queryPlanner.winningPlan, "OR"));
    assert.eq(explainResult.executionStats.totalKeysExamined,
              2,
              "Unexpected number of keys examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.totalDocsExamined,
              1,
              "Unexpected number of documents examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.nReturned,
              1,
              "Unexpected number of results returned: " + tojson(explainResult));

    // When we include the text score in the projection, we use a TEXT_OR instead of an OR in our
    // query plan, which changes how filtering is done. We should get the same result, however.
    explainResult = coll.find({$text: {$search: "hello"}, "b.d": 1},
                              {a: 1, b: 1, c: 1, textScore: {$meta: "textScore"}})
                        .explain("executionStats");
    assert.commandWorked(explainResult);
    assert(planHasStage(db, explainResult.queryPlanner.winningPlan, "TEXT_OR"));
    assert.eq(explainResult.executionStats.totalKeysExamined,
              2,
              "Unexpected number of keys examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.totalDocsExamined,
              1,
              "Unexpected number of documents examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.nReturned,
              1,
              "Unexpected number of results returned: " + tojson(explainResult));

    //
    // Test the second query again, this time with dotted fields:
    //   {$text: {$search: "hello"}, "c.e": 1}
    //

    // Expected result:
    //   - We examine two keys, for the two documents with "hello" in their text;
    //   - we examine more than just the matching document, because we need to fetch documents in
    //     order to examine the non-covered 'c' field;
    //   - we return exactly one document.
    explainResult = coll.find({$text: {$search: "hello"}, "c.e": 1}).explain("executionStats");
    assert.commandWorked(explainResult);
    assert(planHasStage(db, explainResult.queryPlanner.winningPlan, "OR"));
    assert.eq(explainResult.executionStats.totalKeysExamined,
              2,
              "Unexpected number of keys examined: " + tojson(explainResult));
    assert.gt(explainResult.executionStats.totalDocsExamined,
              1,
              "Unexpected number of documents examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.nReturned,
              1,
              "Unexpected number of results returned: " + tojson(explainResult));

    // As before, including the text score in the projection changes how filtering occurs, but we
    // still expect the same result.
    explainResult = coll.find({$text: {$search: "hello"}, "c.e": 1},
                              {a: 1, b: 1, c: 1, textScore: {$meta: "textScore"}})
                        .explain("executionStats");
    assert.commandWorked(explainResult);
    assert.eq(explainResult.executionStats.totalKeysExamined,
              2,
              "Unexpected number of keys examined: " + tojson(explainResult));
    assert.gt(explainResult.executionStats.totalDocsExamined,
              1,
              "Unexpected number of documents examined: " + tojson(explainResult));
    assert.eq(explainResult.executionStats.nReturned,
              1,
              "Unexpected number of results returned: " + tojson(explainResult));
})();