Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/server/src/services/search/services/lex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ describe("Lexer expression", () => {
expect(lex(`#!capital ~!neighbor`).expressionTokens.map((t) => t.token)).toEqual(["#!capital", "~!neighbor"]);
});

it("fuzzy operators ~= and ~* are tokenized as single operators", () => {
// regression: https://github.com/TriliumNext/Trilium/issues/9426
expect(lex(`note.title ~= books`).expressionTokens.map((t) => t.token)).toEqual(["note", ".", "title", "~=", "books"]);
expect(lex(`note.title ~* books`).expressionTokens.map((t) => t.token)).toEqual(["note", ".", "title", "~*", "books"]);
expect(lex(`#author ~= tolkien`).expressionTokens.map((t) => t.token)).toEqual(["#author", "~=", "tolkien"]);
expect(lex(`#author ~*'lord of the rings'`).expressionTokens.map((t) => t.token)).toEqual(["#author", "~*", "lord of the rings"]);
});

it("relation prefix still works when ~ is not followed by = or *", () => {
expect(lex(`~author.title = Tolkien`).expressionTokens.map((t) => t.token)).toEqual(["~author", ".", "title", "=", "tolkien"]);
});

it("negation of sub-expression", () => {
expect(lex(`# not(#capital) and note.noteId != "root"`).expressionTokens.map((t) => t.token)).toEqual(["#", "not", "(", "#capital", ")", "and", "note", ".", "noteid", "!=", "root"]);
});
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/services/search/services/lex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ function lex(str: string) {
} else if (["#", "~"].includes(currentWord) && chr === "!") {
currentWord += chr;
continue;
} else if (currentWord === "~" && (chr === "=" || chr === "*")) {
// ~= and ~* are fuzzy-match operators, not a relation prefix followed by an operator
currentWord += chr;
continue;
} else if (chr === " ") {
finishWord(i - 1);
continue;
Expand Down
Loading