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): restore strict equality for attribute = operator
perfectra1n9b6b8e0016b4
diff --git a/apps/server/spec/etapi/search.spec.ts b/apps/server/spec/etapi/search.spec.tsindex 72beed25a5..fbfd47e1b3 100644--- a/apps/server/spec/etapi/search.spec.ts+++ b/apps/server/spec/etapi/search.spec.ts@@ -39,6 +39,23 @@ describe("etapi/search", () => {expect(response.body.results).toHaveLength(0);});+ it("attribute = uses strict equality, not word/phrase matching", async () => {+ // Regression for #9422: "#capital=Vienna" must match the note whose label+ // value equals "Vienna" exactly, and must NOT match one whose value merely+ // contains the word "Vienna" (e.g. "Vienna Austria").+ const exactNoteId = await createNoteWithLabel(app, token, "capital", "Vienna");+ await createNoteWithLabel(app, token, "capital", "Vienna Austria");++ const query = encodeURIComponent("#capital=Vienna");+ const response = await supertest(app)+ .get(`/etapi/notes?search=${query}`)+ .auth(USER, token, { "type": "basic"})+ .expect(200);++ const resultIds = response.body.results.map((r: { noteId: string }) => r.noteId);+ expect(resultIds).toStrictEqual([exactNoteId]);+ });+describe("query parameter handling", () => {afterEach(() => {vi.restoreAllMocks();@@ -95,3 +112,16 @@ describe("etapi/search", () => {});});});++async function createNoteWithLabel(app: Application, token: string, name: string, value: string) {+ const noteId = await createNote(app, token, randomUUID());+ const attributeId = `label${randomUUID().replace(/-/g, "")}`.substring(0, 32);++ await supertest(app)+ .post("/etapi/attributes")+ .auth(USER, token, { "type": "basic" })+ .send({ attributeId, noteId, type: "label", name, value })+ .expect(201);++ return noteId;+}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.tsindex 31ca2dd010..896a46e7d9 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@@ -48,34 +48,61 @@ describe("buildComparator", () => {});describe("equality operators (= and !=)", () => {- it("matches a single word case-insensitively against any word in the value", () => {- const eq = buildComparator("=", "Hello")!;- expect(eq("hello world")).toBe(true);- expect(eq("say HELLO there")).toBe(true);- expect(eq("hellothere")).toBe(false); // not an exact word- expect(eq("")).toBe(false);+ it("matches only the exact full value, case-insensitively (strict equality)", () => {+ const eq = buildComparator("=", "Vienna");+ expect(eq?.("vienna")).toBe(true);+ expect(eq?.("VIENNA")).toBe(true);+ // No longer a word/substring match: the value must equal the whole thing.+ expect(eq?.("Vienna Austria")).toBe(false);+ expect(eq?.("hello vienna world")).toBe(false);+ expect(eq?.("")).toBe(false);});- it("matches a multi-word phrase as a consecutive substring", () => {- const eq = buildComparator("=", "hello world")!;- expect(eq("well hello world friend")).toBe(true);- expect(eq("world hello")).toBe(false);+ it("matches a multi-word compared value only against the whole value", () => {+ const eq = buildComparator("=", "Vienna Austria");+ expect(eq?.("vienna austria")).toBe(true);+ expect(eq?.("well vienna austria friend")).toBe(false);+ expect(eq?.("austria vienna")).toBe(false);});it("normalizes diacritics on both sides", () => {- const eq = buildComparator("=", "café")!;- expect(eq("the cafe is open")).toBe(true);+ const eq = buildComparator("=", "café");+ expect(eq?.("cafe")).toBe(true);+ expect(eq?.("the cafe is open")).toBe(false);});- it("negates the equality match and treats empty value as a match", () => {- const neq = buildComparator("!=", "hello")!;- expect(neq("hello world")).toBe(false);- expect(neq("goodbye world")).toBe(true);- expect(neq("")).toBe(true);+ it("negates the strict equality match and treats empty value as a match", () => {+ const neq = buildComparator("!=", "vienna");+ expect(neq?.("vienna")).toBe(false);+ expect(neq?.("Vienna Austria")).toBe(true);+ expect(neq?.("goodbye world")).toBe(true);+ expect(neq?.("")).toBe(true);+ });+ });++ describe("internal word= operator (title fulltext word/phrase match)", () => {+ it("matches a single word case-insensitively against any word in the value", () => {+ const eq = buildComparator("word=", "Hello");+ expect(eq?.("hello world")).toBe(true);+ expect(eq?.("say HELLO there")).toBe(true);+ expect(eq?.("hellothere")).toBe(false); // not an exact word+ expect(eq?.("")).toBe(false);+ });- const neqPhrase = buildComparator("!=", "hello world")!;- expect(neqPhrase("well hello world")).toBe(false);- expect(neqPhrase("world hello")).toBe(true);+ it("matches a multi-word phrase as a consecutive substring", () => {+ const eq = buildComparator("word=", "hello world");+ expect(eq?.("well hello world friend")).toBe(true);+ expect(eq?.("world hello")).toBe(false);+ });++ it("matches a punctuation-wrapped word against the bare token", () => {+ const eq = buildComparator("word=", "books");+ expect(eq?.("(Books)")).toBe(true);+ });++ it("normalizes diacritics on both sides", () => {+ const eq = buildComparator("word=", "café");+ expect(eq?.("the cafe is open")).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.tsindex d67ee843df..a3f572ac3f 100644--- a/packages/trilium-core/src/services/search/services/build_comparator.ts+++ b/packages/trilium-core/src/services/search/services/build_comparator.ts@@ -13,9 +13,15 @@ function getRegex(str: string) {type Comparator<T> = (comparedValue: T) => (val: string) => boolean;const stringComparators: Record<string, Comparator<string>> = {- "=": (comparedValue) => (val) => {- // For the = operator, check if the value contains the exact word or phrase- // This is case-insensitive+ // Strict normalized full-value equality: the whole value must equal the whole+ // compared value (case- and diacritic-insensitive). This is the documented+ // label-equality semantics — e.g. #capital=Vienna matches "vienna" but NOT+ // "Vienna Austria". Word/phrase matching lives in the internal "word=" operator.+ "=": (comparedValue) => (val) => normalizeSearchText(val) === normalizeSearchText(comparedValue),+ "!=": (comparedValue) => (val) => normalizeSearchText(val) !== normalizeSearchText(comparedValue),+ // Internal operator (not user-typable): word/phrase match used by the leading-"="+ // fulltext title comparison. Punctuation-aware via Task 1's helpers.+ "word=": (comparedValue) => (val) => {if (!val) return false;const normalizedVal = normalizeSearchText(val);@@ -32,24 +38,6 @@ const stringComparators: Record<string, Comparator<string>> = {const words = tokenizeIntoWords(normalizedVal);return words.some(word => word === stripWordPunctuation(normalizedCompared));},- "!=": (comparedValue) => (val) => {- // Negation of exact word/phrase match- if (!val) return true;-- const normalizedVal = normalizeSearchText(val);- const normalizedCompared = normalizeSearchText(comparedValue);-- // If comparedValue has spaces, it's a multi-word phrase- // Check for substring match (consecutive phrase) and negate- if (normalizedCompared.includes(" ")) {- return !normalizedVal.includes(normalizedCompared);- }-- // For single word, tokenize into punctuation-stripped words and check for- // an exact word match, then negate.- const words = tokenizeIntoWords(normalizedVal);- return !words.some(word => word === stripWordPunctuation(normalizedCompared));- },">": (comparedValue) => (val) => val > comparedValue,">=": (comparedValue) => (val) => val >= comparedValue,"<": (comparedValue) => (val) => val < comparedValue,diff --git a/packages/trilium-core/src/services/search/services/parse.ts b/packages/trilium-core/src/services/search/services/parse.tsindex 7a379dd0c8..b579317540 100644--- a/packages/trilium-core/src/services/search/services/parse.ts+++ b/packages/trilium-core/src/services/search/services/parse.ts@@ -44,7 +44,10 @@ function getFulltext(_tokens: TokenData[], searchContext: SearchContext, leading// For multi-word, join tokens with space to form exact phraseconst titleSearchValue = tokens.join(" ");const exactMatchExpressions: Expression[] = [- new PropertyComparisonExp(searchContext, "title", "=", titleSearchValue),+ // "word=" (not the strict "=") keeps the leading-"=" fulltext title+ // match at word/phrase granularity, matching the content expressions+ // below and preserving historical user-visible behavior.+ new PropertyComparisonExp(searchContext, "title", "word=", titleSearchValue),new NoteContentFulltextExp("=", { tokens, flatText: false }),new NoteContentFulltextExp("=", { tokens, flatText: true })];