feat: add min parameter to search query

This commit is contained in:
Vjacheslav Trushkin 2023-05-22 08:57:19 +03:00
parent bcad340030
commit c53a1e7a82
4 changed files with 16 additions and 3 deletions

View File

@ -69,6 +69,7 @@ export function search(
return newItem;
};
const limit = params.limit;
const softLimit = params.softLimit;
interface ExtendedSearchKeywordsEntry extends SearchKeywordsEntry {
// Add prefixes cache to avoid re-calculating it for every partial keyword
@ -256,9 +257,9 @@ export function search(
// Extract results
const results: string[] = [];
const prefixes: Set<string> = new Set();
for (let i = 0; i < allMatches.length && results.length < limit; i++) {
for (let i = 0; i < allMatches.length && (softLimit || results.length < limit); i++) {
const { names } = allMatches[i];
for (let j = 0; j < names.length && results.length < limit; j++) {
for (let j = 0; j < names.length && (softLimit || results.length < limit); j++) {
const name = names[j];
results.push(name);
prefixes.add(name.split(':').shift() as string);

View File

@ -54,6 +54,15 @@ export function generateAPIv2SearchResponse(query: FastifyRequest['query'], res:
}
params.limit = Math.max(minSearchLimit, Math.min(limit, maxSearchLimit));
}
if (v2Query.min) {
const limit = parseInt(v2Query.min);
if (!limit) {
res.send(400);
return;
}
params.limit = Math.max(minSearchLimit, Math.min(limit, maxSearchLimit));
params.softLimit = true;
}
let start = 0;
if (v2Query.start) {

View File

@ -50,6 +50,7 @@ export interface SearchParams {
// Search results limit
limit: number;
softLimit?: boolean; // True if limit can be exceeded
// Toggle partial matches
partial?: boolean;

View File

@ -103,7 +103,9 @@ export interface APIv2SearchParams extends APIv2CommonParams {
query: SearchQuery;
// Maximum number of items in response
limit?: number;
// If `min` is set, `limit` is ignored
limit?: number; // Hard limit. Number of results will not exceed `limit`.
min?: number; // Soft limit. Number of results can exceed `limit` if function already retrieved more icons.
// Start index for results
start?: number;