nomagick commited on
Commit
e4d46e7
·
unverified ·
1 Parent(s): 1c4b64f

fix: count parameter

Browse files
backend/functions/src/cloud-functions/searcher.ts CHANGED
@@ -173,7 +173,8 @@ export class SearcherHost extends RPCHost {
173
  }
174
 
175
  const it = this.fetchSearchResults(crawlerOptions.respondWith, r.web?.results, crawlOpts,
176
- { ...crawlerOptions, cacheTolerance: crawlerOptions.cacheTolerance || this.pageCacheToleranceMs }
 
177
  );
178
 
179
  if (!ctx.req.accepts('text/plain') && ctx.req.accepts('text/event-stream')) {
@@ -311,6 +312,7 @@ export class SearcherHost extends RPCHost {
311
  searchResults?: WebSearchResult[],
312
  options?: ExtraScrappingOptions,
313
  crawlerOptions?: CrawlerOptions,
 
314
  ) {
315
  if (!searchResults) {
316
  return;
@@ -337,20 +339,21 @@ export class SearcherHost extends RPCHost {
337
 
338
  const resultArray = await Promise.all(mapped) as FormattedPage[];
339
 
340
- yield this.reOrganizeSearchResults(resultArray);
341
  }
342
  }
343
 
344
- reOrganizeSearchResults(searchResults: FormattedPage[]) {
 
345
  const [qualifiedPages, unqualifiedPages] = _.partition(searchResults, (x) => this.pageQualified(x));
346
  const acceptSet = new Set(qualifiedPages);
347
 
348
- const n = this.targetResultCount - qualifiedPages.length;
349
  for (const x of unqualifiedPages.slice(0, n >= 0 ? n : 0)) {
350
  acceptSet.add(x);
351
  }
352
 
353
- const filtered = searchResults.filter((x) => acceptSet.has(x)).slice(0, this.targetResultCount);
354
  filtered.toString = searchResults.toString;
355
 
356
  const resultArray = filtered.map((x, i) => {
 
173
  }
174
 
175
  const it = this.fetchSearchResults(crawlerOptions.respondWith, r.web?.results, crawlOpts,
176
+ { ...crawlerOptions, cacheTolerance: crawlerOptions.cacheTolerance || this.pageCacheToleranceMs },
177
+ count,
178
  );
179
 
180
  if (!ctx.req.accepts('text/plain') && ctx.req.accepts('text/event-stream')) {
 
312
  searchResults?: WebSearchResult[],
313
  options?: ExtraScrappingOptions,
314
  crawlerOptions?: CrawlerOptions,
315
+ count?: number,
316
  ) {
317
  if (!searchResults) {
318
  return;
 
339
 
340
  const resultArray = await Promise.all(mapped) as FormattedPage[];
341
 
342
+ yield this.reOrganizeSearchResults(resultArray, count);
343
  }
344
  }
345
 
346
+ reOrganizeSearchResults(searchResults: FormattedPage[], count?: number) {
347
+ const targetResultCount = count || this.targetResultCount;
348
  const [qualifiedPages, unqualifiedPages] = _.partition(searchResults, (x) => this.pageQualified(x));
349
  const acceptSet = new Set(qualifiedPages);
350
 
351
+ const n = targetResultCount - qualifiedPages.length;
352
  for (const x of unqualifiedPages.slice(0, n >= 0 ? n : 0)) {
353
  acceptSet.add(x);
354
  }
355
 
356
+ const filtered = searchResults.filter((x) => acceptSet.has(x)).slice(0, targetResultCount);
357
  filtered.toString = searchResults.toString;
358
 
359
  const resultArray = filtered.map((x, i) => {