summaryrefslogtreecommitdiff
path: root/jstests/core/fts_index.js
blob: 45ed68ce6c6db0c909e82f844ec7f198877214d9 (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
// Cannot implicitly shard accessed collections because of collection existing when none
// expected.
// @tags: [assumes_no_implicit_collection_creation_after_drop]

// 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.

var coll = db.fts_index;
var indexName = "textIndex";
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.
assert.commandWorked(coll.ensureIndex({a: "text"}, {name: indexName, default_language: "spanish"}));
assert.eq(1,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);
coll.dropIndexes();

// Spec fails text-specific index validation ("spanglish" unrecognized).
assert.commandFailed(
    coll.ensureIndex({a: "text"}, {name: indexName, default_language: "spanglish"}));
assert.eq(0,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);
coll.dropIndexes();

// Spec passes general index validation.
assert.commandWorked(coll.ensureIndex({"$**": "text"}, {name: indexName}));
assert.eq(1,
          coll.getIndexes()
              .filter(function(z) {
                  return z.name == indexName;
              })
              .length);
coll.dropIndexes();

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

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

//
// 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.insert({a: ""});
coll.insert({a: "", language: "spanish"});
assert.commandWorked(coll.ensureIndex({a: "text"}));
coll.drop();

// Can't create a text index on a collection containing document with an invalid language_override.
coll.insert({a: "", language: "spanglish"});
assert.commandFailed(coll.ensureIndex({a: "text"}));
coll.drop();

// Can insert documents with valid language_override into text-indexed collection.
assert.commandWorked(coll.ensureIndex({a: "text"}));
coll.insert({a: ""});
assert.writeOK(coll.insert({a: "", language: "spanish"}));
coll.drop();

// Can't insert documents with invalid language_override into text-indexed collection.
assert.commandWorked(coll.ensureIndex({a: "text"}));
assert.writeError(coll.insert({a: "", language: "spanglish"}));
coll.drop();

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

// ensureIndex() becomes a no-op on an equivalent index spec.
assert.commandWorked(coll.getDB().createCollection(coll.getName()));
assert.eq(1, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1}, {background: true}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}, {weights: {b: 1}}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1}, {default_language: "english"}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1}, {textIndexVersion: 2}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1}, {language_override: "language"}));
assert.eq(2, coll.getIndexes().length);
coll.drop();

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

assert.commandWorked(coll.getDB().createCollection(coll.getName()));
assert.eq(1, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: "text"}, {textIndexVersion: 3}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: "text"}, {textIndexVersion: 2}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: "text"}, {textIndexVersion: 1}));
assert.eq(2, coll.getIndexes().length);
assert.commandWorked(coll.ensureIndex({a: "text"}));
assert.eq(2, coll.getIndexes().length);
coll.drop();

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

coll.dropIndexes();

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

assert.commandWorked(coll.ensureIndex({a: "text"}));

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

//
// 5. Bad weights test cases.
//
assert.commandFailed(coll.ensureIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}, {weights: {}}));
assert.commandFailed(coll.ensureIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}));

coll.drop();