From f5fcd03e270df2728217499f3b4d1da66684d86d Mon Sep 17 00:00:00 2001 From: Vyacheslav Tumanov Date: Mon, 1 Apr 2024 13:24:11 +0500 Subject: [PATCH] improve mentions speed (#5121) Signed-off-by: Vyacheslav Tumanov --- packages/presentation/src/search.ts | 37 ++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/packages/presentation/src/search.ts b/packages/presentation/src/search.ts index cc85e512e6..113882b907 100644 --- a/packages/presentation/src/search.ts +++ b/packages/presentation/src/search.ts @@ -61,6 +61,25 @@ function packSearchResultsForListView (sections: SearchSection[]): SearchItem[] return results } +async function searchCategory ( + client: TxOperations, + cl: Ref>, + query: string, + categories: ObjectSearchCategory[] +): Promise { + const r = await client.searchFulltext( + { + query: `${query}*`, + classes: [cl] + }, + { + limit: 5 + } + ) + const category = findCategoryByClass(categories, cl) + return category !== undefined ? { category, items: r.docs } : undefined +} + async function doFulltextSearch ( client: TxOperations, classes: Array>>, @@ -68,18 +87,14 @@ async function doFulltextSearch ( categories: ObjectSearchCategory[] ): Promise { const sections: SearchSection[] = [] + const promises: Array> = [] for (const cl of classes) { - const r = await client.searchFulltext( - { - query: `${query}*`, - classes: [cl] - }, - { - limit: 5 - } - ) - const category = findCategoryByClass(categories, cl) - if (category !== undefined) sections.push({ category, items: r.docs }) + promises.push(searchCategory(client, cl, query, categories)) + } + + const resolvedSections = await Promise.all(promises) + for (const s of resolvedSections) { + if (s !== undefined) sections.push(s) } return sections.sort((a, b) => {