Skip to content

Commit 9cea757

Browse files
committed
feat: implement fetchAllSearchResults for comprehensive search result retrieval
1 parent 7d5d64e commit 9cea757

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/search-manager/data/api.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,58 @@ export async function fetchSearchResults({
310310
};
311311
}
312312

313+
/* eslint-disable no-await-in-loop */
314+
export async function fetchAllSearchResults(
315+
params: FetchSearchParams,
316+
): Promise<{
317+
hits: HitType[];
318+
totalHits: number;
319+
blockTypes: Record<string, number>;
320+
problemTypes: Record<string, number>;
321+
publishStatus: Record<string, number>;
322+
}> {
323+
const allHits: HitType[] = [];
324+
const limit = params.limit ?? 20;
325+
326+
let offset = 0;
327+
let nextOffset: number | undefined;
328+
329+
// Facets + totals only need to be read once (from the first page)
330+
let totalHits = 0;
331+
let blockTypes: Record<string, number> = {};
332+
let problemTypes: Record<string, number> = {};
333+
let publishStatus: Record<string, number> = {};
334+
335+
do {
336+
const page = await fetchSearchResults({
337+
...params,
338+
offset,
339+
limit,
340+
});
341+
342+
allHits.push(...page.hits);
343+
344+
if (offset === 0) {
345+
totalHits = page.totalHits;
346+
blockTypes = page.blockTypes;
347+
problemTypes = page.problemTypes;
348+
publishStatus = page.publishStatus;
349+
}
350+
351+
nextOffset = page.nextOffset;
352+
offset = nextOffset ?? offset;
353+
} while (nextOffset !== undefined);
354+
355+
return {
356+
hits: allHits,
357+
totalHits,
358+
blockTypes,
359+
problemTypes,
360+
publishStatus,
361+
};
362+
}
363+
/* eslint-enable no-await-in-loop */
364+
313365
/**
314366
* Fetch the block types facet distribution for the search results.
315367
*/

src/search-manager/data/apiHooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
SearchSortOption,
77
TAG_SEP,
88
fetchAvailableTagOptions,
9-
fetchSearchResults,
9+
fetchAllSearchResults,
1010
fetchTagsThatMatchKeyword,
1111
getContentSearchConfig,
1212
fetchBlockTypes,
@@ -106,7 +106,7 @@ export const useContentSearchResults = ({
106106
if (client === undefined || indexName === undefined) {
107107
throw new Error('Required data unexpectedly undefined. Check "enable" condition of useQuery.');
108108
}
109-
return fetchSearchResults({
109+
return fetchAllSearchResults({
110110
client,
111111
extraFilter,
112112
indexName,

0 commit comments

Comments
 (0)