summaryrefslogtreecommitdiff
path: root/jstests/core/fts_index.js
blob: c78301509f15be60d55a7697eefb5c507bd88c19 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
 * Test that:
 * 1. Text indexes properly validate the index spec used to create them.
 * 2. Text indexes properly enforce a schema on the language_override field.
 * 3. Collections may have at most one text index.
 * 4. Text indexes properly handle large documents.
 * 5. Bad weights test cases.
 *
 * @tags: [
 *   # Cannot implicitly shard accessed collections because of collection existing when none
 *   # expected.
 *   assumes_no_implicit_collection_creation_after_drop,
 *   # Has operations which may never complete in stepdown/kill/terminate transaction tests.
 *   operations_longer_than_stepdown_interval_in_txns,
 *   # Uses index building in background
 *   requires_background_index,
 * ]
 */

(function() {
'use strict';

const indexName = "textIndex";

const collNamePrefix = 'fts_index1_';
let collCount = 0;

let coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
coll.getDB().createCollection(coll.getName());

//
// 1. Text indexes properly validate the index spec used to create them.
//

// Spec passes text-specific index validation.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.createIndex({a: "text"}, {name: indexName, default_language: "spanish"}));
assert.eq(1,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);

// Spec fails text-specific index validation ("spanglish" unrecognized).
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailed(
    coll.createIndex({a: "text"}, {name: indexName, default_language: "spanglish"}));
assert.eq(0,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);

// Spec passes general index validation.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.createIndex({"$**": "text"}, {name: indexName}));
assert.eq(1,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);

// Spec fails general index validation ("a.$**" invalid field name for key).
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailed(coll.createIndex({"a.$**": "text"}, {name: indexName}));
assert.eq(0,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);

// SERVER-19519 Spec fails if '_fts' is specified on a non-text index.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailed(coll.createIndex({_fts: 1}, {name: indexName}));
assert.eq(0,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailed(coll.createIndex({_fts: "text"}, {name: indexName}));
assert.eq(0,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);

//
// 2. Text indexes properly enforce a schema on the language_override field.
//

// Can create a text index on a collection where no documents have invalid language_override.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.insert({a: ""}));
assert.commandWorked(coll.insert({a: "", language: "spanish"}));
assert.commandWorked(coll.createIndex({a: "text"}));

// Can't create a text index on a collection containing document with an invalid language_override.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.insert({a: "", language: "spanglish"}));
assert.commandFailed(coll.createIndex({a: "text"}));

// Can insert documents with valid language_override into text-indexed collection.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.createIndex({a: "text"}));
assert.commandWorked(coll.insert({a: ""}));
assert.commandWorked(coll.insert({a: "", language: "spanish"}));

// Can't insert documents with invalid language_override into text-indexed collection.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.createIndex({a: "text"}));
assert.writeError(coll.insert({a: "", language: "spanglish"}));

//
// 3. Collections may have at most one text index.
//

// createIndex() becomes a no-op on an equivalent index spec.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.getDB().createCollection(coll.getName()));
assert.eq(1, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: 1, b: "text", c: 1}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: 1, b: "text", c: 1}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: 1, b: "text", c: 1}, {background: true}));
assert.eq(2, coll.getIndexes().length);
assert.commandFailedWithCode(coll.createIndex({a: 1, b: 1, c: "text"}),
                             ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(
    coll.createIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}, {weights: {b: 1}}),
    ErrorCodes.IndexOptionsConflict);
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: 1, b: "text", c: 1}, {default_language: "english"}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: 1, b: "text", c: 1}, {textIndexVersion: 2}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: 1, b: "text", c: 1}, {language_override: "language"}));
assert.eq(2, coll.getIndexes().length);

// Two index specs are also considered equivalent if they differ only in 'textIndexVersion', and
// createIndex() becomes a no-op on repeated requests that only differ in this way.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.getDB().createCollection(coll.getName()));
assert.eq(1, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: "text"}, {textIndexVersion: 1}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: "text"}, {textIndexVersion: 2}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: "text"}, {textIndexVersion: 3}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: "text"}));
assert.eq(2, coll.getIndexes().length);

coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.getDB().createCollection(coll.getName()));
assert.eq(1, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: "text"}, {textIndexVersion: 3}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: "text"}, {textIndexVersion: 2}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: "text"}, {textIndexVersion: 1}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: "text"}));
assert.eq(2, coll.getIndexes().length);

// createIndex() fails if a second text index would be built.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.getDB().createCollection(coll.getName()));
assert.eq(1, coll.getIndexes().length);
assert.commandWorked(coll.createIndex({a: 1, b: "text", c: 1}));
assert.eq(2, coll.getIndexes().length);
assert.commandFailed(coll.createIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}, {weights: {d: 1}}));
assert.commandFailed(coll.createIndex({a: 1, b: "text", c: 1}, {default_language: "none"}));
assert.commandFailed(coll.createIndex({a: 1, b: "text", c: 1}, {language_override: "idioma"}));
assert.commandFailed(coll.createIndex({a: 1, b: "text", c: 1}, {weights: {d: 1}}));
assert.commandFailed(coll.createIndex({a: 1, b: "text", d: 1}));
assert.commandFailed(coll.createIndex({a: 1, d: "text", c: 1}));
assert.commandFailed(coll.createIndex({b: "text"}));
assert.commandFailed(coll.createIndex({b: "text", c: 1}));
assert.commandFailed(coll.createIndex({a: 1, b: "text"}));

//
// 4. Text indexes properly handle large keys.
//

coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.createIndex({a: "text"}));

let longstring = "";
let longstring2 = "";
for (let i = 0; i < 1024 * 1024; ++i) {
    longstring = longstring + "a";
    longstring2 = longstring2 + "b";
}
assert.commandWorked(coll.insert({_id: 0, a: longstring}));
assert.commandWorked(coll.insert({_id: 1, a: longstring2}));
assert.eq(1, coll.find({$text: {$search: longstring}}).itcount(), "long string not found in index");

//
// 5. Bad weights test cases.
//
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailed(coll.createIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}, {weights: {}}));
assert.commandFailed(coll.createIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}));

// The 'weights' parameter should only be allowed when the index is a text index.
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailedWithCode(coll.createIndex({a: 1, c: 1}, {weights: {d: 1}}),
                             ErrorCodes.CannotCreateIndex);
jsTestLog('indexes ' + coll.getFullName() + ':' + tojson(coll.getIndexes()));
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailedWithCode(coll.createIndex({a: 1, c: 1}, {weights: "$**"}),
                             ErrorCodes.CannotCreateIndex);
jsTestLog('indexes ' + coll.getFullName() + ':' + tojson(coll.getIndexes()));
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailedWithCode(coll.createIndex({a: 1, c: 1}, {weights: {}}),
                             ErrorCodes.CannotCreateIndex);
jsTestLog('indexes ' + coll.getFullName() + ':' + tojson(coll.getIndexes()));
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailedWithCode(coll.createIndex({a: 1, c: 1}, {weights: "$foo"}),
                             ErrorCodes.CannotCreateIndex);
jsTestLog('indexes ' + coll.getFullName() + ':' + tojson(coll.getIndexes()));
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandWorked(coll.insert({_id: 0, a: 'abc', b: 100}));
assert.commandFailedWithCode(coll.createIndex({a: 'text'}, {weights: {b: Number.MAX_VALUE}}),
                             ErrorCodes.CannotCreateIndex);
jsTestLog('indexes ' + coll.getFullName() + ':' + tojson(coll.getIndexes()));
assert.commandFailedWithCode(coll.createIndex({a: 'text'}, {weights: {b: -Number.MAX_VALUE}}),
                             ErrorCodes.CannotCreateIndex);
jsTestLog('indexes ' + coll.getFullName() + ':' + tojson(coll.getIndexes()));

//
// 6. Bad direction value for non-text key in compound index.
//
coll = db.getCollection(collNamePrefix + collCount++);
coll.drop();
assert.commandFailedWithCode(coll.createIndex({a: "text", b: Number.MAX_VALUE}),
                             ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({a: "text", b: -Number.MAX_VALUE}),
                             ErrorCodes.CannotCreateIndex);
jsTestLog('indexes ' + coll.getFullName() + ':' + tojson(coll.getIndexes()));
})();