fix(quick_search): centralize the repeated exactWordMatch function, per Gemini's suggestion

This commit is contained in:
perfectra1n 2025-11-12 16:49:43 -08:00
parent dee8c115ab
commit e89646ee7c

View File

@ -118,10 +118,7 @@ class NoteContentFulltextExp extends Expression {
// Single word: look for =word with word boundaries // Single word: look for =word with word boundaries
// Split by = to get attribute values, then check each value for exact word match // Split by = to get attribute values, then check each value for exact word match
const parts = normalizedFlatText.split('='); const parts = normalizedFlatText.split('=');
matches = parts.slice(1).some(part => { matches = parts.slice(1).some(part => this.exactWordMatch(normalizedPhrase, part));
const words = part.split(/\s+/);
return words.some(word => word === normalizedPhrase);
});
} else { } else {
// Multi-word phrase: check for substring match // Multi-word phrase: check for substring match
matches = normalizedFlatText.includes(`=${normalizedPhrase}`); matches = normalizedFlatText.includes(`=${normalizedPhrase}`);
@ -136,6 +133,17 @@ class NoteContentFulltextExp extends Expression {
return resultNoteSet; return resultNoteSet;
} }
/**
* Helper method to check if a single word appears as an exact match in text
* @param wordToFind - The word to search for (should be normalized)
* @param text - The text to search in (should be normalized)
* @returns true if the word is found as an exact match (not substring)
*/
private exactWordMatch(wordToFind: string, text: string): boolean {
const words = text.split(/\s+/);
return words.some(word => word === wordToFind);
}
/** /**
* Checks if content contains the exact word (with word boundaries) or exact phrase * Checks if content contains the exact word (with word boundaries) or exact phrase
* This is case-insensitive since content and token are already normalized * This is case-insensitive since content and token are already normalized
@ -151,9 +159,8 @@ class NoteContentFulltextExp extends Expression {
return normalizedContent.includes(normalizedToken); return normalizedContent.includes(normalizedToken);
} }
// For single words, split content into words and check for exact match // For single words, use exact word matching to avoid substring matches
const words = normalizedContent.split(/\s+/); return this.exactWordMatch(normalizedToken, normalizedContent);
return words.some(word => word === normalizedToken);
} }
/** /**
@ -170,9 +177,8 @@ class NoteContentFulltextExp extends Expression {
// For single-word phrases, use word-boundary matching to avoid substring matches // For single-word phrases, use word-boundary matching to avoid substring matches
// e.g., "asd" should not match "asdfasdf" // e.g., "asd" should not match "asdfasdf"
if (!phrase.includes(' ')) { if (!phrase.includes(' ')) {
// Single word: split into words and check for exact match // Single word: use exact word matching to avoid substring matches
const words = normalizedContent.split(/\s+/); return this.exactWordMatch(phrase, normalizedContent);
return words.some(word => word === phrase);
} }
// For multi-word phrases, check if the phrase appears as consecutive words // For multi-word phrases, check if the phrase appears as consecutive words