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): stop re-normalizing note titles on every match
Elian Doran44c6c5e09e35
diff --git a/packages/trilium-core/src/becca/becca_service.ts b/packages/trilium-core/src/becca/becca_service.tsindex 8dbf70a722..cd1a6b9649 100644--- a/packages/trilium-core/src/becca/becca_service.ts+++ b/packages/trilium-core/src/becca/becca_service.ts@@ -111,6 +111,7 @@ function getNoteTitleForPath(notePathArray: string[]) {export default {getNoteTitle,+ getNoteTitleArrayForPath,getNoteTitleAndIcon,getNoteTitleForPath,isNotePathArchiveddiff --git a/packages/trilium-core/src/becca/entities/bnote.ts b/packages/trilium-core/src/becca/entities/bnote.tsindex 04b721935f..e635143747 100644--- a/packages/trilium-core/src/becca/entities/bnote.ts+++ b/packages/trilium-core/src/becca/entities/bnote.ts@@ -10,6 +10,7 @@ import noteService from "../../services/notes.js";import optionService from "../../services/options.js";import protectedSessionService from "../../services/protected_session.js";import searchService from "../../services/search/services/search.js";+import { normalizeSearchText, tokenizeNormalizedText } from "../../services/search/utils/text_utils.js";import TaskContext from "../../services/task_context.js";import type { NotePojo } from "../becca-interface.js";import AbstractBeccaEntity from "./abstract_becca_entity.js";@@ -48,6 +49,12 @@ interface ConvertOpts {autoConversion?: boolean;}+/** A note's title in the two forms scoring needs, cached together since both derive from it. */+interface SearchableTitle {+ normalized: string;+ words: string[];+}+/*** Trilium's main entity, which can represent text note, image, code note, file attachment etc.*/@@ -77,6 +84,7 @@ class BNote extends AbstractBeccaEntity<BNote> {targetRelations!: BAttribute[];__flatTextCache!: string | null;+ __searchableTitleCache!: SearchableTitle | null;private __attributeCache!: BAttribute[] | null;private __inheritableAttributeCache!: BAttribute[] | null;@@ -129,6 +137,7 @@ class BNote extends AbstractBeccaEntity<BNote> {this.decrypt();this.__flatTextCache = null;+ this.__searchableTitleCache = null;return this;}@@ -803,8 +812,22 @@ class BNote extends AbstractBeccaEntity<BNote> {return this.__flatTextCache as string;}+ /**+ * The title normalized for search, plus its punctuation-stripped words. Scoring reads both for+ * every match it ranks, so they are derived once per title rather than once per result.+ */+ getSearchableTitle(): SearchableTitle {+ if (!this.__searchableTitleCache) {+ const normalized = normalizeSearchText(this.title);+ this.__searchableTitleCache = { normalized, words: tokenizeNormalizedText(normalized) };+ }++ return this.__searchableTitleCache;+ }+invalidateThisCache() {this.__flatTextCache = null;+ this.__searchableTitleCache = null;this.__attributeCache = null;this.__inheritableAttributeCache = null;@@ -1519,6 +1542,7 @@ class BNote extends AbstractBeccaEntity<BNote> {try {this.title = protectedSessionService.decryptString(this.title) || "";this.__flatTextCache = null;+ this.__searchableTitleCache = null;// The pre-built flat text search index still holds this note's encrypted// title, so schedule a refresh — otherwise the note stays unsearchable by// title even after the protected session is unlocked (issue #10406).diff --git a/packages/trilium-core/src/services/search/search_result.ts b/packages/trilium-core/src/services/search/search_result.tsindex 374a4d054d..212f0b6d7f 100644--- a/packages/trilium-core/src/services/search/search_result.ts+++ b/packages/trilium-core/src/services/search/search_result.ts@@ -8,6 +8,7 @@ import {normalizeSearchText,stripWordPunctuation,tokenizeIntoWords,+ tokenizeNormalizedText,wordsContainPhrase} from "./utils/text_utils.js";// Scoring constants for better maintainability@@ -48,13 +49,23 @@ const SCORE_WEIGHTS = {*/export interface ScoringTerms {normalizedQuery: string;+ queryWords: string[];normalizedTokens: string[];+ /**+ * Words per path segment title. Results under the same ancestors repeat those titles, so one+ * search tokenizes each distinct segment once instead of once per result.+ */+ pathSegmentWords: Map<string, string[]>;}export function precomputeScoringTerms(fulltextQuery: string, tokens: string[]): ScoringTerms {+ const normalizedQuery = normalizeSearchText(fulltextQuery);+return {- normalizedQuery: normalizeSearchText(fulltextQuery),- normalizedTokens: tokens.map((token) => stripWordPunctuation(normalizeSearchText(token)))+ normalizedQuery,+ queryWords: tokenizeNormalizedText(normalizedQuery),+ normalizedTokens: tokens.map((token) => stripWordPunctuation(normalizeSearchText(token))),+ pathSegmentWords: new Map()};}@@ -62,6 +73,8 @@ class SearchResult {notePathArray: string[];score: number;notePathTitle: string;+ /** The per-segment titles `notePathTitle` joins, kept so scoring can tokenize them separately. */+ private pathTitleSegments: string[];highlightedNotePathTitle?: string;contentSnippet?: string;highlightedContentSnippet?: string;@@ -71,7 +84,8 @@ class SearchResult {constructor(notePathArray: string[]) {this.notePathArray = notePathArray;- this.notePathTitle = becca_service.getNoteTitleForPath(notePathArray);+ this.pathTitleSegments = becca_service.getNoteTitleArrayForPath(notePathArray);+ this.notePathTitle = this.pathTitleSegments.join(" › ");this.score = 0;this.fuzzyScore = 0;}@@ -89,9 +103,9 @@ class SearchResult {this.fuzzyScore = 0; // Reset fuzzy score trackingconst note = becca.notes[this.noteId];- const { normalizedQuery, normalizedTokens } = terms ?? precomputeScoringTerms(fulltextQuery, tokens);+ const { normalizedQuery, queryWords, normalizedTokens, pathSegmentWords } = terms ?? precomputeScoringTerms(fulltextQuery, tokens);// normalizeSearchText already lowercases — no need for .toLowerCase() first- const normalizedTitle = normalizeSearchText(note.title);+ const { normalized: normalizedTitle, words: titleWords } = note.getSearchableTitle();// Note ID exact match, much higher scoreif (note.noteId.toLowerCase() === fulltextQuery) {@@ -103,7 +117,7 @@ class SearchResult {this.score += SCORE_WEIGHTS.TITLE_EXACT_MATCH;} else if (normalizedTitle.startsWith(normalizedQuery)) {this.score += SCORE_WEIGHTS.TITLE_PREFIX_MATCH;- } else if (this.isWordMatch(normalizedTitle, normalizedQuery)) {+ } else if (wordsContainPhrase(titleWords, queryWords)) {this.score += SCORE_WEIGHTS.TITLE_WORD_MATCH;} else if (enableFuzzyMatching) {// Try fuzzy matching for typos only if enabled@@ -113,8 +127,8 @@ class SearchResult {}// Add scores for token matches- this.addScoreForStrings(tokens, note.title, SCORE_WEIGHTS.TITLE_FACTOR, enableFuzzyMatching, normalizedTokens);- this.addScoreForStrings(tokens, this.notePathTitle, SCORE_WEIGHTS.PATH_FACTOR, enableFuzzyMatching, normalizedTokens);+ this.addScoreForChunks(titleWords, tokens, SCORE_WEIGHTS.TITLE_FACTOR, enableFuzzyMatching, normalizedTokens);+ this.addScoreForChunks(this.getPathWords(pathSegmentWords), tokens, SCORE_WEIGHTS.PATH_FACTOR, enableFuzzyMatching, normalizedTokens);// Add score for how well the note's body content matched the query.if (contentMatch) {@@ -175,9 +189,34 @@ class SearchResult {// Tokenize (strip boundary punctuation) so a chunk like "(sync)" scores as// an exact token match for "sync" rather than only a contains match.const chunks = tokenizeIntoWords(str);-const normalizedTokens = precomputedTokens ?? tokens.map(t => stripWordPunctuation(normalizeSearchText(t)));+ this.addScoreForChunks(chunks, tokens, factor, enableFuzzyMatching, normalizedTokens);+ }++ /**+ * The words of `notePathTitle`. Splitting on whitespace drops the " › " separators, so tokenizing+ * each segment and concatenating yields exactly what tokenizing the joined title would.+ */+ private getPathWords(cache: Map<string, string[]>): string[] {+ const words: string[] = [];++ for (const segment of this.pathTitleSegments) {+ let segmentWords = cache.get(segment);++ if (!segmentWords) {+ segmentWords = tokenizeIntoWords(segment);+ cache.set(segment, segmentWords);+ }++ words.push(...segmentWords);+ }++ return words;+ }++ /** Scores already-tokenized text, so a caller holding cached words skips re-tokenizing them. */+ private addScoreForChunks(chunks: string[], tokens: string[], factor: number, enableFuzzyMatching: boolean, normalizedTokens: string[]) {let tokenScore = 0;for (const chunk of chunks) {for (let ti = 0; ti < normalizedTokens.length; ti++) {@@ -222,10 +261,6 @@ class SearchResult {* in the text. Tokenizing both sides makes this punctuation-aware, so a title* like "(sync) notes" is a word match for the query "sync".*/- private isWordMatch(text: string, query: string): boolean {- return wordsContainPhrase(tokenizeIntoWords(text), tokenizeIntoWords(query));- }-/*** Calculates fuzzy matching score for title matches with caps applied*/diff --git a/packages/trilium-core/src/services/search/utils/text_utils.ts b/packages/trilium-core/src/services/search/utils/text_utils.tsindex b137ea66bc..0314ea5d51 100644--- a/packages/trilium-core/src/services/search/utils/text_utils.ts+++ b/packages/trilium-core/src/services/search/utils/text_utils.ts@@ -398,7 +398,12 @@ export function stripWordPunctuation(word: string): string {* tokenize through this, so punctuation in the content cannot prevent a word from matching.*/export function tokenizeIntoWords(text: string): string[] {- return normalizeSearchText(text)+ return tokenizeNormalizedText(normalizeSearchText(text));+}++/** Splits already-normalized text into the same words {@link tokenizeIntoWords} would produce. */+export function tokenizeNormalizedText(normalized: string): string[] {+ return normalized.split(/\s+/).map(stripWordPunctuation).filter((word) => word.length > 0);