Procházet zdrojové kódy

fix(video): improve regex handling in keyword match filter for better search accuracy

Dave před 1 měsícem
rodič
revize
dcfab1f03d

+ 18 - 5
apps/box-mgnt-api/src/mgnt-backend/feature/video-media/video-media.service.ts

@@ -58,8 +58,8 @@ export class VideoMediaService {
         }),
       ]);
     } else {
-      const regex = new RegExp(this.escapeRegex(keyword), 'i');
-      const matchFilter = this.buildKeywordMatchFilter(baseWhere, regex);
+      const regexSource = this.escapeRegex(keyword);
+      const matchFilter = this.buildKeywordMatchFilter(baseWhere, regexSource);
 
       // Prisma Mongo cannot express regex searches inside array elements, so we fall back to a raw aggregate that uses sanitizedSecondTags.
       const countRes = (await this.prisma.$runCommandRaw({
@@ -140,17 +140,30 @@ export class VideoMediaService {
 
   private buildKeywordMatchFilter(
     baseFilter: Record<string, any>,
-    regex: RegExp,
+    regexSource: string,
   ): Record<string, any> {
     const matchFilter = { ...baseFilter };
+
     if (Array.isArray(matchFilter.$and)) {
       matchFilter.$and = [...matchFilter.$and];
     }
 
     const keywordClause = {
       $or: [
-        { title: { $regex: regex } },
-        { sanitizedSecondTags: { $elemMatch: { $regex: regex } } },
+        {
+          title: {
+            $regex: regexSource,
+            $options: 'i',
+          },
+        },
+        {
+          sanitizedSecondTags: {
+            $elemMatch: {
+              $regex: regexSource,
+              $options: 'i',
+            },
+          },
+        },
       ],
     };