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): honour the autocomplete typo-tolerance option (closes #8360)
Elian Dorana4f68a5041d2
diff --git a/packages/trilium-core/src/services/search/search_context.spec.ts b/packages/trilium-core/src/services/search/search_context.spec.tsindex 00bfd60b67..6ce4fcee0c 100644--- a/packages/trilium-core/src/services/search/search_context.spec.ts+++ b/packages/trilium-core/src/services/search/search_context.spec.ts@@ -104,6 +104,19 @@ describe("SearchContext", () => {expect(ctx.enableFuzzyMatching).toBe(false);});+ it("lets a caller narrow enableFuzzyMatching but never widen it past the option", () => {+ // The option is on, so a surface that opts out still gets no fuzzy matching.+ getOptionBool.mockReturnValue(true);+ expect(new SearchContext({ enableFuzzyMatching: false }).enableFuzzyMatching).toBe(false);+ expect(new SearchContext({ enableFuzzyMatching: true }).enableFuzzyMatching).toBe(true);+ expect(new SearchContext().enableFuzzyMatching).toBe(true);++ // The option is off, so asking for fuzzy matching cannot bring it back.+ getOptionBool.mockReturnValue(false);+ expect(new SearchContext({ enableFuzzyMatching: true }).enableFuzzyMatching).toBe(false);+ expect(new SearchContext().enableFuzzyMatching).toBe(false);+ });+it("defaults enableFuzzyMatching to true when the option is not yet initialized (throws)", () => {getOptionBool.mockImplementation(() => {throw new Error("option store not initialized");diff --git a/packages/trilium-core/src/services/search/search_context.ts b/packages/trilium-core/src/services/search/search_context.tsindex 44b8ebbae0..81c45954d1 100644--- a/packages/trilium-core/src/services/search/search_context.ts+++ b/packages/trilium-core/src/services/search/search_context.ts@@ -64,11 +64,15 @@ class SearchContext {this.fuzzyAttributeSearch = !!params.fuzzyAttributeSearch;this.autocomplete = !!params.autocomplete;this.rankInTwoPasses = !!params.rankInTwoPasses;+ let fuzzyMatchingEnabled: boolean;try {- this.enableFuzzyMatching = optionService.getOptionBool("searchEnableFuzzyMatching");+ fuzzyMatchingEnabled = optionService.getOptionBool("searchEnableFuzzyMatching");} catch {- this.enableFuzzyMatching = true; // Default to true if option not yet initialized+ fuzzyMatchingEnabled = true; // Default to true if option not yet initialized}+ // A caller can only narrow the option, so a surface that opts out stays out whatever the+ // option says, and one that says nothing follows it.+ this.enableFuzzyMatching = fuzzyMatchingEnabled && (params.enableFuzzyMatching ?? true);this.highlightedTokens = [];this.regexTokens = new Set();this.originalQuery = "";diff --git a/packages/trilium-core/src/services/search/services/progressive_search.spec.ts b/packages/trilium-core/src/services/search/services/progressive_search.spec.tsindex c2e2cada87..0330e0a924 100644--- a/packages/trilium-core/src/services/search/services/progressive_search.spec.ts+++ b/packages/trilium-core/src/services/search/services/progressive_search.spec.ts@@ -1,9 +1,10 @@-import { describe, it, expect, beforeEach } from "vitest";+import { describe, it, expect, beforeEach, vi } from "vitest";import searchService from "./search.js";import BNote from "../../../becca/entities/bnote.js";import BBranch from "../../../becca/entities/bbranch.js";import SearchContext from "../search_context.js";import becca from "../../../becca/becca.js";+import optionService from "../../options.js";import { getContext } from "../../context.js";import noteService from "../../notes.js";import { findNoteByTitle, note, NoteBuilder } from "../../../test/becca_mocking.js";@@ -279,4 +280,32 @@ describe("Progressive Search Strategy", () => {expect(searchResults.some((result) => result.noteId === typo.noteId)).toBe(false);});});+ describe("Autocomplete typo tolerance option", () => {+ // `searchAutocompleteFuzzy` is the toggle behind Options -> Other -> "Typo tolerance in+ // autocomplete". Autocomplete queries on every keystroke, so it ships off.+ function searchAutocomplete(query: string, autocompleteFuzzy: boolean) {+ vi.spyOn(optionService, "getOptionBool").mockImplementation((name: string) =>+ name === "searchAutocompleteFuzzy" ? autocompleteFuzzy : true);++ try {+ return searchService.searchNotesForAutocomplete(query, true);+ } finally {+ vi.restoreAllMocks();+ }+ }++ beforeEach(() => {+ rootNote.child(note("Documnt Analysis"));+ });++ it("skips the fuzzy fallback when the option is off", () => {+ expect(searchAutocomplete("document", false)).toHaveLength(0);+ });++ it("runs the fuzzy fallback when the option is on", () => {+ const results = searchAutocomplete("document", true);++ expect(results.map((result) => result.noteTitle)).toContain("Documnt Analysis");+ });+ });});diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.tsindex ac30ff9f9d..86db7746a3 100644--- a/packages/trilium-core/src/services/search/services/search.ts+++ b/packages/trilium-core/src/services/search/services/search.ts@@ -7,6 +7,7 @@ import becca_service from "../../../becca/becca_service.js";import type BNote from "../../../becca/entities/bnote.js";import blobService from "../../blob.js";import hoistedNoteService from "../../hoisted_note.js";+import optionService from "../../options.js";import { getLog } from "../../log.js";import scriptService from "../../script.js";import { isScriptingEnabled } from "../../scripting_guard.js";@@ -776,6 +777,10 @@ function searchNotesForAutocomplete(query: string, fastSearch: boolean = true) {fuzzyAttributeSearch: true,ignoreInternalAttributes: true,ancestorNoteId: hoistedNoteService.isHoistedInHiddenSubtree() ? "root" : hoistedNoteService.getHoistedNoteId(),+ // Typo tolerance is opt-in here: `searchEnableFuzzyMatching` covers quick search and the+ // search screen, and `searchAutocompleteFuzzy` decides it for the jump-to-note and note+ // selector dropdowns, which query on every keystroke.+ enableFuzzyMatching: optionService.getOptionBool("searchAutocompleteFuzzy"),// Only the first `AUTOCOMPLETE_RESULT_LIMIT` results are ever read.rankInTwoPasses: true});diff --git a/packages/trilium-core/src/services/search/services/types.ts b/packages/trilium-core/src/services/search/services/types.tsindex dfebc50b25..4c05719f06 100644--- a/packages/trilium-core/src/services/search/services/types.ts+++ b/packages/trilium-core/src/services/search/services/types.ts@@ -23,6 +23,11 @@ export interface SearchParams {fuzzyAttributeSearch?: boolean;/** When true, skip the two-phase fuzzy fallback and use the single-token fast path. */autocomplete?: boolean;+ /**+ * Narrows the `searchEnableFuzzyMatching` option for one surface: false turns fuzzy matching+ * off for this search even when the option is on. It cannot turn it on against the option.+ */+ enableFuzzyMatching?: boolean;/*** Rank in two passes, keeping only the best results. Set by callers that show a fixed number of* results and never read the rest, so the full set is never fully ranked.