Skip to content

Build your personal knowledge base with Trilium Notes

perf(collections): skip archived notes in the filter search

Elian Dorand0bc2e7ecf3f
diff --git a/apps/client/src/services/froca-interface.ts b/apps/client/src/services/froca-interface.ts
index aeb3bfc529..1230e40633 100644
--- a/apps/client/src/services/froca-interface.ts
+++ b/apps/client/src/services/froca-interface.ts
@@ -49,8 +49,13 @@ export interface FrocaSource {
getBlob(entityType: string, entityId: string): Promise<FBlobRow>;
/** The ids of the notes below `ancestorNoteId` that match the search `query`. */
searchNoteIds(query: string, ancestorNoteId: string): Promise<string[]>;
- /** The notes below `ancestorNoteId` that match `query`, with the tokens to highlight and its error. */
- searchInSubtree(query: string, ancestorNoteId: string): Promise<SearchWithTokensResponse>;
+ /**
+ * The notes below `ancestorNoteId` that match `query`, with the tokens to highlight and its
+ * error. Archived notes are searched only when `includeArchived` is set.
+ */
+ searchInSubtree(
+ query: string, ancestorNoteId: string, includeArchived: boolean
+ ): Promise<SearchWithTokensResponse>;
/** What is wrong with the search string, read without running it. */
lintSearch(searchString: string): Promise<SearchLintResponse>;
/** The names of the attributes of `type` that contain `query`, to complete one with. */
@@ -84,7 +89,9 @@ export interface Froca {
getAttachmentsForNote(noteId: string): Promise<FAttachment[]>;
searchNoteIds(query: string, ancestorNoteId: string): Promise<string[]>;
- searchInSubtree(query: string, ancestorNoteId: string): Promise<SearchWithTokensResponse>;
+ searchInSubtree(
+ query: string, ancestorNoteId: string, includeArchived: boolean
+ ): Promise<SearchWithTokensResponse>;
lintSearch(searchString: string): Promise<SearchLintResponse>;
getAttributeNames(type: "label" | "relation", query: string): Promise<string[]>;
getNoteMap(mapRootNoteId: string, mapType: "tree" | "link", filters: NoteMapFilters): Promise<NoteMapPostResponse>;
diff --git a/apps/client/src/services/froca.ts b/apps/client/src/services/froca.ts
index 6e29f6c8e6..cc8c28d690 100644
--- a/apps/client/src/services/froca.ts
+++ b/apps/client/src/services/froca.ts
@@ -421,8 +421,8 @@ class FrocaImpl implements Froca {
return this.source.searchNoteIds(query, ancestorNoteId);
}
- searchInSubtree(query: string, ancestorNoteId: string) {
- return this.source.searchInSubtree(query, ancestorNoteId);
+ searchInSubtree(query: string, ancestorNoteId: string, includeArchived: boolean) {
+ return this.source.searchInSubtree(query, ancestorNoteId, includeArchived);
}
lintSearch(searchString: string) {
@@ -490,9 +490,11 @@ const SERVER_SOURCE: FrocaSource = {
const { default: search } = await import("./search.js");
return await search.searchForNoteIds(query);
},
- searchInSubtree: (query, ancestorNoteId) => server.get<SearchWithTokensResponse>(
- `search?searchString=${encodeURIComponent(query)}`
- + `&ancestorNoteId=${encodeURIComponent(ancestorNoteId)}&includeTokens=true`),
+ searchInSubtree: (query, ancestorNoteId, includeArchived) =>
+ server.get<SearchWithTokensResponse>(
+ `search?searchString=${encodeURIComponent(query)}`
+ + `&ancestorNoteId=${encodeURIComponent(ancestorNoteId)}&includeTokens=true`
+ + `&includeArchivedNotes=${includeArchived}`),
lintSearch: (searchString) => server.post<SearchLintResponse>("search/lint", { searchString }),
getAttributeNames: (type, query) =>
server.get<string[]>(`attribute-names/?type=${type}&query=${encodeURIComponent(query)}`),
diff --git a/apps/client/src/services/search.spec.ts b/apps/client/src/services/search.spec.ts
index 3abc008cbe..a88b98f635 100644
--- a/apps/client/src/services/search.spec.ts
+++ b/apps/client/src/services/search.spec.ts
@@ -40,10 +40,11 @@ describe("search service", () => {
const get = vi.fn(async () => response);
server.get = get as typeof server.get;
- const result = await searchService.searchInSubtree("#done & task", "board1");
+ const result = await searchService.searchInSubtree("#done & task", "board1", false);
expect(get).toHaveBeenCalledWith(
- `search?searchString=${encodeURIComponent("#done & task")}&ancestorNoteId=board1&includeTokens=true`);
+ `search?searchString=${encodeURIComponent("#done & task")}&ancestorNoteId=board1`
+ + "&includeTokens=true&includeArchivedNotes=false");
expect(result).toBe(response);
});
diff --git a/apps/client/src/services/search.ts b/apps/client/src/services/search.ts
index 4c010b708e..bbb92a8187 100644
--- a/apps/client/src/services/search.ts
+++ b/apps/client/src/services/search.ts
@@ -17,9 +17,12 @@ async function searchForNotes(searchString: string) {
/**
* Runs a search restricted to one subtree and returns the matching note ids together with the
* tokens to highlight and any parse error, for filtering a collection down to the matches.
+ * Archived notes are searched only when `includeArchived` is set.
*/
-async function searchInSubtree(searchString: string, ancestorNoteId: string) {
- return await froca.searchInSubtree(searchString, ancestorNoteId);
+async function searchInSubtree(
+ searchString: string, ancestorNoteId: string, includeArchived: boolean
+) {
+ return await froca.searchInSubtree(searchString, ancestorNoteId, includeArchived);
}
/**
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 76e635b150..8b819ce2de 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -3877,7 +3877,7 @@ describe("Board filtering", () => {
// The matches arrive in score order; the board must keep branch order regardless.
const note = await setup({ matched: [ "filtered3", "filtered1" ] });
- expect(searchInSubtree).toHaveBeenCalledWith("#urgent", note.noteId);
+ expect(searchInSubtree).toHaveBeenCalledWith("#urgent", note.noteId, false);
expect(cardTitles(0)).toEqual([ "First", "Third" ]);
expect(cardTitles(1)).toEqual([]);
expect(container.querySelectorAll(".board-column")).toHaveLength(2);
diff --git a/apps/client/src/widgets/collections/collection_filter.spec.tsx b/apps/client/src/widgets/collections/collection_filter.spec.tsx
index a5cd82a72e..e4f6a1b01b 100644
--- a/apps/client/src/widgets/collections/collection_filter.spec.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.spec.tsx
@@ -139,7 +139,7 @@ describe("useCollectionFilter", () => {
});
await settle();
- expect(searchInSubtree).toHaveBeenCalledWith("#done", note.noteId);
+ expect(searchInSubtree).toHaveBeenCalledWith("#done", note.noteId, false);
expect([ ...(currentFilter?.shownNoteIds ?? []) ]).toEqual([ "n1", "n2" ]);
expect(currentFilter?.highlightedTokens).toEqual([ { token: "match", type: "plain" } ]);
@@ -170,10 +170,20 @@ describe("useCollectionFilter", () => {
searchInSubtree.mockResolvedValue(matchResponse([ "n1" ]));
const note = await mount({ persistedQuery: "#done" });
- expect(searchInSubtree).toHaveBeenCalledWith("#done", note.noteId);
+ expect(searchInSubtree).toHaveBeenCalledWith("#done", note.noteId, false);
expect(currentFilter?.shownNoteIds?.has("n1")).toBe(true);
});
+ it("searches archived notes only in a collection that shows them", async () => {
+ searchInSubtree.mockResolvedValue(matchResponse([ "n1" ]));
+ const note = await mount({
+ note: buildNote({ "title": "Board", "#includeArchived": "" }),
+ persistedQuery: "#done"
+ });
+
+ expect(searchInSubtree).toHaveBeenCalledWith("#done", note.noteId, true);
+ });
+
it("holds while a stored query resolves, and not for one submitted afterwards", async () => {
const stored = deferred<ReturnType<typeof matchResponse>>();
searchInSubtree.mockReturnValueOnce(stored);
diff --git a/apps/client/src/widgets/collections/collection_filter.tsx b/apps/client/src/widgets/collections/collection_filter.tsx
index 261a2c42b5..998842155e 100644
--- a/apps/client/src/widgets/collections/collection_filter.tsx
+++ b/apps/client/src/widgets/collections/collection_filter.tsx
@@ -10,7 +10,7 @@ import { t } from "../../services/i18n";
import type LoadResults from "../../services/load_results";
import searchService from "../../services/search";
import ActionButton from "../react/ActionButton";
-import { useNoteContext, useTriliumEvent } from "../react/hooks";
+import { useNoteContext, useNoteLabelBoolean, useTriliumEvent } from "../react/hooks";
import SearchStringEditor from "../search/SearchStringEditor";
/** How long to wait after a change before re-running the active filter. */
@@ -60,6 +60,9 @@ export function useCollectionFilter(note: FNote, {
collectionNoteIds: string[];
}): CollectionFilter {
const [ query, setQuery ] = useState(persistedQuery ?? "");
+ // The collection shows archived notes only with `#includeArchived`, so the search skips the
+ // rest instead of reading the content of notes that could never be drawn.
+ const [ includeArchived ] = useNoteLabelBoolean(note, "includeArchived");
const [ matches, setMatches ] = useState<FilterMatches>(NO_MATCHES);
const [ error, setError ] = useState<string | null>(null);
// Set before anything is drawn, so a collection opened onto a stored query waits for its
@@ -82,7 +85,7 @@ export function useCollectionFilter(note: FNote, {
const seq = ++runSeqRef.current;
let response;
try {
- response = await searchService.searchInSubtree(activeQuery, note.noteId);
+ response = await searchService.searchInSubtree(activeQuery, note.noteId, includeArchived);
} catch (e) {
console.error("Failed to run the collection filter:", e);
if (seq === runSeqRef.current) {
@@ -138,7 +141,7 @@ export function useCollectionFilter(note: FNote, {
return;
}
run(query);
- }, [ note.noteId, query ]);
+ }, [ note.noteId, query, includeArchived ]);
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
const collection = collectionSetRef.current;
diff --git a/packages/share-theme/src/content/share_froca_source.spec.ts b/packages/share-theme/src/content/share_froca_source.spec.ts
index 21365b50fc..73e64eaad0 100644
--- a/packages/share-theme/src/content/share_froca_source.spec.ts
+++ b/packages/share-theme/src/content/share_froca_source.spec.ts
@@ -31,7 +31,7 @@ describe("createShareFrocaSource", () => {
expect(await source.searchNoteIds("tag", "root")).toEqual([ "found" ]);
await source.saveAttachment("note 1", {} as never);
await source.removeAttachment("att 1");
- await source.searchInSubtree("#done", "root");
+ await source.searchInSubtree("#done", "root", false);
await source.getAttributeNames("label", "do");
await source.lintSearch("#done");
await source.getNoteMap("map 1", "link",
@@ -44,7 +44,7 @@ describe("createShareFrocaSource", () => {
"api/notes/note%201/attachments",
"api/notes/note%201/blob",
"api/notes?search=tag&ancestorNoteId=root",
- "api/search?searchString=%23done&ancestorNoteId=root",
+ "api/search?searchString=%23done&ancestorNoteId=root&includeArchivedNotes=false",
"api/attribute-names?type=label&query=do",
"api/search/lint?searchString=%23done",
"api/note-map/map%201/link?excludeRelation=template&includeRelation=author",
@@ -136,7 +136,7 @@ describe("createStaticFrocaSource", () => {
const source = createStaticFrocaSource({}, "");
expect(await source.searchNoteIds("#a", "root")).toEqual([]);
- expect(await source.searchInSubtree("#a", "root"))
+ expect(await source.searchInSubtree("#a", "root", true))
.toEqual({ searchResultNoteIds: [], highlightedTokens: [], error: null });
expect(await source.lintSearch("#a")).toEqual({ error: null });
expect(await source.getAttributeNames("label", "a")).toEqual([]);
diff --git a/packages/share-theme/src/content/share_froca_source.ts b/packages/share-theme/src/content/share_froca_source.ts
index 97c576fa88..b9ae2dbdbe 100644
--- a/packages/share-theme/src/content/share_froca_source.ts
+++ b/packages/share-theme/src/content/share_froca_source.ts
@@ -42,8 +42,10 @@ export function createShareFrocaSource(links: Record<string, string>): ShareFroc
// A visitor cannot change notes, so what a view saves holds only while the page is open.
saveAttachment: async () => {},
removeAttachment: async () => {},
- searchInSubtree: (query, ancestorNoteId) => {
- const params = new URLSearchParams({ searchString: query, ancestorNoteId });
+ searchInSubtree: (query, ancestorNoteId, includeArchived) => {
+ const params = new URLSearchParams({
+ searchString: query, ancestorNoteId, includeArchivedNotes: String(includeArchived)
+ });
return getJson(`api/search?${params}`);
},
getAttributeNames: (type, query) =>
diff --git a/packages/trilium-core/src/routes/api/search.spec.ts b/packages/trilium-core/src/routes/api/search.spec.ts
index e8e3a81dc6..67b984436f 100644
--- a/packages/trilium-core/src/routes/api/search.spec.ts
+++ b/packages/trilium-core/src/routes/api/search.spec.ts
@@ -62,6 +62,26 @@ describe("Search API (core)", () => {
expect(unscoped.body).toContain(outside.noteId);
});
+ it("leaves archived notes out of a full search only when asked to", async () => {
+ const token = "ZzArchivedSearchQwerty";
+ const board = await createTextNote(api, { title: "Board" });
+ const live = await createTextNote(api, { parentNoteId: board.noteId, title: `${token} live` });
+ const archived = await createTextNote(api, { parentNoteId: board.noteId, title: `${token} archived` });
+ await api.post(`/api/notes/${archived.noteId}/attributes`, {
+ body: { type: "label", name: "archived", value: "" }
+ });
+
+ const search = (includeArchivedNotes?: string) => api.get<string[]>("/api/search", {
+ query: { searchString: token, ancestorNoteId: board.noteId, includeArchivedNotes }
+ });
+
+ expect((await search()).body).toEqual(expect.arrayContaining([ live.noteId, archived.noteId ]));
+ expect((await search("true")).body).toEqual(expect.arrayContaining([ live.noteId, archived.noteId ]));
+ const withoutArchived = await search("false");
+ expect(withoutArchived.body).toContain(live.noteId);
+ expect(withoutArchived.body).not.toContain(archived.noteId);
+ });
+
it("returns note ids with token infos and the error when includeTokens is set", async () => {
const withTokens = await api.get<SearchWithTokensResponse>("/api/search", {
query: { searchString: UNIQUE_TOKEN, includeTokens: "true" }
diff --git a/packages/trilium-core/src/routes/api/search.ts b/packages/trilium-core/src/routes/api/search.ts
index 14d014c306..6df93a6f38 100644
--- a/packages/trilium-core/src/routes/api/search.ts
+++ b/packages/trilium-core/src/routes/api/search.ts
@@ -155,14 +155,19 @@ function quickSearch(req: Request<{}, { searchString?: string }>): QuickSearchRe
}
function search(
- req: Request<{}, { searchString?: string, ancestorNoteId?: string, includeTokens?: string }>
+ req: Request<{}, {
+ searchString?: string, ancestorNoteId?: string, includeTokens?: string,
+ includeArchivedNotes?: string
+ }>
): string[] | SearchWithTokensResponse {
const searchString = getSearchString(req);
- const { ancestorNoteId, includeTokens } = req.query;
+ const { ancestorNoteId, includeTokens, includeArchivedNotes } = req.query;
const searchContext = new SearchContext({
fastSearch: false,
- includeArchivedNotes: true,
+ // A collection that hides archived notes asks for them to be left out, so their content is
+ // never read.
+ includeArchivedNotes: includeArchivedNotes !== "false",
fuzzyAttributeSearch: false,
ignoreHoistedNote: true,
// Restricts the results to one subtree, for callers that filter a collection rather
diff --git a/packages/trilium-core/src/share/handlers.spec.ts b/packages/trilium-core/src/share/handlers.spec.ts
index 379e92bf52..4975a12392 100644
--- a/packages/trilium-core/src/share/handlers.spec.ts
+++ b/packages/trilium-core/src/share/handlers.spec.ts
@@ -541,6 +541,12 @@ describe("share handlers", () => {
`Basic ${encodeBase64("root:hunter2")}`);
expect(JSON.parse(String(authorized.body)).searchResultNoteIds).toEqual([ "seen", "guarded" ]);
+ const includesArchived = () =>
+ vi.mocked(searchService.findResultsWithQuery).mock.lastCall?.[1].includeArchivedNotes;
+ expect(includesArchived()).toBe(true);
+ filter({ searchString: "seen", ancestorNoteId: "board", includeArchivedNotes: "false" });
+ expect(includesArchived()).toBe(false);
+
expect(filter({ ancestorNoteId: "board" }).status).toBe(400);
expect(filter({ searchString: "seen" }).status).toBe(400);
expect(filter({ searchString: "seen", ancestorNoteId: "missing" }).status).toBe(404);
diff --git a/packages/trilium-core/src/share/handlers.ts b/packages/trilium-core/src/share/handlers.ts
index 797d7c21f5..f4963a029e 100644
--- a/packages/trilium-core/src/share/handlers.ts
+++ b/packages/trilium-core/src/share/handlers.ts
@@ -381,7 +381,7 @@ function downloadAttachment(req: ShareRequest): ShareReply {
* visitor can read with the tokens to highlight and the query's error.
*/
function searchInSubtree(req: ShareRequest): ShareReply {
- const { searchString, ancestorNoteId } = req.query;
+ const { searchString, ancestorNoteId, includeArchivedNotes } = req.query;
if (typeof searchString !== "string" || !searchString || typeof ancestorNoteId !== "string") {
return jsonReply(400, { message: "'searchString' and 'ancestorNoteId' parameters are mandatory." });
}
@@ -389,7 +389,7 @@ function searchInSubtree(req: ShareRequest): ShareReply {
const searchContext = new SearchContext({
fastSearch: false,
- includeArchivedNotes: true,
+ includeArchivedNotes: includeArchivedNotes !== "false",
fuzzyAttributeSearch: false,
ignoreHoistedNote: true,
ancestorNoteId