Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-search-attachment-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Fix: Message search now includes attachment text fields (text, title, description, pretext, author_name) in the MongoDB text index so searching works on post attachments, not just message body.
24 changes: 22 additions & 2 deletions apps/meteor/server/startup/ensureMessagesTextIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@ import { SystemLogger } from '../lib/logger/system';

const { USE_ROOM_SEARCH_INDEX = 'false' } = process.env;

const ATTACHMENT_TEXT_FIELDS = [
'attachments.text',
'attachments.title',
'attachments.description',
'attachments.pretext',
'attachments.author_name',
] as const;

const TEXT_INDEX_FIELDS = { msg: 'text' as const, ...Object.fromEntries(ATTACHMENT_TEXT_FIELDS.map((f) => [f, 'text' as const])) };

// MongoDB stores a text index's key with `_fts: 'text'` / `_ftsx: 1` placeholders
// and tracks the original text fields in `weights`. Classify by looking at the
// non-placeholder prefix fields plus weights.
const classifyTextIndex = (idx: { key: Record<string, unknown>; weights?: Record<string, number> }) => {
const { weights, key } = idx;
if (weights?.msg !== 1 || Object.keys(weights).length !== 1) {

const expectedWeightKeys = Object.keys(TEXT_INDEX_FIELDS);
const actualWeightKeys = Object.keys(weights ?? {});

if (
actualWeightKeys.length !== expectedWeightKeys.length ||
!expectedWeightKeys.every((k) => weights?.[k] === 1)
) {
return 'other';
}

Expand Down Expand Up @@ -79,7 +96,10 @@ export const ensureMessagesTextIndex = async (): Promise<void> => {
shape: desiredShape,
});
try {
const name = await Messages.col.createIndex(desiredShape === 'room-scoped' ? { rid: 1, msg: 'text' } : { msg: 'text' });
const name = await Messages.col.createIndex(
desiredShape === 'room-scoped' ? { rid: 1, ...TEXT_INDEX_FIELDS } : TEXT_INDEX_FIELDS,
{ name: desiredShape === 'room-scoped' ? 'messages_text_room' : 'messages_text' },
);
SystemLogger.startup({ msg: 'created messages text index', name, shape: desiredShape });
} catch (err) {
SystemLogger.error({ msg: 'failed to create messages text index', shape: desiredShape, err });
Expand Down