Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): read # and ~ inside a full-text word as text

Elian Doran5d61602db40b
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 b2b7b951d6..2acc17a8e6 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
@@ -270,6 +270,9 @@
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>
+ <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>
<li><code>towers #book or #author</code>: Search for notes containing "towers"
and having either the "book" or "author" label.</li>
<li><code>towers #!book</code>: Search for notes containing "towers" and not
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 daedcaaee2..021fec54e8 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
@@ -144,6 +144,7 @@ In addition: 
* `"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.
+* `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.
* `#book #publicationYear = 1954`: Find notes with the "book" label and "publicationYear" set to 1954.
diff --git a/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts b/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts
index 087ba9ff3b..509d45b051 100644
--- a/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts
+++ b/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts
@@ -77,6 +77,13 @@ describe("tokenizeSearchQuery", () => {
expect(tokenize("note.")).toEqual([ "property:note", "property:." ]);
});
+ it("reads # and ~ inside a full-text word as text, as lex() does", () => {
+ expect(tokenize("learn c# towers#book a~b")).toEqual([]);
+ expect(tokenize("c# #book")).toEqual([ "label:#book" ]);
+ expect(tokenize("(#a)")).toEqual([ "bracket:(", "label:#a", "bracket:)" ]);
+ expect(tokenize("#a and b#c")).toEqual([ "label:#a", "keyword:and", "label:#c" ]);
+ });
+
it("runs an unclosed quote to the end of the query", () => {
expect(tokenize("#title = \"unfinished")).toEqual([
"label:#title",
diff --git a/packages/codemirror/src/extensions/trilium_search_highlighter.ts b/packages/codemirror/src/extensions/trilium_search_highlighter.ts
index 36cf422693..c4c71fbe72 100644
--- a/packages/codemirror/src/extensions/trilium_search_highlighter.ts
+++ b/packages/codemirror/src/extensions/trilium_search_highlighter.ts
@@ -64,6 +64,8 @@ export function tokenizeSearchQuery(text: string): SearchToken[] {
let pos = 0;
// Set by `note` and by a relation, both of which a dotted property path can follow.
let inPath = false;
+ // Set by the first attribute or `note.`, which end the full-text part as they do in `lex()`.
+ let fulltextEnded = false;
const match = (pattern: RegExp) => {
pattern.lastIndex = pos;
@@ -100,16 +102,24 @@ export function tokenizeSearchQuery(text: string): SearchToken[] {
}
const attribute = match(ATTRIBUTE);
+ if (attribute && !fulltextEnded && !/^\(*$/.test(wordBefore(text, pos))) {
+ // Inside a full-text word, `#` and `~` are text: `c#`, `towers#book`.
+ pos++;
+ continue;
+ }
+
if (attribute) {
const isLabel = attribute[1] === "#";
take(isLabel ? "label" : "relation", ATTRIBUTE);
inPath = !isLabel;
+ fulltextEnded = true;
continue;
}
if (match(NOTE_PREFIX)) {
take("property", NOTE_PREFIX);
inPath = true;
+ fulltextEnded = true;
continue;
}
@@ -148,6 +158,11 @@ export function tokenizeSearchQuery(text: string): SearchToken[] {
return tokens;
}
+/** The non-whitespace run that ends at `pos`. */
+function wordBefore(text: string, pos: number) {
+ return /\S*$/.exec(text.slice(0, pos))?.[0] ?? "";
+}
+
const MARKS: Record<SearchTokenKind, Decoration> = {
label: Decoration.mark({ class: "cm-search-label" }),
relation: Decoration.mark({ class: "cm-search-relation" }),
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 a033299b9e..42e6ec0e5f 100644
--- a/packages/trilium-core/src/services/search/services/lex.spec.ts
+++ b/packages/trilium-core/src/services/search/services/lex.spec.ts
@@ -67,6 +67,19 @@ describe("Lexer fulltext", () => {
expect(lex("hello \\#\\~\\'").fulltextTokens.map((t) => t.token)).toEqual(["hello", "#~'"]);
});
+ it("# and ~ inside a word are literal characters", () => {
+ const towers = lex("towers#book");
+ expect(towers.fulltextTokens.map((t) => t.token)).toEqual(["towers#book"]);
+ expect(towers.expressionTokens).toEqual([]);
+
+ expect(lex("learn c# and f#").fulltextTokens.map((t) => t.token)).toEqual(["learn", "c#", "and", "f#"]);
+ expect(lex("issue#42 a~b").fulltextTokens.map((t) => t.token)).toEqual(["issue#42", "a~b"]);
+
+ const spaced = lex("towers #book");
+ expect(spaced.fulltextTokens.map((t) => t.token)).toEqual(["towers"]);
+ expect(spaced.expressionTokens.map((t) => t.token)).toEqual(["#book"]);
+ });
+
it("recognizes leading = operator for exact match", () => {
const result1 = lex("=example");
expect(result1.fulltextTokens.map((t) => t.token)).toEqual(["example"]);
diff --git a/packages/trilium-core/src/services/search/services/lex.ts b/packages/trilium-core/src/services/search/services/lex.ts
index 9be267dc79..fdef27536a 100644
--- a/packages/trilium-core/src/services/search/services/lex.ts
+++ b/packages/trilium-core/src/services/search/services/lex.ts
@@ -97,6 +97,13 @@ function lex(str: string) {
}
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)) {
+ currentWord += chr;
+ continue;
+ }
+
if (!fulltextEnded) {
fulltextEnded = true;
} else {
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 1a42799d48..071df25123 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -413,6 +413,19 @@ describe("Search", () => {
expect(searchResults.length).toEqual(5);
});
+ it("a # inside a full-text word is searched literally", () => {
+ rootNote
+ .child(note("Learning C# basics"))
+ .child(note("C basics").label("book"));
+
+ const searchContext = new SearchContext();
+ const literal = searchService.findResultsWithQuery("c# basics", searchContext);
+ expect(searchContext.error).toBeNull();
+ expect(literal.map((r) => becca.notes[r.noteId].title)).toEqual(["Learning C# basics"]);
+
+ expect(searchService.findResultsWithQuery("basics#book", new SearchContext())).toEqual([]);
+ });
+
it("exact word search matches a content word wrapped in punctuation (#10616)", () => {
// The note body contains "(sync)" (parenthesised). Exact-word search for
// "sync" must still find it, because content tokenization strips boundary