diff --git a/server/src/search/search.service.ts b/server/src/search/search.service.ts index a8abf278..e124318d 100644 --- a/server/src/search/search.service.ts +++ b/server/src/search/search.service.ts @@ -20,8 +20,6 @@ export class SearchService { } public async search(queryBody: SearchQueryDTO) { - this.logger.debug(`Searching for ${JSON.stringify(queryBody)}`); - queryBody.query = (queryBody.query || '').trim().toLowerCase(); const { @@ -29,12 +27,25 @@ export class SearchService { page = 1, limit = 10, sort, - order = false, + order, category, searchSongs, searchUsers, } = queryBody; + this.logger.debug( + `Searching for ${JSON.stringify({ + query, + page, + limit, + sort, + order, + category, + searchSongs, + searchUsers, + })}`, + ); + const { users, total: totalUsers } = searchUsers ? await this.userService.search({ ...queryBody, diff --git a/server/src/song/song.service.ts b/server/src/song/song.service.ts index f72f434b..8067a3b4 100644 --- a/server/src/song/song.service.ts +++ b/server/src/song/song.service.ts @@ -479,25 +479,19 @@ export class SongService { const songs: SongViewDto[] = await this.songModel.aggregate([ { - /** - $search: { - index: 'song_search_index', - text: { - query: query, - }, - }, - */ $match: { - $or: [ - { originalAuthor: { $regex: query, $options: 'i' } }, - { title: { $regex: query, $options: 'i' } }, - { description: { $regex: query, $options: 'i' } }, - ], - //category: category, + $text: { + $search: query, + $caseSensitive: false, + $diacriticSensitive: false, + }, + ...(category && { category: category }), }, }, { - $sort: { [sort]: sortOrder }, + $sort: { + [sort]: sortOrder, + }, }, { $skip: skip, @@ -550,12 +544,12 @@ export class SongService { }, */ $match: { - $or: [ - { originalAuthor: { $regex: query, $options: 'i' } }, - { title: { $regex: query, $options: 'i' } }, - { description: { $regex: query, $options: 'i' } }, - ], - category: category, + $text: { + $search: query, + $caseSensitive: false, // Case-insensitive search + $diacriticSensitive: false, // Diacritic-insensitive search + }, + ...(category && { category: category }), }, }, { diff --git a/server/src/user/user.service.ts b/server/src/user/user.service.ts index ab83cc6f..56181c31 100644 --- a/server/src/user/user.service.ts +++ b/server/src/user/user.service.ts @@ -77,23 +77,12 @@ export class UserService { profileImage: string; }[] = await this.userModel.aggregate([ { - /* - $search: { - index: 'user_search_index', - text: { - query: query, - path: { - wildcard: '*', - }, - }, - }, - */ $match: { - $or: [ - { username: { $regex: query, $options: 'i' } }, - { publicName: { $regex: query, $options: 'i' } }, - // { description: { $regex: query, $options: 'i' } }, - ], + $text: { + $search: query, + $caseSensitive: false, + $diacriticSensitive: false, + }, }, }, { @@ -103,31 +92,24 @@ export class UserService { }, }, { - $sort: { [sort]: sortOrder }, // Sort the results + $sort: { [sort]: sortOrder }, }, { - $skip: skip, // Skip the first 'skip' results + $skip: skip, }, { - $limit: limit, // Limit the results to 'limit' + $limit: limit, }, ]); const totalResult = await this.userModel.aggregate([ { - /* - $search: { - index: 'user_search_index', - text: { - query: query, - }, - },*/ $match: { - $or: [ - { username: { $regex: query, $options: 'i' } }, - { publicName: { $regex: query, $options: 'i' } }, - // { description: { $regex: query, $options: 'i' } }, - ], + $text: { + $search: query, + $caseSensitive: false, + $diacriticSensitive: false, + }, }, }, { diff --git a/shared/validation/common/dto/SearchQuery.dto.ts b/shared/validation/common/dto/SearchQuery.dto.ts index 6f5f8694..e78167b0 100644 --- a/shared/validation/common/dto/SearchQuery.dto.ts +++ b/shared/validation/common/dto/SearchQuery.dto.ts @@ -15,7 +15,6 @@ export class SearchQueryDTO { @ApiProperty({ example: 'Nirvana - Dumb', description: 'Natural language query.', - required: false, }) query?: string; @@ -32,9 +31,8 @@ export class SearchQueryDTO { @ApiProperty({ example: 1, description: 'Page number.', - required: false, }) - page?: number = 1; + page?: number; @IsNumber({ maxDecimalPlaces: 0 }) @Min(1) @@ -42,7 +40,6 @@ export class SearchQueryDTO { @ApiProperty({ example: 20, description: 'Number of results per page.', - required: false, }) limit?: number = 20; @@ -53,7 +50,7 @@ export class SearchQueryDTO { description: 'Sort field.', required: false, }) - sort?: string = 'createdAt'; + sort?: string; @IsBoolean() @Transform(({ value }) => value === 'true') @@ -62,25 +59,23 @@ export class SearchQueryDTO { description: 'Sort in ascending order if true, descending if false.', required: false, }) - order?: boolean = false; + order?: boolean; @IsOptional() @IsBoolean() @ApiProperty({ example: true, description: 'Search Users.', - required: false, }) - searchUsers?: boolean = true; + searchUsers?: boolean; @IsOptional() @IsBoolean() @ApiProperty({ example: true, description: 'Search Songs.', - required: false, }) - searchSongs?: boolean = true; + searchSongs?: boolean; constructor(partial: Partial) { Object.assign(this, partial);