Skip to content

Build your personal knowledge base with Trilium Notes

perf(search): cap autocomplete at a dropdown's worth of results

Elian Doranc016aebb7bbd
diff --git a/packages/trilium-core/src/routes/api/autocomplete.spec.ts b/packages/trilium-core/src/routes/api/autocomplete.spec.ts
index 8f748788ba..df9378c28c 100644
--- a/packages/trilium-core/src/routes/api/autocomplete.spec.ts
+++ b/packages/trilium-core/src/routes/api/autocomplete.spec.ts
@@ -86,6 +86,19 @@ describe("Autocomplete API (core)", () => {
expect(Array.isArray(res.body)).toBe(true);
});
+ it("returns at most one dropdown's worth of results", async () => {
+ const marker = `capmarker${Date.now()}`;
+ for (let i = 0; i < 30; i++) {
+ await createTextNote(api, { title: `${marker} note ${i}` });
+ }
+
+ const res = await api.get<AutocompleteResult[]>("/api/autocomplete", {
+ query: { query: marker }
+ });
+ expect(res.status).toBe(200);
+ expect(res.body.length).toBe(25);
+ });
+
it("400s when the query param is missing", async () => {
const res = await api.get("/api/autocomplete");
expect(res.status).toBe(400);
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index 026c2ada66..24e115bf2a 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -739,6 +739,10 @@ function extractAttributeSnippet(noteId: string, searchTokens: HighlightedTokenI
}
}
+// Each row past this costs a `buildSearchResultDetails` snippet extraction and roughly a kilobyte of
+// response, so the limit follows what a dropdown shows rather than what the query matched.
+const AUTOCOMPLETE_RESULT_LIMIT = 25;
+
function searchNotesForAutocomplete(query: string, fastSearch: boolean = true) {
const searchContext = new SearchContext({
fastSearch,
@@ -749,7 +753,7 @@ function searchNotesForAutocomplete(query: string, fastSearch: boolean = true) {
ancestorNoteId: hoistedNoteService.isHoistedInHiddenSubtree() ? "root" : hoistedNoteService.getHoistedNoteId()
});
- const trimmed = findResultsWithQuery(query, searchContext).slice(0, 200);
+ const trimmed = findResultsWithQuery(query, searchContext).slice(0, AUTOCOMPLETE_RESULT_LIMIT);
return buildSearchResultDetails(trimmed, searchContext);
}