Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(search): also support exact phrase matching such as `='test phrase'`
perf3ctfb0d971e4848
diff --git a/apps/server/src/services/search/expressions/note_content_fulltext.ts b/apps/server/src/services/search/expressions/note_content_fulltext.tsindex 81250dda54..cf68f6e23c 100644--- a/apps/server/src/services/search/expressions/note_content_fulltext.ts+++ b/apps/server/src/services/search/expressions/note_content_fulltext.ts@@ -118,7 +118,7 @@ class NoteContentFulltextExp extends Expression {}/**- * Checks if content contains the exact word (with word boundaries)+ * Checks if content contains the exact word (with word boundaries) or exact phrase* This is case-insensitive since content and token are already normalized*/private containsExactWord(token: string, content: string): boolean {@@ -126,7 +126,13 @@ class NoteContentFulltextExp extends Expression {const normalizedToken = normalizeSearchText(token);const normalizedContent = normalizeSearchText(content);- // Split content into words and check for exact match+ // If token contains spaces, it's a multi-word phrase from quotes+ // Check for substring match (consecutive phrase)+ if (normalizedToken.includes(' ')) {+ return normalizedContent.includes(normalizedToken);+ }++ // For single words, split content into words and check for exact matchconst words = normalizedContent.split(/\s+/);return words.some(word => word === normalizedToken);}diff --git a/apps/server/src/services/search/services/build_comparator.ts b/apps/server/src/services/search/services/build_comparator.tsindex 0f8020de39..c090b458f3 100644--- a/apps/server/src/services/search/services/build_comparator.ts+++ b/apps/server/src/services/search/services/build_comparator.ts@@ -15,18 +15,19 @@ type Comparator<T> = (comparedValue: T) => (val: string) => boolean;const stringComparators: Record<string, Comparator<string>> = {"=": (comparedValue) => (val) => {// For the = operator, check if the value contains the exact word or phrase- // This is case-insensitive since both values are already lowercased+ // This is case-insensitiveif (!val) return false;const normalizedVal = normalizeSearchText(val);const normalizedCompared = normalizeSearchText(comparedValue);- // If comparedValue has multiple words, check for exact phrase+ // If comparedValue has spaces, it's a multi-word phrase+ // Check for substring match (consecutive phrase)if (normalizedCompared.includes(" ")) {return normalizedVal.includes(normalizedCompared);}- // For single word, split into words and check for exact match+ // For single word, split into words and check for exact word matchconst words = normalizedVal.split(/\s+/);return words.some(word => word === normalizedCompared);},@@ -37,12 +38,13 @@ const stringComparators: Record<string, Comparator<string>> = {const normalizedVal = normalizeSearchText(val);const normalizedCompared = normalizeSearchText(comparedValue);- // If comparedValue has multiple words, check for exact phrase+ // If comparedValue has spaces, it's a multi-word phrase+ // Check for substring match (consecutive phrase) and negateif (normalizedCompared.includes(" ")) {return !normalizedVal.includes(normalizedCompared);}- // For single word, split into words and check for exact match+ // For single word, split into words and check for exact word match, then negateconst words = normalizedVal.split(/\s+/);return !words.some(word => word === normalizedCompared);},