Skip to content

Build your personal knowledge base with Trilium Notes

To prevent search lag when there are a large number of notes

SiriusXTadcb803caafe
diff --git a/src/public/app/services/note_autocomplete.ts b/src/public/app/services/note_autocomplete.ts
index e879c87f19..c194e257e1 100644
--- a/src/public/app/services/note_autocomplete.ts
+++ b/src/public/app/services/note_autocomplete.ts
@@ -11,7 +11,8 @@ const SELECTED_NOTE_PATH_KEY = "data-note-path";
const SELECTED_EXTERNAL_LINK_KEY = "data-external-link";
// To prevent search lag when there are a large number of notes, set a delay based on the number of notes to avoid jitter.
-const notesCount = await server.get<number>(`stats/notesCount`);
+const notesCount = await server.get<number>(`autocomplete/notesCount`);
+console.log(notesCount);
let debounceTimeoutId: ReturnType<typeof setTimeout>;
function getSearchDelay(notesCount: number): number {
diff --git a/src/routes/api/autocomplete.ts b/src/routes/api/autocomplete.ts
index a357ca4b3a..584c2c88ad 100644
--- a/src/routes/api/autocomplete.ts
+++ b/src/routes/api/autocomplete.ts
@@ -8,6 +8,7 @@ import cls from "../../services/cls.js";
import becca from "../../becca/becca.js";
import type { Request } from "express";
import ValidationError from "../../errors/validation_error.js";
+import sql from "../../services/sql.js";
function getAutocomplete(req: Request) {
if (typeof req.query.query !== "string") {
@@ -79,6 +80,15 @@ function getRecentNotes(activeNoteId: string) {
});
}
+// Get the total number of notes
+function getNotesCount(req: Request) {
+ const notesCount = sql.getRow(
+ `SELECT COUNT(*) AS count FROM notes WHERE isDeleted = 0;`,
+ ) as { count: number };
+ return notesCount.count;
+}
+
export default {
- getAutocomplete
+ getAutocomplete,
+ getNotesCount
};
diff --git a/src/routes/api/stats.ts b/src/routes/api/stats.ts
index 8f953bc0b2..15e28f0838 100644
--- a/src/routes/api/stats.ts
+++ b/src/routes/api/stats.ts
@@ -48,16 +48,7 @@ function getSubtreeSize(req: Request) {
};
}
-// Get the total number of notes
-function getNotesCount(req: Request) {
- const notesCount = sql.getRow(
- `SELECT COUNT(*) AS count FROM notes WHERE isDeleted = 0;`,
- ) as { count: number };
- return notesCount.count;
-}
-
export default {
getNoteSize,
- getSubtreeSize,
- getNotesCount
+ getSubtreeSize
};
diff --git a/src/routes/routes.ts b/src/routes/routes.ts
index 4b09def4fa..a08a37c9b9 100644
--- a/src/routes/routes.ts
+++ b/src/routes/routes.ts
@@ -273,6 +273,7 @@ function register(app: express.Application) {
route(PST, "/api/setup/sync-seed", [auth.checkAppNotInitialized], setupApiRoute.saveSyncSeed, apiResultHandler, false);
apiRoute(GET, "/api/autocomplete", autocompleteApiRoute.getAutocomplete);
+ apiRoute(GET, "/api/autocomplete/notesCount", autocompleteApiRoute.getNotesCount);
apiRoute(GET, "/api/quick-search/:searchString", searchRoute.quickSearch);
apiRoute(GET, "/api/search-note/:noteId", searchRoute.searchFromNote);
apiRoute(PST, "/api/search-and-execute-note/:noteId", searchRoute.searchAndExecute);
@@ -361,7 +362,6 @@ function register(app: express.Application) {
apiRoute(GET, "/api/similar-notes/:noteId", similarNotesRoute.getSimilarNotes);
apiRoute(GET, "/api/backend-log", backendLogRoute.getBackendLog);
apiRoute(GET, "/api/stats/note-size/:noteId", statsRoute.getNoteSize);
- apiRoute(GET, "/api/stats/notesCount", statsRoute.getNotesCount);
apiRoute(GET, "/api/stats/subtree-size/:noteId", statsRoute.getSubtreeSize);
apiRoute(PST, "/api/delete-notes-preview", notesApiRoute.getDeleteNotesPreview);
route(GET, "/api/fonts", [auth.checkApiAuthOrElectron], fontsRoute.getFontCss);