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
perf(search): reject impossible fuzzy matches before scanning words
Elian Doran0753b7ce0f66
diff --git a/packages/trilium-core/src/services/search/utils/text_utils.spec.ts b/packages/trilium-core/src/services/search/utils/text_utils.spec.tsindex 5d998ea7e3..36e779a9b5 100644--- a/packages/trilium-core/src/services/search/utils/text_utils.spec.ts+++ b/packages/trilium-core/src/services/search/utils/text_utils.spec.ts@@ -147,6 +147,26 @@ describe('Fuzzy Search Core', () => {// Sanity: a genuine 1-edit typo of a 4-char token still matches.expect(fuzzyMatchWordWithResult('sync', 'sinc')).toBe('sinc');});++ it('finds a word within the distance wherever the edits land', () => {+ // The chunk prefilter splits "abcdefg" into "ab", "cd" and "efg". Two edits reach at+ // most two of those, so every two-substitution variant keeps one chunk intact and+ // must still be found — a filter that left any chunk out would drop the variants+ // whose edits fall on the other two.+ const token = "abcdefg";++ for (let i = 0; i < token.length; i++) {+ for (let j = i + 1; j < token.length; j++) {+ const characters = token.split("");+ characters[i] = "z";+ characters[j] = "z";+ const variant = characters.join("");++ expect(calculateOptimizedEditDistance(token, variant, 2)).toBeLessThanOrEqual(2);+ expect(fuzzyMatchWordWithResult(token, variant, 2)).toBe(variant);+ }+ }+ });});describe('wordsContainPhrase', () => {diff --git a/packages/trilium-core/src/services/search/utils/text_utils.ts b/packages/trilium-core/src/services/search/utils/text_utils.tsindex 0314ea5d51..1bfbbcc3e1 100644--- a/packages/trilium-core/src/services/search/utils/text_utils.ts+++ b/packages/trilium-core/src/services/search/utils/text_utils.ts@@ -323,6 +323,10 @@ export function fuzzyMatchWordWithResult(token: string, text: string, maxDistanc// A substring is not a fuzzy match, so there is no whole-text shortcut here. Callers that// want substring semantics run their own `includes()` check first.+ if (!containsAnyChunk(normalizedText, normalizedToken, maxDistance)) {+ return null;+ }+// For fuzzy matching, split into words and check each against the tokenconst words = normalizedText.split(/\s+/).filter(word => word.length > 0);const originalWords = text.split(/\s+/).filter(word => word.length > 0);@@ -357,6 +361,31 @@ export function fuzzyMatchWordWithResult(token: string, text: string, maxDistanc}}+/**+ * Whether `text` can hold a word within `maxDistance` edits of `token`.+ *+ * Splitting the token into `maxDistance + 1` contiguous chunks means that many edits can touch at+ * most `maxDistance` of them, so a word within the distance keeps at least one chunk verbatim —+ * and so does any text containing that word. Each chunk costs one native substring scan, and the+ * check never rejects a text the per-word scan would have matched, so it can front that scan.+ */+function containsAnyChunk(text: string, token: string, maxDistance: number): boolean {+ const chunkCount = maxDistance + 1;+ const chunkLength = Math.floor(token.length / chunkCount);++ for (let i = 0; i < chunkCount; i++) {+ const start = i * chunkLength;+ // The last chunk takes the remainder, so no character of the token goes unexamined.+ const end = i === chunkCount - 1 ? token.length : start + chunkLength;++ if (text.includes(token.slice(start, end))) {+ return true;+ }+ }++ return false;+}+/*** Checks if a word matches a token with fuzzy matching.* Optimized for common case where distances are small.