Skip to content
Snippets Groups Projects
Commit 609b51b8 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

feat: add full text search

parent 0673e49a
No related branches found
No related tags found
No related merge requests found
...@@ -161,3 +161,5 @@ export class Structure { ...@@ -161,3 +161,5 @@ export class Structure {
} }
export const StructureSchema = SchemaFactory.createForClass(Structure); export const StructureSchema = SchemaFactory.createForClass(Structure);
StructureSchema.index({ '$**': 'text' });
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 { CreateStructureDto } from './dto/create-structure.dto';
import { Structure } from './schemas/structure.schema'; import { Structure } from './schemas/structure.schema';
import { StructuresService } from './structures.service'; import { StructuresService } from './structures.service';
...@@ -17,6 +17,11 @@ export class StructuresController { ...@@ -17,6 +17,11 @@ export class StructuresController {
return this.structureService.findAll(); return this.structureService.findAll();
} }
@Get(':text')
async search(@Param('text') text: string): Promise<Structure[]> {
return this.structureService.search(text);
}
@Get('count') @Get('count')
async countCategories(): Promise<Array<{ id: string; count: number }>> { async countCategories(): Promise<Array<{ id: string; count: number }>> {
const data = await Promise.all([ const data = await Promise.all([
......
...@@ -13,6 +13,10 @@ export class StructuresService { ...@@ -13,6 +13,10 @@ export class StructuresService {
return createdStructure.save(); return createdStructure.save();
} }
async search(searchString: string): Promise<Structure[]> {
return this.structureModel.find({ $text: { $search: searchString } }).exec();
}
async findAll(): Promise<Structure[]> { async findAll(): Promise<Structure[]> {
return this.structureModel.find().exec(); return this.structureModel.find().exec();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment