Skip to content

Build your personal knowledge base with Trilium Notes

perf(search): scan each distinct flat-text token once

Elian Doran9f11a71ede33
diff --git a/packages/trilium-core/src/services/search/expressions/note_flat_text.spec.ts b/packages/trilium-core/src/services/search/expressions/note_flat_text.spec.ts
index 61ea206c52..0edc9c8570 100644
--- a/packages/trilium-core/src/services/search/expressions/note_flat_text.spec.ts
+++ b/packages/trilium-core/src/services/search/expressions/note_flat_text.spec.ts
@@ -55,6 +55,24 @@ describe("NoteFlatTextExp", () => {
expect(exp.tokens).toEqual(["cafe", "hello"]);
});
+ it("keeps one of each token, in the order they were typed", () => {
+ // A token is matched against every note on its own, so a repeat only repeats the scan.
+ // Normalizing first means case and diacritic variants fold together too.
+ const exp = new NoteFlatTextExp(["The", "quick", "the", "THE", "Café", "cafe"]);
+ expect(exp.tokens).toEqual(["the", "quick", "cafe"]);
+ });
+
+ it("matches the same notes whether or not a token is repeated", () => {
+ const austria = note("Austria Vienna");
+ rootNote.child(austria).child(note("Germany Berlin"));
+
+ const once = execute(new NoteFlatTextExp(["austria", "vienna"]));
+ const repeated = execute(new NoteFlatTextExp(["austria", "vienna", "austria"]));
+
+ expect(noteIds(repeated.result)).toEqual([austria.note.noteId]);
+ expect(noteIds(repeated.result)).toEqual(noteIds(once.result));
+ });
+
it("matches notes by a title token and records the resolved note path", () => {
const austria = note("Austria");
const germany = note("Germany");
diff --git a/packages/trilium-core/src/services/search/expressions/note_flat_text.ts b/packages/trilium-core/src/services/search/expressions/note_flat_text.ts
index c271edd15b..2667d7f914 100644
--- a/packages/trilium-core/src/services/search/expressions/note_flat_text.ts
+++ b/packages/trilium-core/src/services/search/expressions/note_flat_text.ts
@@ -12,8 +12,10 @@ class NoteFlatTextExp extends Expression {
constructor(tokens: string[]) {
super();
- // Normalize tokens using centralized normalization function
- this.tokens = tokens.map(token => normalizeSearchText(token));
+ // Every token is matched against each note on its own, so a repeat only repeats the scan.
+ // Deduplicating after normalization also folds tokens that differ solely in case or
+ // diacritics. Order is kept, so the tokens still read as the user typed them.
+ this.tokens = [ ...new Set(tokens.map(token => normalizeSearchText(token))) ];
}
execute(inputNoteSet: NoteSet, executionContext: any, searchContext: SearchContext) {