@@ -477,26 +477,93 @@ export class SongService {
477
477
const skip = ( page - 1 ) * limit ;
478
478
const sortOrder = order ? 1 : - 1 ;
479
479
480
- const songs = await this . songModel
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
- } )
489
- . select ( 'title category thumbnailUrl likeCount' )
490
- . sort ( { [ sort ] : sortOrder } )
491
- . skip ( skip )
492
- . limit ( limit ) ;
480
+ const songs : SongViewDto [ ] = await this . songModel . aggregate ( [
481
+ {
482
+ /**
483
+ $search: {
484
+ index: 'song_search_index',
485
+ text: {
486
+ query: query,
487
+ },
488
+ },
489
+ */
490
+ $match : {
491
+ $or : [
492
+ { originalAuthor : { $regex : query , $options : 'i' } } ,
493
+ { title : { $regex : query , $options : 'i' } } ,
494
+ { description : { $regex : query , $options : 'i' } } ,
495
+ ] ,
496
+ //category: category,
497
+ } ,
498
+ } ,
499
+ {
500
+ $sort : { [ sort ] : sortOrder } ,
501
+ } ,
502
+ {
503
+ $skip : skip ,
504
+ } ,
505
+ {
506
+ $limit : limit ,
507
+ } ,
508
+ {
509
+ $lookup : {
510
+ from : 'users' , // The collection to join
511
+ localField : 'uploader' , // The field from the input documents (username)
512
+ foreignField : 'username' , // The field from the documents of the "from" collection (username)
513
+ as : 'uploader' , // The name of the new array field to add to the input documents
514
+ } ,
515
+ } ,
516
+ {
517
+ $unwind : '$uploader' , // Unwind the array to include the user document directly
518
+ } ,
519
+ {
520
+ $project : {
521
+ publicId : 1 ,
522
+ createdAt : 1 ,
523
+ thumbnailUrl : 1 ,
524
+ playCount : 1 ,
525
+ downloadCount : 1 ,
526
+ likeCount : 1 ,
527
+ allowDownload : 1 ,
528
+ title : 1 ,
529
+ originalAuthor : 1 ,
530
+ description : 1 ,
531
+ category : 1 ,
532
+ license : 1 ,
533
+ customInstruments : 1 ,
534
+ fileSize : 1 ,
535
+ stats : 1 ,
536
+ 'uploader.username' : 1 ,
537
+ 'uploader.profileImage' : 1 ,
538
+ } ,
539
+ } ,
540
+ ] ) ;
493
541
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
- } ) ;
542
+ const totalResult = await this . songModel . aggregate ( [
543
+ {
544
+ /**
545
+ $search: {
546
+ index: 'song_search_index',
547
+ text: {
548
+ query: query,
549
+ },
550
+ },
551
+ */
552
+ $match : {
553
+ $or : [
554
+ { originalAuthor : { $regex : query , $options : 'i' } } ,
555
+ { title : { $regex : query , $options : 'i' } } ,
556
+ { description : { $regex : query , $options : 'i' } } ,
557
+ ] ,
558
+ category : category ,
559
+ } ,
560
+ } ,
561
+ {
562
+ $count : 'total' ,
563
+ } ,
564
+ ] ) ;
565
+
566
+ const total = totalResult . length > 0 ? totalResult [ 0 ] . total : 0 ;
500
567
501
568
this . logger . debug (
502
569
`Retrieved songs: ${ songs . length } documents, with total: ${ total } ` ,
0 commit comments