Skip to content

Commit 7aa68c6

Browse files
committed
feat: enhance song and user services to support regex search and improve logging
1 parent b070a76 commit 7aa68c6

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

server/src/song/song.service.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,29 @@ export class SongService {
478478
const sortOrder = order ? 1 : -1;
479479

480480
const songs = await this.songModel
481-
.find({ $text: { $search: query }, category: category }) // Ensure a text index is created
481+
.find({
482+
$or: [
483+
{ originalAuthor: { $regex: query, $options: 'i' } },
484+
{ title: { $regex: query, $options: 'i' } },
485+
{ description: { $regex: query, $options: 'i' } },
486+
],
487+
category: category,
488+
})
482489
.select('title category thumbnailUrl likeCount')
483490
.sort({ [sort]: sortOrder })
484491
.skip(skip)
485492
.limit(limit);
486493

487-
const total = await this.songModel.countDocuments();
494+
const total = await this.songModel.countDocuments({
495+
originalAuthor: { $regex: query, $options: 'i' },
496+
title: { $regex: query, $options: 'i' },
497+
description: { $regex: query, $options: 'i' },
498+
category: category,
499+
});
500+
501+
this.logger.debug(
502+
`Retrieved songs: ${songs.length} documents, with total: ${total}`,
503+
);
488504

489505
return {
490506
songs: await this.songModel.populate(songs, {

server/src/user/user.service.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
1+
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
22
import { InjectModel } from '@nestjs/mongoose';
33
import { PageQueryDTO } from '@shared/validation/common/dto/PageQuery.dto';
44
import { SearchQueryDTO } from '@shared/validation/common/dto/SearchQuery.dto';
@@ -13,6 +13,7 @@ import { User, UserDocument } from './entity/user.entity';
1313

1414
@Injectable()
1515
export class UserService {
16+
private readonly logger = new Logger(UserService.name);
1617
constructor(@InjectModel(User.name) private userModel: Model<User>) {}
1718

1819
public async create(user_registered: CreateUser) {
@@ -73,14 +74,26 @@ export class UserService {
7374

7475
const users = await this.userModel
7576
.find({
76-
$text: { $search: query },
77+
$or: [
78+
{ username: { $regex: query, $options: 'i' } },
79+
{ publicName: { $regex: query, $options: 'i' } },
80+
{ description: { $regex: query, $options: 'i' } },
81+
],
7782
})
7883
.select('username publicName email profileImage')
7984
.sort({ [sort]: sortOrder })
8085
.skip(skip)
8186
.limit(limit);
8287

83-
const total = await this.userModel.countDocuments();
88+
const total = await this.userModel.countDocuments({
89+
username: { $regex: query, $options: 'i' },
90+
publicName: { $regex: query, $options: 'i' },
91+
description: { $regex: query, $options: 'i' },
92+
});
93+
94+
this.logger.debug(
95+
`Retrived users: ${users.length} documents, with total: ${total}`,
96+
);
8497

8598
return {
8699
users,
@@ -196,7 +209,7 @@ export class UserService {
196209
}
197210

198211
public async createSearchIndexes() {
199-
await this.userModel.collection.createIndex(
212+
return await this.userModel.collection.createIndex(
200213
{
201214
username: 'text',
202215
publicName: 'text',

0 commit comments

Comments
 (0)