Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): restore fragment-contains matching for ~* fuzzy operator (#10616)

perfectra1n779db26644f7
diff --git a/packages/trilium-core/src/services/search/expressions/note_content_fulltext.spec.ts b/packages/trilium-core/src/services/search/expressions/note_content_fulltext.spec.ts
index 50d9123fdf..9d37999809 100644
--- a/packages/trilium-core/src/services/search/expressions/note_content_fulltext.spec.ts
+++ b/packages/trilium-core/src/services/search/expressions/note_content_fulltext.spec.ts
@@ -1,5 +1,13 @@
-import { describe, expect,it } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
+import becca from "../../../becca/becca.js";
+import BBranch from "../../../becca/entities/bbranch.js";
+import BNote from "../../../becca/entities/bnote.js";
+import { getContext } from "../../context.js";
+import noteService from "../../notes.js";
+import NoteSet from "../note_set.js";
+import SearchContext from "../search_context.js";
+import { NoteBuilder } from "../../../test/becca_mocking.js";
import NoteContentFulltextExp from "./note_content_fulltext.js";
describe("Fuzzy Search Operators", () => {
@@ -17,3 +25,35 @@ describe("Fuzzy Search Operators", () => {
expect(() => new NoteContentFulltextExp("~*", { tokens: ["wo"] })).toThrow(); // Too short
});
});
+
+describe("~* fuzzy-contains against note content", () => {
+ beforeEach(() => {
+ becca.reset();
+ new NoteBuilder(new BNote({ noteId: "root", title: "root", type: "text" }));
+ new BBranch({ branchId: "none_root", noteId: "root", parentNoteId: "none", notePosition: 10 });
+ });
+
+ function matches(operator: string, token: string, content: string): boolean {
+ const note = getContext().init(() => noteService.createNewNote({
+ parentNoteId: "root",
+ title: "Untitled",
+ content,
+ type: "text"
+ }).note);
+
+ const exp = new NoteContentFulltextExp(operator, { tokens: [token] });
+ const result = exp.execute(new NoteSet([note]), {}, new SearchContext());
+ return result.notes.some(n => n.noteId === note.noteId);
+ }
+
+ it("matches a fragment that is a proper substring of a longer word (#10616)", () => {
+ // "progr" is a proper substring of "programming"; fuzzy contains must match.
+ expect(matches("~*", "progr", "learn programming today")).toBe(true);
+ expect(matches("~*", "progr", "nothing relevant here")).toBe(false);
+ });
+
+ it("still matches a fuzzy typo that is not a substring", () => {
+ // "programing" is one edit from "programming" but not a substring of it.
+ expect(matches("~*", "programing", "learn programming today")).toBe(true);
+ });
+});
diff --git a/packages/trilium-core/src/services/search/expressions/note_content_fulltext.ts b/packages/trilium-core/src/services/search/expressions/note_content_fulltext.ts
index 52e5368f03..d6a60092f3 100644
--- a/packages/trilium-core/src/services/search/expressions/note_content_fulltext.ts
+++ b/packages/trilium-core/src/services/search/expressions/note_content_fulltext.ts
@@ -154,10 +154,11 @@ class NoteContentFulltextExp extends Expression {
const normalizedToken = normalizeSearchText(token);
const normalizedContent = normalizeSearchText(content);
- // If token contains spaces, it's a multi-word phrase from quotes
- // Check for substring match (consecutive phrase)
+ // If token contains spaces, it's a multi-word phrase from quotes. Scan the
+ // tokenized content for a consecutive run so the phrase matches across
+ // boundary punctuation (consistent with containsExactPhrase).
if (normalizedToken.includes(' ')) {
- return normalizedContent.includes(normalizedToken);
+ return wordsContainPhrase(tokenizeIntoWords(content), tokenizeIntoWords(normalizedToken));
}
// For single words, use exact word matching to avoid substring matches
@@ -261,7 +262,7 @@ class NoteContentFulltextExp extends Expression {
(this.operator === "*=*" && content.includes(token)) ||
(this.operator === "%=" && getRegex(token).test(content)) ||
(this.operator === "~=" && this.matchesWithFuzzy(content, noteId)) ||
- (this.operator === "~*" && this.fuzzyMatchToken(normalizeSearchText(token), normalizeSearchText(content)))
+ (this.operator === "~*" && this.fuzzyContainsToken(normalizeSearchText(token), normalizeSearchText(content)))
) {
resultNoteSet.add(becca.notes[noteId]);
}
@@ -434,6 +435,16 @@ class NoteContentFulltextExp extends Expression {
return limitedWords.some(word => this.fuzzyMatchSingle(token, word));
}
+ /**
+ * Fuzzy CONTAINS for a single "~*" token: a plain substring (fragment) match
+ * counts (so "progr" matches "programming"), otherwise fall back to per-word
+ * fuzzy matching. Mirrors the substring fallback used by "~=" and keeps "~="
+ * (fuzzy equals, word-level via fuzzyMatchToken) unaffected.
+ */
+ private fuzzyContainsToken(token: string, content: string): boolean {
+ return content.includes(token) || this.fuzzyMatchToken(token, content);
+ }
+
/**
* Fuzzy matches a single token against a single word
*/
diff --git a/packages/trilium-core/src/services/search/services/build_comparator.spec.ts b/packages/trilium-core/src/services/search/services/build_comparator.spec.ts
index ed56b9c4dc..31ca2dd010 100644
--- a/packages/trilium-core/src/services/search/services/build_comparator.spec.ts
+++ b/packages/trilium-core/src/services/search/services/build_comparator.spec.ts
@@ -155,5 +155,20 @@ describe("buildComparator", () => {
expect(fuzzy("crab")).toBe(true);
expect(fuzzy("xyz")).toBe(false);
});
+
+ it("~* matches a substring fragment of a longer word (fuzzy contains, #10616)", () => {
+ // "~*" is fuzzy CONTAINS, so a fragment like "progr" must match a value
+ // containing "programming" even though it is a proper substring.
+ const fuzzy = buildComparator("~*", "progr");
+ expect(fuzzy?.("learn programming today")).toBe(true);
+ expect(fuzzy?.("unrelated content")).toBe(false);
+ });
+
+ it("~* still matches a fuzzy typo that is not a substring", () => {
+ // "progrms" is one edit from "programs" (not a substring), so fuzzy
+ // matching must still catch it.
+ const fuzzy = buildComparator("~*", "progrms");
+ expect(fuzzy?.("view programs list")).toBe(true);
+ });
});
});
diff --git a/packages/trilium-core/src/services/search/services/build_comparator.ts b/packages/trilium-core/src/services/search/services/build_comparator.ts
index 5e872049cd..d67ee843df 100644
--- a/packages/trilium-core/src/services/search/services/build_comparator.ts
+++ b/packages/trilium-core/src/services/search/services/build_comparator.ts
@@ -88,8 +88,14 @@ const stringComparators: Record<string, Comparator<string>> = {
const normalizedVal = normalizeSearchText(val);
const normalizedCompared = normalizeSearchText(comparedValue);
-
- // For ~* operator, use fuzzy matching across the entire content
+
+ // "~*" is fuzzy CONTAINS: first try a plain substring (fragment) match, so
+ // a fragment like "progr" matches "programming" (mirrors the ~= fallback).
+ // Then fall back to fuzzy matching for typos that are not substrings.
+ if (normalizedVal.includes(normalizedCompared)) {
+ return true;
+ }
+
return fuzzyMatchWord(normalizedCompared, normalizedVal);
}
};