Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/content manager: improve ordering when there are multiple notes with the same title
Adorian Doran9b08e012b315
diff --git a/apps/client/src/services/content_manager.spec.ts b/apps/client/src/services/content_manager.spec.tsindex e648cef082..d82196a59b 100644--- a/apps/client/src/services/content_manager.spec.ts+++ b/apps/client/src/services/content_manager.spec.ts@@ -16,12 +16,18 @@ function categoryById(id: string) {}describe("buildCategoryQuery", () => {- it("orders alphabetically by title", () => {- expect(buildCategoryQuery("#appCss", "title")).toBe("#appCss orderBy note.title");+ it("orders alphabetically by title, then by note ID", () => {+ expect(buildCategoryQuery("#appCss", "title")).toBe("#appCss orderBy note.title, note.noteId");});- it("orders newest first by creation date", () => {- expect(buildCategoryQuery("#appCss", "dateCreated")).toBe("#appCss orderBy note.dateCreated desc");+ it("orders newest first by creation date, then by note ID", () => {+ expect(buildCategoryQuery("#appCss", "dateCreated")).toBe("#appCss orderBy note.dateCreated desc, note.noteId");+ });++ it("always breaks ties on note ID so the order never shifts between refreshes", () => {+ for (const sortOrder of [ "title", "dateCreated" ] as const) {+ expect(buildCategoryQuery("#appCss", sortOrder)).toMatch(/, note\.noteId$/);+ }});it("keeps the ordering clause at the top level of an OR chain", () => {@@ -29,7 +35,7 @@ describe("buildCategoryQuery", () => {// filters must not be wrapped in parentheses.const query = buildCategoryQuery("#widget OR #disabled:widget", "title");- expect(query).toBe("#widget OR #disabled:widget orderBy note.title");+ expect(query).toBe("#widget OR #disabled:widget orderBy note.title, note.noteId");expect(query).not.toContain("(");});});diff --git a/apps/client/src/services/content_manager.ts b/apps/client/src/services/content_manager.tsindex 7cfdba037d..647f147bc7 100644--- a/apps/client/src/services/content_manager.ts+++ b/apps/client/src/services/content_manager.ts@@ -82,10 +82,17 @@ export async function findCategoryNotes(category: ContentCategory, sortOrder: Coreturn notes.filter((note) => isUserContent(note));}-/** Appends the ordering clause, which the search engine applies server-side. */+/**+ * Appends the ordering clause, which the search engine applies server-side.+ *+ * Note IDs break ties. Notes routinely share a title, and the sort would otherwise be unstable —+ * equally named rows could swap places on every refresh, which reads as the list jumping about.+ */export function buildCategoryQuery(filter: string, sortOrder: ContentSortOrder) {// Newest first reads better for dates, while titles are most useful alphabetically.- const orderBy = sortOrder === "dateCreated" ? "note.dateCreated desc" : "note.title";+ const orderBy = sortOrder === "dateCreated"+ ? "note.dateCreated desc, note.noteId"+ : "note.title, note.noteId";return `${filter} orderBy ${orderBy}`;}diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.tsindex b50de9bb58..5fd52b8a72 100644--- a/packages/trilium-core/src/services/search/services/search.spec.ts+++ b/packages/trilium-core/src/services/search/services/search.spec.ts@@ -663,6 +663,25 @@ describe("Search", () => {expect(searchResults.length).toEqual(4);});+ it("test order by multiple properties", () => {+ // Two notes deliberately share a title: with only one order key their relative order is+ // whatever the sort happens to produce, so a second key is what makes the result stable.+ const bravo = new NoteBuilder(new BNote({ noteId: "nB", title: "Shared", type: "text" }));+ const alpha = new NoteBuilder(new BNote({ noteId: "nA", title: "Shared", type: "text" }));+ const unique = new NoteBuilder(new BNote({ noteId: "nZ", title: "Unique", type: "text" }));++ rootNote.child(note("Europe").child(bravo).child(alpha).child(unique));++ const searchContext = new SearchContext();++ let searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe orderBy note.title, note.noteId", searchContext);+ expect(searchResults.map((sr) => sr.noteId)).toEqual([ "nA", "nB", "nZ" ]);++ // The secondary key carries its own direction, independently of the primary one.+ searchResults = searchService.findResultsWithQuery("# note.parents.title = Europe orderBy note.title, note.noteId DESC", searchContext);+ expect(searchResults.map((sr) => sr.noteId)).toEqual([ "nB", "nA", "nZ" ]);+ });+it("test not(...)", () => {const italy = note("Italy").label("capital", "Rome");const slovakia = note("Slovakia").label("capital", "Bratislava");