summaryrefslogtreecommitdiff
path: root/jstests/core/text_covered_matching.js
blob: 49cab673e3dde25c51f126c1a9d0983eefea4eee (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// 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 or
// IXSCAN 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,
//   assumes_read_concern_local,
// ]

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.commandWorked(coll.insert({a: "hello", b: 1, c: 1}));
assert.commandWorked(coll.insert({a: "world", b: 2, c: 2}));
assert.commandWorked(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, getWinningPlan(explainResult.queryPlanner), "TEXT_OR"));
assert(!planHasStage(db, getWinningPlan(explainResult.queryPlanner), "OR"));
assert(planHasStage(db, getWinningPlan(explainResult.queryPlanner), "IXSCAN"));
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));
// If we are executing against a mongos, we have more than one occurrence of IXSCAN.
let filteringStage = getPlanStages(explainResult, "IXSCAN")[0];
assert.hasFields(
    filteringStage, ["filter"], "No filter found on IXSCAN: " + tojson(filteringStage));
assert.docEq(filteringStage.filter, {"b": {"$eq": 1}}, "Incorrect filter on IXSCAN.");

// When we include the text score in the projection, we use a TEXT_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, getWinningPlan(explainResult.queryPlanner), "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));
filteringStage = getPlanStages(explainResult, "TEXT_OR")[0];
assert.hasFields(
    filteringStage, ["filter"], "No filter found on TEXT_OR: " + tojson(filteringStage));
assert.docEq(filteringStage.filter, {"b": {"$eq": 1}}, "Incorrect filter on TEXT_OR.");

// When we search more than one term, we perform filtering in the OR stage rather than the
// underlying IXSCANs, but we should get an equivalent result.
explainResult = coll.find({$text: {$search: "hello world"}, b: 1}).explain("executionStats");
assert.commandWorked(explainResult);
assert(planHasStage(db, getWinningPlan(explainResult.queryPlanner), "OR"));
assert.eq(explainResult.executionStats.totalKeysExamined,
          4,
          "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));
filteringStage = getPlanStages(explainResult, "OR")[0];
assert.hasFields(filteringStage, ["filter"], "No filter found on OR: " + tojson(filteringStage));
assert.docEq(filteringStage.filter, {"b": {"$eq": 1}}, "Incorrect filter on OR.");

//
// 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, getWinningPlan(explainResult.queryPlanner), "TEXT_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.commandWorked(coll.insert({a: "hello", b: {d: 1}, c: {e: 1}}));
assert.commandWorked(coll.insert({a: "world", b: {d: 2}, c: {e: 2}}));
assert.commandWorked(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, getWinningPlan(explainResult.queryPlanner), "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));

// When we include the text score in the projection, we use a TEXT_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, getWinningPlan(explainResult.queryPlanner), "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, getWinningPlan(explainResult.queryPlanner), "TEXT_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));
})();