Skip to content

Commit

Permalink
fix: do not limit the search page result by searchResultLimits
Browse files Browse the repository at this point in the history
Closes  #480
  • Loading branch information
weareoutman committed Jan 16, 2025
1 parent 416b553 commit 29f6d69
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import {
useAllContextsWithNoSearchContext,
} from "../../utils/proxiedGenerated";
import LoadingRing from "../LoadingRing/LoadingRing";
import { normalizeContextByPath } from "../../utils/normalizeContextByPath";
import { searchResultLimits } from "../../utils/proxiedGeneratedConstants";

import styles from "./SearchBar.module.css";
import { normalizeContextByPath } from "../../utils/normalizeContextByPath";

async function fetchAutoCompleteJS(): Promise<any> {
const autoCompleteModule = await import("@easyops-cn/autocomplete.js");
Expand Down Expand Up @@ -261,7 +262,8 @@ export default function SearchBar({
const result = await searchByWorker(
versionUrl,
searchContext,
input
input,
searchResultLimits
);
callback(result);
},
Expand Down Expand Up @@ -432,7 +434,7 @@ export default function SearchBar({
// Manually make the search bar be LTR even if in RTL
dir="ltr"
>
<input
<input
placeholder={translate({
id: "theme.SearchBar.label",
message: "Search",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ function SearchPageContent(): React.ReactElement {
const results = await searchByWorker(
versionUrl,
searchContext,
searchQuery
searchQuery,
100
);
setSearchResults(results);
})();
Expand Down
8 changes: 5 additions & 3 deletions docusaurus-search-local/src/client/theme/searchByWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ interface RemoteWorker {
search(
baseUrl: string,
searchContext: string,
input: string
input: string,
limit: number
): Promise<SearchResult[]>;
}

Expand Down Expand Up @@ -37,11 +38,12 @@ export async function fetchIndexesByWorker(
export async function searchByWorker(
baseUrl: string,
searchContext: string,
input: string
input: string,
limit: number
): Promise<SearchResult[]> {
if (process.env.NODE_ENV === "production") {
const remoteWorker = await getRemoteWorker();
return remoteWorker!.search(baseUrl, searchContext, input);
return remoteWorker!.search(baseUrl, searchContext, input, limit);
}
return [];
}
11 changes: 6 additions & 5 deletions docusaurus-search-local/src/client/theme/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Comlink from "comlink";
import lunr from "lunr";
import { searchIndexUrl, searchResultLimits, language } from "../utils/proxiedGeneratedConstants";
import { searchIndexUrl, language } from "../utils/proxiedGeneratedConstants";
import { tokenize } from "../utils/tokenize";
import { smartQueries } from "../utils/smartQueries";
import {
Expand Down Expand Up @@ -49,7 +49,8 @@ export class SearchWorker {
async search(
baseUrl: string,
searchContext: string,
input: string
input: string,
limit: number
): Promise<SearchResult[]> {
const rawTokens = tokenize(input, language);
if (rawTokens.length === 0) {
Expand All @@ -76,15 +77,15 @@ export class SearchWorker {
});
}
})
.slice(0, searchResultLimits)
.slice(0, limit)
// Remove duplicated results.
.filter(
(result) =>
!results.some(
(item) => item.document.i.toString() === result.ref
)
)
.slice(0, searchResultLimits - results.length)
.slice(0, limit - results.length)
.map((result) => {
const document = documents.find(
(doc) => doc.i.toString() === result.ref
Expand All @@ -103,7 +104,7 @@ export class SearchWorker {
};
})
);
if (results.length >= searchResultLimits) {
if (results.length >= limit) {
break search;
}
}
Expand Down

0 comments on commit 29f6d69

Please sign in to comment.