Skip to content

Commit

Permalink
Merge pull request #463 from easyops-cn/steve/perf
Browse files Browse the repository at this point in the history
Steve/perf
  • Loading branch information
weareoutman authored Oct 9, 2024
2 parents 3eddfab + abe720c commit 50f93f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
11 changes: 11 additions & 0 deletions docusaurus-search-local/src/client/utils/smartQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 14 additions & 17 deletions docusaurus-search-local/src/client/utils/smartTerms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,26 @@ export function smartTerms(
tokens: string[],
zhDictionary: string[]
): SmartTerm[] {
const terms: SmartTerm[] = [];
const tokenTerms = tokens.map((token) => {
if (/\p{Unified_Ideograph}/u.test(token)) {
return cutZhWords(token, zhDictionary);
} else {
return [{ value: token }];
}
});

function cutMixedWords(subTokens: string[], carry: SmartTerm): void {
if (subTokens.length === 0) {
// Get all possible combinations of terms.
const terms: SmartTerm[] = [];
function combine(index: number, carry: SmartTerm): void {
if (index === tokenTerms.length) {
terms.push(carry);
return;
}
const token = subTokens[0];
if (/\p{Unified_Ideograph}/u.test(token)) {
const terms = cutZhWords(token, zhDictionary);
for (const term of terms) {
const nextCarry = carry.concat(...term);
cutMixedWords(subTokens.slice(1), nextCarry);
}
} else {
const nextCarry = carry.concat({
value: token,
});
cutMixedWords(subTokens.slice(1), nextCarry);
for (const term of tokenTerms[index]) {
combine(index + 1, carry.concat(term));
}
}

cutMixedWords(tokens, []);
combine(0, []);

return terms;
}

0 comments on commit 50f93f6

Please sign in to comment.