Skip to content

Commit abe720c

Browse files
committed
feat: fix a perf issue when searching with a large number of Chinese words
fixes #312
1 parent 870dc88 commit abe720c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

docusaurus-search-local/src/client/utils/smartQueries.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ export function smartQueries(
8181
refinedTerms = terms.slice();
8282
}
8383

84+
const MAX_TERMS = 10;
85+
if (refinedTerms.length > MAX_TERMS) {
86+
// Sort terms by length in ascending order.,
87+
// And keep the top 10 terms.
88+
refinedTerms.sort((a, b) => a.length - b.length);
89+
refinedTerms.splice(MAX_TERMS, refinedTerms.length - MAX_TERMS);
90+
91+
terms.sort((a, b) => a.length - b.length);
92+
terms.splice(MAX_TERMS, terms.length - MAX_TERMS);
93+
}
94+
8495
// Also try to add extra terms which miss one of the searched tokens,
8596
// when the term contains 3 or more tokens,
8697
// to improve the search precision.

0 commit comments

Comments
 (0)