Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): don't search for an AND joining fulltext to an expression

Elian Doran6dd24d096d26
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 98a59a7c3c..8c16234c85 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
@@ -243,10 +243,9 @@
To have the find bar look for a whole phrase instead, put the phrase in
quotes in the search, e.g. <code>"The Lord of the Rings"</code>. In read-only
notes, the other words of the search are highlighted as well.</li>
- <li><strong>Open all results in new tabs</strong> (the <span class="bx bx-window-open"></span> button
- above the results) opens every result in a tab of its own, up to 50. Each
- tab opens on its first match with the find bar pre-filled, as when clicking
- a result.</li>
+ <li><strong>Open all results in new tabs</strong> (the button above the results)
+ opens every result in a tab of its own, up to 50. Each tab opens on its
+ first match with the find bar pre-filled, as when clicking a result.</li>
<li>If a match is inside a <strong>collapsed section</strong> (for example a
folded heading), that section is expanded automatically so the match is
visible.</li>
@@ -260,7 +259,8 @@
<li><code>note.content *=* rings OR note.content *=* tolkien</code>: Find
notes containing "rings" or "tolkien" in their content.</li>
<li><code>towers #book</code>: Combine full-text and attribute search to find
- notes containing "towers" and having the "book" label.</li>
+ notes containing "towers" and having the "book" label. Writing <code>towers AND #book</code> does
+ the same; to search for the word "and" itself, put it in quotes.</li>
<li><code>c#</code> or <code>towers#book</code>: A <code>#</code> or <code>~</code> inside
a word is part of the text, so these find notes containing "c#" or "towers#book".
To start a label or relation, put a space before it.</li>
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 7768b75409..ed55ff89ae 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
@@ -139,7 +139,7 @@ In addition: 
* `rings tolkien`: Full-text search to find notes containing both "rings" and "tolkien".
* `"The Lord of the Rings" Tolkien`: Full-text search where "The Lord of the Rings" must match exactly.
* `note.content *=* rings OR note.content *=* tolkien`: Find notes containing "rings" or "tolkien" in their content.
-* `towers #book`: Combine full-text and attribute search to find notes containing "towers" and having the "book" label.
+* `towers #book`: Combine full-text and attribute search to find notes containing "towers" and having the "book" label. Writing `towers AND #book` does the same; to search for the word "and" itself, put it in quotes.
* `c#` or `towers#book`: A `#` or `~` inside a word is part of the text, so these find notes containing "c#" or "towers#book". To start a label or relation, put a space before it.
* `towers #book or #author`: Search for notes containing "towers" and having either the "book" or "author" label.
* `towers #!book`: Search for notes containing "towers" and not having the "book" label.
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 5b4f024926..6851ab09be 100644
--- a/packages/trilium-core/src/services/search/services/lex.spec.ts
+++ b/packages/trilium-core/src/services/search/services/lex.spec.ts
@@ -184,6 +184,18 @@ describe("Lexer expression", () => {
expect(lex(`hello fulltext note.labels.capital = Prague`).expressionTokens.map((t) => t.token)).toEqual(["note", ".", "labels", ".", "capital", "=", "prague"]);
});
+ it("an AND joining the fulltext to the expression is not a fulltext word", () => {
+ const lexed = lex(`"Trilium" AND note.noteId != abc`);
+ expect(lexed.fulltextTokens.map((t) => t.token)).toEqual(["trilium"]);
+ expect(lexed.fulltextQuery).toBe(lex(`"Trilium"`).fulltextQuery);
+ expect(lexed.expressionTokens.map((t) => t.token)).toEqual(["note", ".", "noteid", "!=", "abc"]);
+
+ expect(lex(`rings and #book`).fulltextTokens.map((t) => t.token)).toEqual(["rings"]);
+ // Quoted, or not followed by an expression, "and" is searched for.
+ expect(lex(`rings "and" #book`).fulltextTokens.map((t) => t.token)).toEqual(["rings", "and"]);
+ expect(lex(`rock and roll`).fulltextTokens.map((t) => t.token)).toEqual(["rock", "and", "roll"]);
+ });
+
it("note. prefix in quotes will note start expression", () => {
expect(lex(`hello fulltext "note.txt"`).expressionTokens.map((t) => t.token)).toEqual([]);
diff --git a/packages/trilium-core/src/services/search/services/lex.ts b/packages/trilium-core/src/services/search/services/lex.ts
index 31a8fd5375..d48d10f115 100644
--- a/packages/trilium-core/src/services/search/services/lex.ts
+++ b/packages/trilium-core/src/services/search/services/lex.ts
@@ -9,6 +9,7 @@ function lex(str: string) {
str = str.toLowerCase();
let fulltextQuery = "";
+ let fulltextQueryBeforeLastToken = "";
const fulltextTokens: TokenData[] = [];
const expressionTokens: TokenData[] = [];
@@ -53,6 +54,7 @@ function lex(str: string) {
} else {
fulltextTokens.push(rec);
+ fulltextQueryBeforeLastToken = fulltextQuery;
fulltextQuery = str.substr(0, endIndex + 1);
}
@@ -155,6 +157,14 @@ function lex(str: string) {
finishWord(str.length - 1);
+ // `parse()` always joins the fulltext to the expression with AND, so an unquoted "and" between
+ // the two is that join, not a word to search for.
+ const lastFulltextToken = fulltextTokens.at(-1);
+ if (expressionTokens.length > 0 && lastFulltextToken?.token === "and" && !lastFulltextToken.inQuotes) {
+ fulltextTokens.pop();
+ fulltextQuery = fulltextQueryBeforeLastToken;
+ }
+
// Scoring compares the whole fulltext query against note titles, so the layout the query
// was typed in must not reach it.
fulltextQuery = fulltextQuery.replace(/\s+/g, " ").trim();