Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): load db-backed note properties for pure '#' queries and count revisions

unknownee772276689b
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index d5290ee1da..8517c18104 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -227,9 +227,12 @@ function loadNeededInfoFromDatabase() {
noteBlobs[noteId][blobId] = length;
if (isNoteRevision) {
- const noteRevision = becca.notes[noteId];
- if (noteRevision && noteRevision.revisionCount) {
- noteRevision.revisionCount++;
+ const note = becca.notes[noteId];
+ // Why the existence-only guard: `revisionCount` was reset to 0 a few lines above, so
+ // the old truthiness check made this increment dead code and every comparison against
+ // `note.revisionCount` matched only zero (#11131).
+ if (note) {
+ note.revisionCount = (note.revisionCount ?? 0) + 1;
}
}
}
@@ -429,7 +432,15 @@ function findResultsWithQuery(query: string, searchContext: SearchContext): Sear
const isPureExpressionQuery = query.trim().startsWith('#');
if (isPureExpressionQuery) {
- // For pure expression queries, use standard search without progressive phases
+ // For pure expression queries, use standard search without progressive phases.
+ // This path must not skip the database load: `#label note.contentSize >= 0` asks for a
+ // db-backed property just like any other query, and the values are memoized on the becca
+ // notes, so skipping the load made the answer depend on what was searched earlier in the
+ // process (#11131).
+ if (searchContext.dbLoadNeeded) {
+ loadNeededInfoFromDatabase();
+ }
+
return performSearch(expression, searchContext, searchContext.enableFuzzyMatching);
}
diff --git a/packages/trilium-core/src/services/search/services/search_db_properties.spec.ts b/packages/trilium-core/src/services/search/services/search_db_properties.spec.ts
index 1d5045c732..609a8acdf8 100644
--- a/packages/trilium-core/src/services/search/services/search_db_properties.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search_db_properties.spec.ts
@@ -3,6 +3,7 @@ import { beforeAll, describe, expect, it } from "vitest";
import { getContext } from "../../context.js";
import noteService from "../../notes.js";
import sql_init from "../../sql_init.js";
+import { getSql } from "../../sql/index.js";
import searchService from "./search.js";
/**
@@ -30,11 +31,59 @@ describe("search on database-backed note properties", () => {
title: "DbPropsProbe",
type: "text",
mime: "text/html",
- content: `<p>${"x".repeat(BODY_LENGTH)}</p>`
+ content: `<p>${"x".repeat(BODY_LENGTH)}</p>`,
+ attributes: [{ type: "label", name: "tcDbProbe", isInheritable: false, position: 0 }]
})
);
});
+ /**
+ * The loader memoizes its results onto the becca notes, so a test that ran any other
+ * db-backed query first would mask exactly the cold-process failure these tests assert.
+ */
+ function resetMemoizedNoteProperties() {
+ const probe = searchService.searchNotes("note.title *=* DbPropsProbe")[0];
+ probe.contentSize = undefined;
+ probe.contentAndAttachmentsSize = undefined;
+ probe.contentAndAttachmentsAndRevisionsSize = undefined;
+ probe.revisionCount = undefined;
+ }
+
+ it("loads the database-backed properties for a pure '#' query", () => {
+ resetMemoizedNoteProperties();
+
+ // Regression (#11131): queries starting with '#' take the pure-expression shortcut,
+ // which used to skip loadNeededInfoFromDatabase() entirely — the comparison then ran
+ // against undefined, and the same query answered differently depending on what had
+ // been searched earlier in the process.
+ expect(searchService.searchNotes("#tcDbProbe note.contentSize >= 0")).toHaveLength(1);
+ expect(searchService.searchNotes(`#tcDbProbe note.contentSize > ${BODY_LENGTH}`)).toHaveLength(1);
+ expect(searchService.searchNotes(`#tcDbProbe note.contentSize > ${BODY_LENGTH * 10}`)).toHaveLength(0);
+ });
+
+ it("counts revisions instead of always reporting zero", () => {
+ const probe = searchService.searchNotes("note.title *=* DbPropsProbe")[0];
+ const utcNow = "2026-01-01T00:00:00.000Z";
+ const dateNow = "2026-01-01 00:00:00";
+
+ getSql().execute(
+ `INSERT INTO blobs (blobId, content, dateModified, utcDateModified) VALUES (?, ?, ?, ?)`,
+ ["tcRevBlob1", "y".repeat(200), dateNow, utcNow]
+ );
+ getSql().execute(
+ `INSERT INTO revisions (revisionId, noteId, title, blobId, utcDateLastEdited, utcDateCreated, utcDateModified, dateLastEdited, dateCreated)
+ VALUES (?, ?, 'DbPropsProbe', ?, ?, ?, ?, ?, ?)`,
+ ["tcRev1", probe.noteId, "tcRevBlob1", utcNow, utcNow, utcNow, dateNow, dateNow]
+ );
+
+ resetMemoizedNoteProperties();
+
+ // Regression (#11131): the increment was guarded by the truthiness of a counter that had
+ // just been reset to zero, so revisionCount stayed 0 on every path.
+ expect(searchService.searchNotes("note.title *=* DbPropsProbe AND note.revisionCount >= 1")).toHaveLength(1);
+ expect(searchService.searchNotes("note.title *=* DbPropsProbe AND note.revisionCount = 0")).toHaveLength(0);
+ });
+
it("filters on contentSize instead of silently matching nothing", () => {
const titleOnly = searchService.searchNotes("note.title *=* DbPropsProbe");
expect(titleOnly).toHaveLength(1);