Skip to content

Commit

Permalink
feat: fix a perf issue when searching with a large number of Chinese …
Browse files Browse the repository at this point in the history
…words

fixes #312
  • Loading branch information
weareoutman committed Oct 9, 2024
1 parent 870dc88 commit abe720c
Showing 1 changed file with 11 additions and 0 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

0 comments on commit abe720c

Please sign in to comment.