Skip to content

Commit

Permalink
PATCH: Optimize Message Text Searches for from:Username
Browse files Browse the repository at this point in the history
- Restrict the flexibility of our username search
	- Case-Sensitive only
	- Prefix, NOT substring
	- We still support multiple `from:` lines,
	  even though no one *ever* does that.
- Add index `{ rid: 1, ts: 1, 'u.username': 1 }`
	- Support the `from:Username` prefix query
	- Additionally, index the timestamp, since these searches
	  default to {ts:-1} and it is extremely rare that users
		customize away from that.  Conform to ESR.
- Change FTS index to `{ rid: 1, msg: 'text', 'u.username': 1 }`
	- Support the `from:Username` prefix query, albeit not as well
	  as the other index will.  (IXSCAN vs. B-Tree range)
  • Loading branch information
nmagedman committed Nov 10, 2024
1 parent 91d8e88 commit 54c4ad4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
6 changes: 2 additions & 4 deletions apps/meteor/server/lib/parseMessageSearchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ class MessageSearchQueryParser {
}
from.push(username);

this.query['u.username'] = {
$regex: from.join('|'),
$options: 'i',
};
// Search for case-sensitive prefix match (no substrings)
this.query['u.username'] = { $in: from.map((prefix) => RegExp(`^${prefix}`)) };

return '';
});
Expand Down
7 changes: 4 additions & 3 deletions apps/meteor/server/models/raw/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
{ key: { 'editedBy._id': 1 }, sparse: true },
{ key: { 'rid': 1, 't': 1, 'u._id': 1 } },
{ key: { expireAt: 1 }, expireAfterSeconds: 0 },
{ key: { rid: 1, msg: 'text' } },
{ key: { 'rid': 1, 'msg': 'text', 'u.username': 1 }, name: 'noach__rid_1_msg_text_u.username_1' },
{ key: { 'rid': 1, 'ts': 1, 'u.username': 1 }, name: 'noach__rid_1_ts_1_u.username_1' }, // Support `from:Username` searches (*sans* msg text terms)
{ key: { 'file._id': 1 }, sparse: true },
{ key: { 'mentions.username': 1 }, sparse: true },
{ key: { pinned: 1 }, sparse: true },
Expand Down Expand Up @@ -866,8 +867,8 @@ export class MessagesRaw extends BaseRaw<IMessage> implements IMessagesModel {
findForUpdates(roomId: string, timestamp: Date, options?: FindOptions<IMessage>): FindCursor<IMessage> {
// HACK: To support bulk username renames without causing massive load (due to fetching every ancient post),
// only fetch **recently created** messages.
const duration_30_days_in_ms = 30 * 24 * 60 * 60 * 1000
const date_30_days_ago = new Date(Date.now() - duration_30_days_in_ms)
const duration_30_days_in_ms = 30 * 24 * 60 * 60 * 1000;

Check failure on line 870 in apps/meteor/server/models/raw/Messages.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Variable name `duration_30_days_in_ms` must match one of the following formats: camelCase, UPPER_CASE, PascalCase
const date_30_days_ago = new Date(Date.now() - duration_30_days_in_ms);

Check failure on line 871 in apps/meteor/server/models/raw/Messages.ts

View workflow job for this annotation

GitHub Actions / 🔎 Code Check / Code Lint

Variable name `date_30_days_ago` must match one of the following formats: camelCase, UPPER_CASE, PascalCase

const query = {
rid: roomId,
Expand Down

0 comments on commit 54c4ad4

Please sign in to comment.