Skip to content

Commit

Permalink
feat: refactor search services to use text search and improve logging…
Browse files Browse the repository at this point in the history
… in SearchQueryDTO
  • Loading branch information
tomast1337 committed Jan 24, 2025
1 parent 2af37e7 commit e4b3a80
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 65 deletions.
17 changes: 14 additions & 3 deletions server/src/search/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,32 @@ export class SearchService {
}

public async search(queryBody: SearchQueryDTO) {
this.logger.debug(`Searching for ${JSON.stringify(queryBody)}`);

queryBody.query = (queryBody.query || '').trim().toLowerCase();

const {
query,
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,
Expand Down
36 changes: 15 additions & 21 deletions server/src/song/song.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 }),
},
},
{
Expand Down
44 changes: 13 additions & 31 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
{
Expand All @@ -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,
},
},
},
{
Expand Down
15 changes: 5 additions & 10 deletions shared/validation/common/dto/SearchQuery.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class SearchQueryDTO {
@ApiProperty({
example: 'Nirvana - Dumb',
description: 'Natural language query.',
required: false,
})
query?: string;

Expand All @@ -32,17 +31,15 @@ export class SearchQueryDTO {
@ApiProperty({
example: 1,
description: 'Page number.',
required: false,
})
page?: number = 1;
page?: number;

@IsNumber({ maxDecimalPlaces: 0 })
@Min(1)
@Max(100)
@ApiProperty({
example: 20,
description: 'Number of results per page.',
required: false,
})
limit?: number = 20;

Expand All @@ -53,7 +50,7 @@ export class SearchQueryDTO {
description: 'Sort field.',
required: false,
})
sort?: string = 'createdAt';
sort?: string;

@IsBoolean()
@Transform(({ value }) => value === 'true')
Expand All @@ -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<SearchQueryDTO>) {
Object.assign(this, partial);
Expand Down

0 comments on commit e4b3a80

Please sign in to comment.