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
|
// Test that queries with a sort on text metadata return results in the correct order in a sharded
// collection.
var st = new ShardingTest({shards: 2});
st.stopBalancer();
var mongos = st.s0;
var shards = [st.shard0, st.shard1];
var coll = mongos.getCollection("foo.bar");
var admin = mongos.getDB("admin");
var cursor;
//
// Pre-split collection: shard 0 takes {_id: {$lt: 0}}, shard 1 takes {_id: {$gte: 0}}.
//
assert.commandWorked(admin.runCommand({enableSharding: coll.getDB().getName()}));
admin.runCommand({movePrimary: coll.getDB().getName(), to: "shard0000"});
assert.commandWorked(admin.runCommand({shardCollection: coll.getFullName(), key: {_id: 1}}));
assert.commandWorked(admin.runCommand({split: coll.getFullName(), middle: {_id: 0}}));
assert.commandWorked(
admin.runCommand({moveChunk: coll.getFullName(), find: {_id: 0}, to: "shard0001"}));
//
// Insert documents into collection and create text index.
//
assert.writeOK(coll.insert({_id: 1, a: "pizza"}));
assert.writeOK(coll.insert({_id: -1, a: "pizza pizza"}));
assert.writeOK(coll.insert({_id: 2, a: "pizza pizza pizza"}));
assert.writeOK(coll.insert({_id: -2, a: "pizza pizza pizza pizza"}));
assert.commandWorked(coll.ensureIndex({a: "text"}));
//
// Execute query with sort on document score, verify results are in correct order.
//
var results = coll.find({$text: {$search: "pizza"}}, {s: {$meta: "textScore"}})
.sort({s: {$meta: "textScore"}})
.toArray();
assert.eq(results.length, 4);
assert.eq(results[0]._id, -2);
assert.eq(results[1]._id, 2);
assert.eq(results[2]._id, -1);
assert.eq(results[3]._id, 1);
//
// Verify that mongos requires the text metadata sort to be specified in the projection.
//
// Projection not specified at all.
cursor = coll.find({$text: {$search: "pizza"}}).sort({s: {$meta: "textScore"}});
assert.throws(function() {
cursor.next();
});
// Projection specified with incorrect field name.
cursor = coll.find({$text: {$search: "pizza"}}, {t: {$meta: "textScore"}})
.sort({s: {$meta: "textScore"}});
assert.throws(function() {
cursor.next();
});
// Projection specified on correct field but with wrong sort.
cursor = coll.find({$text: {$search: "pizza"}}, {s: 1}).sort({s: {$meta: "textScore"}});
assert.throws(function() {
cursor.next();
});
cursor = coll.find({$text: {$search: "pizza"}}, {s: -1}).sort({s: {$meta: "textScore"}});
assert.throws(function() {
cursor.next();
});
// TODO Test sort on compound key.
st.stop();
|