diff --git a/src/routes/companies/service.ts b/src/routes/companies/service.ts index 561141b..e211afd 100644 --- a/src/routes/companies/service.ts +++ b/src/routes/companies/service.ts @@ -251,12 +251,14 @@ export interface GetUserModel { * @param options.sortBy - The field to sort companies by. * @param options.sortOrder - The order to sort companies by. * @param options.status - The status of the companies. + * @param options.q - The query to search companies by. * @returns The filter to get companies. */ function buildFilter({ cursor, includePrivateCompanies = false, onlyRecommended = false, + q, sortBy = "name", sortOrder = "asc", status @@ -276,6 +278,8 @@ function buildFilter({ ]; } + if (q) filter["$text"] = { $search: q }; + if (includePrivateCompanies) { // Include both public and private companies } else filter["privateCompany"] = { $ne: true }; diff --git a/src/schema-mongodb/companies.ts b/src/schema-mongodb/companies.ts index 5bcce7c..2604089 100644 --- a/src/schema-mongodb/companies.ts +++ b/src/schema-mongodb/companies.ts @@ -60,6 +60,8 @@ export const CompanySchema = new mongoose.Schema( { versionKey: false } ); +CompanySchema.index({ description: "text", name: "text" }); + /** * Creates a company model. * @param connection - The mongoose connection. diff --git a/src/schema/get-all-options.ts b/src/schema/get-all-options.ts index 128bd45..f6a9ca7 100644 --- a/src/schema/get-all-options.ts +++ b/src/schema/get-all-options.ts @@ -12,6 +12,7 @@ export interface GetCompaniesOptions { readonly limit?: number | undefined; readonly offset?: number | undefined; readonly onlyRecommended?: boolean | undefined; + readonly q?: string | null | undefined; readonly sortBy?: "createdAt" | "foundedAt" | "name" | undefined; readonly sortOrder?: "asc" | "desc" | undefined; readonly status?: CompanyStatus | undefined; diff --git a/src/schema/get-all-options.validation.ts b/src/schema/get-all-options.validation.ts index 02ef72a..e6bff83 100644 --- a/src/schema/get-all-options.validation.ts +++ b/src/schema/get-all-options.validation.ts @@ -26,6 +26,7 @@ export const GetCompaniesOptionsValidationSchema = zod.object({ limit: LimitValidationSchema, offset: OffsetValidationSchema, onlyRecommended: preprocessBoolean(zod.boolean()).optional(), + q: zod.string().optional(), sortBy: zod .union([ zod.literal("createdAt"),