Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): lexer support for ~= and ~* fuzzy operators (#9426)

perfectra1n59e8d3d8457e
diff --git a/packages/trilium-core/src/services/search/services/lex.spec.ts b/packages/trilium-core/src/services/search/services/lex.spec.ts
index 668522b18d..a492cbfe05 100644
--- a/packages/trilium-core/src/services/search/services/lex.spec.ts
+++ b/packages/trilium-core/src/services/search/services/lex.spec.ts
@@ -180,6 +180,29 @@ describe("Lexer expression", () => {
});
});
+describe("Lexer fuzzy operators ~= and ~*", () => {
+ it("tokenizes ~= as a single operator (with and without surrounding spaces)", () => {
+ expect(lex("#title ~= books").expressionTokens.map((t) => t.token)).toEqual(["#title", "~=", "books"]);
+ expect(lex("#title~=books").expressionTokens.map((t) => t.token)).toEqual(["#title", "~=", "books"]);
+ expect(lex("#book ~= tolkien").expressionTokens.map((t) => t.token)).toEqual(["#book", "~=", "tolkien"]);
+ });
+
+ it("tokenizes ~* as a single operator", () => {
+ expect(lex("#title ~* books").expressionTokens.map((t) => t.token)).toEqual(["#title", "~*", "books"]);
+ });
+
+ it("supports ~= on note properties and on relation properties", () => {
+ expect(lex("note.title ~= books").expressionTokens.map((t) => t.token)).toEqual(["note", ".", "title", "~=", "books"]);
+ expect(lex("~author.title ~= tolkien").expressionTokens.map((t) => t.token)).toEqual(["~author", ".", "title", "~=", "tolkien"]);
+ });
+
+ it("still treats a leading ~ as the start of a relation (regression)", () => {
+ // The first "~" of the query starts a relation and ends fulltext, so
+ // "~author.title = x" must remain a relation comparison, not a fuzzy op.
+ expect(lex("~author.title = x").expressionTokens.map((t) => t.token)).toEqual(["~author", ".", "title", "=", "x"]);
+ });
+});
+
describe("Lexer invalid queries and edge cases", () => {
it("concatenated attributes", () => {
expect(lex("#label~relation").expressionTokens.map((t) => t.token)).toEqual(["#label", "~relation"]);
diff --git a/packages/trilium-core/src/services/search/services/lex.ts b/packages/trilium-core/src/services/search/services/lex.ts
index 2fcd99ca2a..189e9f128d 100644
--- a/packages/trilium-core/src/services/search/services/lex.ts
+++ b/packages/trilium-core/src/services/search/services/lex.ts
@@ -93,6 +93,18 @@ function lex(str: string) {
fulltextEnded = true;
}
+ // Fuzzy operators ~= and ~*. The `fulltextEnded` guard keeps the first
+ // `~` of a query starting a relation (e.g. `~author.title=x`); only a
+ // `~` that appears once we're in the expression section and is followed
+ // by `=`/`*` is treated as a two-character fuzzy operator.
+ if (chr === "~" && fulltextEnded && (str[i + 1] === "=" || str[i + 1] === "*")) {
+ finishWord(i - 1);
+ currentWord = "~" + str[i + 1];
+ finishWord(i + 1);
+ i++;
+ continue;
+ }
+
if (chr === "#" || chr === "~") {
if (!fulltextEnded) {
fulltextEnded = true;
diff --git a/packages/trilium-core/src/services/search/services/parse.spec.ts b/packages/trilium-core/src/services/search/services/parse.spec.ts
index ccf4925499..a617b4a65a 100644
--- a/packages/trilium-core/src/services/search/services/parse.spec.ts
+++ b/packages/trilium-core/src/services/search/services/parse.spec.ts
@@ -262,6 +262,37 @@ describe("Parser", () => {
});
});
+describe("Fuzzy operator parsing (#9426)", () => {
+ it("parses a label fuzzy comparison (#label ~= value) without error", () => {
+ const searchContext = new SearchContext();
+ const rootExp = parse(
+ {
+ fulltextTokens: [],
+ expressionTokens: tokens(["#title", "~=", "books"]),
+ searchContext
+ },
+ AndExp
+ );
+
+ expect(searchContext.error).toBeNull();
+ const labelComparisonExp = expectExpression(rootExp.subExpressions[2], LabelComparisonExp);
+ expect(labelComparisonExp.attributeName).toEqual("title");
+ expect(labelComparisonExp.comparator).toBeTruthy();
+ });
+
+ it("parses note-property, relation-property and ~* fuzzy comparisons without error", () => {
+ for (const expressionTokens of [
+ tokens(["note", ".", "title", "~=", "books"]),
+ tokens(["~author", ".", "title", "~=", "tolkien"]),
+ tokens(["#title", "~*", "books"])
+ ]) {
+ const searchContext = new SearchContext();
+ parseInternal({ fulltextTokens: [], expressionTokens, searchContext });
+ expect(searchContext.error).toBeNull();
+ }
+ });
+});
+
describe("Invalid expressions", () => {
it("incomplete comparison", () => {
const searchContext = new SearchContext();
diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.ts
index 74d9cc2b7e..e990da2695 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -408,6 +408,25 @@ describe("Search", () => {
expect(searchResults[0].noteId).toEqual(guide.noteId);
});
+ it("fuzzy operator queries (~=) match note properties, labels and relations end-to-end (#9426)", () => {
+ const tolkienAuthor = note("Tolkien");
+ const booksNote = note("Books")
+ .label("title", "Books")
+ .label("book", "Tolkien")
+ .relation("author", tolkienAuthor.note);
+ rootNote.child(booksNote).child(tolkienAuthor);
+
+ const searchContext = new SearchContext();
+
+ // note.property ~= value
+ expect(findNoteByTitle(searchService.findResultsWithQuery("note.title ~= books", searchContext), "Books")).toBeTruthy();
+ // #label ~= value
+ expect(findNoteByTitle(searchService.findResultsWithQuery("#title ~= books", searchContext), "Books")).toBeTruthy();
+ expect(findNoteByTitle(searchService.findResultsWithQuery("#book ~= tolkien", searchContext), "Books")).toBeTruthy();
+ // ~relation.property ~= value
+ expect(findNoteByTitle(searchService.findResultsWithQuery("~author.title ~= tolkien", searchContext), "Books")).toBeTruthy();
+ });
+
it("fuzzy attribute search", () => {
rootNote.child(note("Europe")
.label("country", "", true)