From abe720cd3d521122e3aaa8b2d8ba7d0967fe046d Mon Sep 17 00:00:00 2001 From: weareoutman Date: Wed, 9 Oct 2024 14:15:18 +0800 Subject: [PATCH] feat: fix a perf issue when searching with a large number of Chinese words fixes #312 --- .../src/client/utils/smartQueries.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docusaurus-search-local/src/client/utils/smartQueries.ts b/docusaurus-search-local/src/client/utils/smartQueries.ts index 29fe8d7b..3a10d1cb 100644 --- a/docusaurus-search-local/src/client/utils/smartQueries.ts +++ b/docusaurus-search-local/src/client/utils/smartQueries.ts @@ -81,6 +81,17 @@ export function smartQueries( refinedTerms = terms.slice(); } + const MAX_TERMS = 10; + if (refinedTerms.length > MAX_TERMS) { + // Sort terms by length in ascending order., + // And keep the top 10 terms. + refinedTerms.sort((a, b) => a.length - b.length); + refinedTerms.splice(MAX_TERMS, refinedTerms.length - MAX_TERMS); + + terms.sort((a, b) => a.length - b.length); + terms.splice(MAX_TERMS, terms.length - MAX_TERMS); + } + // Also try to add extra terms which miss one of the searched tokens, // when the term contains 3 or more tokens, // to improve the search precision.