Skip to content

Commit 50f93f6

Browse files
authored
Merge pull request #463 from easyops-cn/steve/perf
Steve/perf
2 parents 3eddfab + abe720c commit 50f93f6

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
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.

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,26 @@ export function smartTerms(
1414
tokens: string[],
1515
zhDictionary: string[]
1616
): SmartTerm[] {
17-
const terms: SmartTerm[] = [];
17+
const tokenTerms = tokens.map((token) => {
18+
if (/\p{Unified_Ideograph}/u.test(token)) {
19+
return cutZhWords(token, zhDictionary);
20+
} else {
21+
return [{ value: token }];
22+
}
23+
});
1824

19-
function cutMixedWords(subTokens: string[], carry: SmartTerm): void {
20-
if (subTokens.length === 0) {
25+
// Get all possible combinations of terms.
26+
const terms: SmartTerm[] = [];
27+
function combine(index: number, carry: SmartTerm): void {
28+
if (index === tokenTerms.length) {
2129
terms.push(carry);
2230
return;
2331
}
24-
const token = subTokens[0];
25-
if (/\p{Unified_Ideograph}/u.test(token)) {
26-
const terms = cutZhWords(token, zhDictionary);
27-
for (const term of terms) {
28-
const nextCarry = carry.concat(...term);
29-
cutMixedWords(subTokens.slice(1), nextCarry);
30-
}
31-
} else {
32-
const nextCarry = carry.concat({
33-
value: token,
34-
});
35-
cutMixedWords(subTokens.slice(1), nextCarry);
32+
for (const term of tokenTerms[index]) {
33+
combine(index + 1, carry.concat(term));
3634
}
3735
}
38-
39-
cutMixedWords(tokens, []);
36+
combine(0, []);
4037

4138
return terms;
4239
}

0 commit comments

Comments
 (0)