Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): make filters on contentSize and revisionCount actually match

Elian Doran82e5cad89f42
diff --git a/packages/trilium-core/src/services/search/expressions/property_comparison.spec.ts b/packages/trilium-core/src/services/search/expressions/property_comparison.spec.ts
index 455dc0dee5..39bc1c09b4 100644
--- a/packages/trilium-core/src/services/search/expressions/property_comparison.spec.ts
+++ b/packages/trilium-core/src/services/search/expressions/property_comparison.spec.ts
@@ -64,10 +64,10 @@ describe("PropertyComparisonExp", () => {
expect(searchContext.dbLoadNeeded).toBeUndefined();
});
- it("leaves dbLoadNeeded unset for DB-backed properties (mapped name never matches the lower-cased guard)", () => {
- // The guard compares the already case-mapped propertyName ("contentSize") against
- // a list of lower-cased names ("contentsize"), so it can never match - dbLoadNeeded
- // is therefore never set here. This locks in the current observed behaviour.
+ it("flags dbLoadNeeded for DB-backed properties", () => {
+ // These four aren't held on the becca note; the search service only computes them when
+ // dbLoadNeeded is set. The guard used to test the case-mapped name ("contentSize") against
+ // a lower-cased list, so it never matched and filtering on them always found nothing.
for (const prop of [
"contentsize",
"contentandattachmentssize",
@@ -76,7 +76,7 @@ describe("PropertyComparisonExp", () => {
]) {
const searchContext: any = {};
new PropertyComparisonExp(searchContext, prop, ">", "0");
- expect(searchContext.dbLoadNeeded).toBeUndefined();
+ expect(searchContext.dbLoadNeeded).toBe(true);
}
});
});
diff --git a/packages/trilium-core/src/services/search/expressions/property_comparison.ts b/packages/trilium-core/src/services/search/expressions/property_comparison.ts
index 02906f9956..75e7a4611e 100644
--- a/packages/trilium-core/src/services/search/expressions/property_comparison.ts
+++ b/packages/trilium-core/src/services/search/expressions/property_comparison.ts
@@ -36,6 +36,20 @@ const PROP_MAPPING: Record<string, string> = {
revisioncount: "revisionCount"
};
+/**
+ * Properties that aren't held on the becca note and have to be computed from the database by
+ * `loadNeededInfoFromDatabase()` in the search service, which only runs when `dbLoadNeeded` is set.
+ *
+ * These are lower-cased, like the search string and {@link PROP_MAPPING}'s keys, so they must be
+ * matched against the raw `propertyName` argument — NOT against the case-mapped `this.propertyName`.
+ */
+const DB_BACKED_PROPERTIES = new Set([
+ "contentsize",
+ "contentandattachmentssize",
+ "contentandattachmentsandrevisionssize",
+ "revisioncount"
+]);
+
interface SearchContext {
dbLoadNeeded?: boolean;
}
@@ -58,7 +72,7 @@ class PropertyComparisonExp extends Expression {
this.comparedValue = comparedValue; // for DEBUG mode
this.comparator = buildComparator(operator, comparedValue);
- if (["contentsize", "contentandattachmentssize", "contentandattachmentsandrevisionssize", "revisioncount"].includes(this.propertyName)) {
+ if (DB_BACKED_PROPERTIES.has(propertyName)) {
searchContext.dbLoadNeeded = true;
}
}
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
new file mode 100644
index 0000000000..1d5045c732
--- /dev/null
+++ b/packages/trilium-core/src/services/search/services/search_db_properties.spec.ts
@@ -0,0 +1,60 @@
+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 searchService from "./search.js";
+
+/**
+ * `contentSize`, `contentAndAttachmentsSize`, `contentAndAttachmentsAndRevisionsSize` and
+ * `revisionCount` are not held on the becca note — the search service computes them in
+ * `loadNeededInfoFromDatabase()`, which only runs when an expression asked for it via
+ * `searchContext.dbLoadNeeded`. That makes these the one group of properties that needs a real
+ * database to test, hence this file rather than the mocked-becca `search.spec.ts`.
+ *
+ * Regression: PropertyComparisonExp used to set the flag by testing its case-mapped property name
+ * ("contentSize") against a lower-cased list, so it never matched. The fields stayed undefined and
+ * every comparison against them silently matched nothing, while `orderBy` on the same properties
+ * worked (ValueExtractor does the equivalent check correctly).
+ */
+describe("search on database-backed note properties", () => {
+ const BODY_LENGTH = 500;
+
+ beforeAll(async () => {
+ sql_init.initializeDb();
+ await sql_init.dbReady;
+
+ getContext().init(() =>
+ noteService.createNewNote({
+ parentNoteId: "root",
+ title: "DbPropsProbe",
+ type: "text",
+ mime: "text/html",
+ content: `<p>${"x".repeat(BODY_LENGTH)}</p>`
+ })
+ );
+ });
+
+ it("filters on contentSize instead of silently matching nothing", () => {
+ const titleOnly = searchService.searchNotes("note.title *=* DbPropsProbe");
+ expect(titleOnly).toHaveLength(1);
+
+ // Every note clears `>= 0`, so this must not narrow the result at all.
+ expect(searchService.searchNotes("note.title *=* DbPropsProbe AND note.contentSize >= 0")).toHaveLength(1);
+ expect(searchService.searchNotes(`note.title *=* DbPropsProbe AND note.contentSize >= ${BODY_LENGTH}`)).toHaveLength(1);
+ // ... and a threshold above the body really does exclude it, so the value is genuinely compared.
+ expect(searchService.searchNotes(`note.title *=* DbPropsProbe AND note.contentSize > ${BODY_LENGTH * 10}`)).toHaveLength(0);
+ });
+
+ it("filters on the remaining database-backed properties", () => {
+ for (const property of ["contentAndAttachmentsSize", "contentAndAttachmentsAndRevisionsSize", "revisionCount"]) {
+ expect(searchService.searchNotes(`note.title *=* DbPropsProbe AND note.${property} >= 0`)).toHaveLength(1);
+ }
+ });
+
+ it("still supports ordering by them", () => {
+ // ValueExtractor sets the same flag for orderBy, which is why that path always worked.
+ const ordered = searchService.searchNotes("note.title *=* DbPropsProbe orderBy note.contentSize desc");
+ expect(ordered).toHaveLength(1);
+ });
+});