diff --git a/src/structures/schemas/structure.schema.ts b/src/structures/schemas/structure.schema.ts index 254774be33195a471a16dc4816382fb8cfc1f2fb..aaebe759fc5964fc857d78effa21e08fda39aeeb 100644 --- a/src/structures/schemas/structure.schema.ts +++ b/src/structures/schemas/structure.schema.ts @@ -161,3 +161,5 @@ export class Structure { } export const StructureSchema = SchemaFactory.createForClass(Structure); + +StructureSchema.index({ '$**': 'text' }); diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 462e4adf25fc5bf2b3923184da27f8757705a485..a60dea001cee4fef71c2e14408fcafd19290a48d 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Post } from '@nestjs/common'; +import { Body, Controller, Get, Param, Post } from '@nestjs/common'; import { CreateStructureDto } from './dto/create-structure.dto'; import { Structure } from './schemas/structure.schema'; import { StructuresService } from './structures.service'; @@ -17,6 +17,11 @@ export class StructuresController { return this.structureService.findAll(); } + @Get(':text') + async search(@Param('text') text: string): Promise<Structure[]> { + return this.structureService.search(text); + } + @Get('count') async countCategories(): Promise<Array<{ id: string; count: number }>> { const data = await Promise.all([ diff --git a/src/structures/structures.service.ts b/src/structures/structures.service.ts index 5526a29fbbf2c2220725a76994fad9d1b8773c4c..bd3f4f9687a7415bf872efa5bbbfad8b72b73aee 100644 --- a/src/structures/structures.service.ts +++ b/src/structures/structures.service.ts @@ -13,6 +13,10 @@ export class StructuresService { return createdStructure.save(); } + async search(searchString: string): Promise<Structure[]> { + return this.structureModel.find({ $text: { $search: searchString } }).exec(); + } + async findAll(): Promise<Structure[]> { return this.structureModel.find().exec(); }