summaryrefslogtreecommitdiff
path: root/jstests/core/fts_index3.js
blob: 02bd95595918f68f2b165a70cc0aa0eb860a3c9e (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
// Cannot implicitly shard accessed collections because of following errmsg: A single
// update/delete on a sharded collection must contain an exact match on _id or contain the shard
// key.
// @tags: [
//   assumes_unsharded_collection,
//   sbe_incompatible,
// ]

// Test that updates to fields in a text-indexed document are correctly reflected in the text index.
var coll = db.fts_index3;

// 1) Create a text index on a single field, insert a document, update the value of the field, and
// verify that $text with the new value returns the document.
coll.drop();
assert.commandWorked(coll.ensureIndex({a: "text"}));
assert.commandWorked(coll.insert({a: "hello"}));
assert.eq(1, coll.find({$text: {$search: "hello"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {a: "world"}}));
assert.eq(0, coll.find({$text: {$search: "hello"}}).itcount());
assert.eq(1, coll.find({$text: {$search: "world"}}).itcount());

// 2) Same as #1, but with a wildcard text index.
coll.drop();
assert.commandWorked(coll.ensureIndex({"$**": "text"}));
assert.commandWorked(coll.insert({a: "hello"}));
assert.eq(1, coll.find({$text: {$search: "hello"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {a: "world"}}));
assert.eq(0, coll.find({$text: {$search: "hello"}}).itcount());
assert.eq(1, coll.find({$text: {$search: "world"}}).itcount());

// 3) Create a compound text index with an index prefix, insert a document, update the value of the
// index prefix field, and verify that $text with the new value returns the document.
coll.drop();
assert.commandWorked(coll.ensureIndex({a: 1, b: "text"}));
assert.commandWorked(coll.insert({a: 1, b: "hello"}));
assert.eq(1, coll.find({a: 1, $text: {$search: "hello"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {a: 2}}));
assert.eq(0, coll.find({a: 1, $text: {$search: "hello"}}).itcount());
assert.eq(1, coll.find({a: 2, $text: {$search: "hello"}}).itcount());

// 4) Same as #3, but with a wildcard text index.
coll.drop();
assert.commandWorked(coll.ensureIndex({a: 1, "$**": "text"}));
assert.commandWorked(coll.insert({a: 1, b: "hello"}));
assert.eq(1, coll.find({a: 1, $text: {$search: "hello"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {a: 2}}));
assert.eq(0, coll.find({a: 1, $text: {$search: "hello"}}).itcount());
assert.eq(1, coll.find({a: 2, $text: {$search: "hello"}}).itcount());

// 5) Create a compound text index with an index suffix, insert a document, update the value of the
// index suffix field, and verify that $text with the new value returns the document.
coll.drop();
assert.commandWorked(coll.ensureIndex({a: "text", b: 1}));
assert.commandWorked(coll.insert({a: "hello", b: 1}));
assert.eq(1, coll.find({b: 1, $text: {$search: "hello"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {b: 2}}));
assert.eq(0, coll.find({b: 1, $text: {$search: "hello"}}).itcount());
assert.eq(1, coll.find({b: 2, $text: {$search: "hello"}}).itcount());

// 6) Same as #5, but with a wildcard text index.
coll.drop();
assert.commandWorked(coll.ensureIndex({"$**": "text", b: 1}));
assert.commandWorked(coll.insert({a: "hello", b: 1}));
assert.eq(1, coll.find({b: 1, $text: {$search: "hello"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {b: 2}}));
assert.eq(0, coll.find({b: 1, $text: {$search: "hello"}}).itcount());
assert.eq(1, coll.find({b: 2, $text: {$search: "hello"}}).itcount());

// 7) Create a text index on a single field, insert a document, update the language of the document
// (so as to change the stemming), and verify that $text with the new language returns the document.
coll.drop();
assert.commandWorked(coll.ensureIndex({a: "text"}));
assert.commandWorked(coll.insert({a: "testing", language: "es"}));
assert.eq(1, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(0, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {language: "en"}}));
assert.eq(0, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(1, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());

// 8) Same as #7, but with a wildcard text index.
coll.drop();
assert.commandWorked(coll.ensureIndex({"$**": "text"}));
assert.commandWorked(coll.insert({a: "testing", language: "es"}));
assert.eq(1, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(0, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {language: "en"}}));
assert.eq(0, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(1, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());

// 9) Create a text index on a single nested field, insert a document, update the language of the
// subdocument (so as to change the stemming), and verify that $text with the new language returns
// the document.
coll.drop();
assert.commandWorked(coll.ensureIndex({"a.b": "text"}));
assert.commandWorked(coll.insert({a: {b: "testing", language: "es"}}));
assert.eq(1, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(0, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {"a.language": "en"}}));
assert.eq(0, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(1, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());

// 10) Same as #9, but with a wildcard text index.
coll.drop();
assert.commandWorked(coll.ensureIndex({"$**": "text"}));
assert.commandWorked(coll.insert({a: {b: "testing", language: "es"}}));
assert.eq(1, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(0, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {"a.language": "en"}}));
assert.eq(0, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(1, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());

// 11) Create a text index on a single field with a custom language override, insert a document,
// update the language of the document (so as to change the stemming), and verify that $text with
// the new language returns the document.
coll.drop();
assert.commandWorked(coll.ensureIndex({a: "text"}, {language_override: "idioma"}));
assert.commandWorked(coll.insert({a: "testing", idioma: "es"}));
assert.eq(1, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(0, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {idioma: "en"}}));
assert.eq(0, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(1, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());

// 12) Same as #11, but with a wildcard text index.
coll.drop();
assert.commandWorked(coll.ensureIndex({"$**": "text"}, {language_override: "idioma"}));
assert.commandWorked(coll.insert({a: "testing", idioma: "es"}));
assert.eq(1, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(0, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());
assert.commandWorked(coll.update({}, {$set: {idioma: "en"}}));
assert.eq(0, coll.find({$text: {$search: "testing", $language: "es"}}).itcount());
assert.eq(1, coll.find({$text: {$search: "testing", $language: "en"}}).itcount());