Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): let a parenthesis open the expression part of a query

Elian Doran61a853663e7e
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
index 2acc17a8e6..a4989e1282 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
@@ -325,9 +325,11 @@
</ul>
</li>
<li><code>~author.title *= Tolkien OR (#publicationDate &gt;= 1954 AND #publicationDate &lt;= 1960)</code>:
- Use boolean expressions and parentheses to group expressions. Note that
- expressions starting with a parenthesis need an "expression separator sign"
- (# or ~) prepended.</li>
+ Use boolean expressions and parentheses to group expressions.</li>
+ <li><code>towers (#genre = fantasy OR #genre = scifi)</code>: A group can
+ open the expression part directly, without a <code>#</code> or <code>~</code> before
+ the parenthesis. Here it finds notes containing "towers" whose genre is
+ fantasy or scifi.</li>
<li><code>note.parents.title = 'Books'</code>: Find notes with a parent named
"Books".</li>
<li><code>note.parents.parents.title = 'Books'</code>: Find notes with a grandparent
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
index 021fec54e8..6c8eb6276f 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
@@ -166,7 +166,8 @@ In addition: 
* J. R. R. Tolkien
* relation: “son” points to "Christopher Tolkien" note
* Christopher Tolkien
-* `~author.title *= Tolkien OR (#publicationDate >= 1954 AND #publicationDate <= 1960)`: Use boolean expressions and parentheses to group expressions. Note that expressions starting with a parenthesis need an "expression separator sign" (# or ~) prepended.
+* `~author.title *= Tolkien OR (#publicationDate >= 1954 AND #publicationDate <= 1960)`: Use boolean expressions and parentheses to group expressions.
+* `towers (#genre = fantasy OR #genre = scifi)`: A group can open the expression part directly, without a `#` or `~` before the parenthesis. Here it finds notes containing "towers" whose genre is fantasy or scifi.
* `note.parents.title = 'Books'`: Find notes with a parent named "Books".
* `note.parents.parents.title = 'Books'`: Find notes with a grandparent named "Books".
* `note.ancestors.title = 'Books'`: Find notes with an ancestor named "Books".
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 42e6ec0e5f..5b4f024926 100644
--- a/packages/trilium-core/src/services/search/services/lex.spec.ts
+++ b/packages/trilium-core/src/services/search/services/lex.spec.ts
@@ -63,6 +63,29 @@ describe("Lexer fulltext", () => {
expect(lex("# abc+=-def**-+d").expressionTokens.map((t) => t.token)).toEqual(["#", "abc", "+=-", "def", "**-+", "d"]);
});
+ it("a parenthesis that opens a word starting an expression opens the expression part", () => {
+ const tokens = (query: string) => lex(query).expressionTokens.map((t) => t.token);
+
+ expect(tokens("(#a)")).toEqual([ "(", "#a", ")" ]);
+ expect(tokens("((#a))")).toEqual([ "(", "(", "#a", ")", ")" ]);
+ expect(tokens("( #a)")).toEqual([ "(", "#a", ")" ]);
+ expect(tokens("(~rel)")).toEqual([ "(", "~rel", ")" ]);
+ expect(tokens("(#a) and (#b)")).toEqual([ "(", "#a", ")", "and", "(", "#b", ")" ]);
+ expect(tokens("(note.title=x)")).toEqual([ "(", "note", ".", "title", "=", "x", ")" ]);
+
+ const afterText = lex("towers (#a)");
+ expect(afterText.fulltextTokens.map((t) => t.token)).toEqual([ "towers" ]);
+ expect(afterText.expressionTokens.map((t) => t.token)).toEqual([ "(", "#a", ")" ]);
+
+ // Brackets around plain words, and escaped ones, stay full text.
+ const fulltext = (query: string) => lex(query).fulltextTokens.map((t) => t.token);
+ expect(fulltext("(hello world)")).toEqual([ "(hello", "world)" ]);
+ expect(fulltext("(notebook)")).toEqual([ "(notebook)" ]);
+ expect(fulltext("foo(#a)")).toEqual([ "foo(#a)" ]);
+ expect(fulltext("\\(#a")).toEqual([ "(#a" ]);
+ expect(lex("\\(#a").expressionTokens).toEqual([]);
+ });
+
it("escaping special characters", () => {
expect(lex("hello \\#\\~\\'").fulltextTokens.map((t) => t.token)).toEqual(["hello", "#~'"]);
});
diff --git a/packages/trilium-core/src/services/search/services/lex.ts b/packages/trilium-core/src/services/search/services/lex.ts
index fdef27536a..31a8fd5375 100644
--- a/packages/trilium-core/src/services/search/services/lex.ts
+++ b/packages/trilium-core/src/services/search/services/lex.ts
@@ -2,6 +2,8 @@ import type { TokenData } from "./types.js";
/** Separates tokens outside quotes, so a query can be laid out over several lines. */
const WHITESPACE = /\s/;
+/** Opening brackets followed by an attribute or `note.`, which make the brackets grouping. */
+const GROUP_START = /^\(+\s*(#|~|note\.)/;
function lex(str: string) {
str = str.toLowerCase();
@@ -96,10 +98,16 @@ function lex(str: string) {
fulltextEnded = true;
}
+ // A "(" that opens a word starting with an attribute or `note.` opens the expression
+ // part, so "(#a OR #b)" groups without a leading "#".
+ if (!fulltextEnded && currentWord === "" && chr === "(" && GROUP_START.test(str.substring(i))) {
+ fulltextEnded = true;
+ }
+
if (chr === "#" || chr === "~") {
// In the full-text part, # and ~ start an attribute only at the start of a word,
- // so "c#" and "towers#book" are searched as written. A leading "(" is grouping.
- if (!fulltextEnded && currentWord !== "" && !/^\(+$/.test(currentWord)) {
+ // so "c#" and "towers#book" are searched as written.
+ if (!fulltextEnded && currentWord !== "") {
currentWord += chr;
continue;
}
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 1fcac3643c..580b12beb7 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -38,6 +38,25 @@ describe("Search", () => {
expect(findNoteByTitle(searchResults, "Austria")).toBeTruthy();
});
+ it("runs a query whose expression part opens with a parenthesis", () => {
+ rootNote
+ .child(note("Alpha").label("a"))
+ .child(note("Beta").label("b"))
+ .child(note("Gamma"));
+
+ const titles = (query: string) => {
+ const searchContext = new SearchContext();
+ const results = searchService.findResultsWithQuery(query, searchContext);
+ expect(searchContext.error).toBeNull();
+ return results.map((r) => becca.notes[r.noteId].title).sort();
+ };
+
+ expect(titles("(#a OR #b)")).toEqual([ "Alpha", "Beta" ]);
+ expect(titles("(note.title = 'Alpha')")).toEqual([ "Alpha" ]);
+ expect(titles("(#a) OR (#b)")).toEqual([ "Alpha", "Beta" ]);
+ expect(titles("alpha (#a OR #b)")).toEqual([ "Alpha" ]);
+ });
+
it("normal search looks also at attributes", () => {
const austria = note("Austria");
const vienna = note("Vienna");