From e66bf44341175bc5a10fbf7dfa00b351e76136c2 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Thu, 28 Jul 2022 15:10:33 +0000
Subject: [PATCH] fix(search-episodes): add fallback sql query using LIKE for
 search query with less than 4 characters

fixes #236
---
 modules/Admin/Controllers/EpisodeController.php | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/modules/Admin/Controllers/EpisodeController.php b/modules/Admin/Controllers/EpisodeController.php
index 9fafb6e8fd..d58be7ba42 100644
--- a/modules/Admin/Controllers/EpisodeController.php
+++ b/modules/Admin/Controllers/EpisodeController.php
@@ -67,9 +67,19 @@ class EpisodeController extends BaseController
         $query = $this->request->getGet('q');
 
         if ($query !== null && $query !== '') {
-            $episodes = (new EpisodeModel())
-                ->where('podcast_id', $this->podcast->id)
-                ->where("MATCH (title, description_markdown) AGAINST ('{$query}')");
+            // Default value for MySQL Full-Text Search's minimum length of words is 4.
+            // Use LIKE operator as a fallback.
+            if (strlen($query) < 4) {
+                $episodes = (new EpisodeModel())
+                    ->where('podcast_id', $this->podcast->id)
+                    ->like('title', $query)
+                    ->orLike('description_markdown', $query)
+                    ->orderBy('created_at', 'desc');
+            } else {
+                $episodes = (new EpisodeModel())
+                    ->where('podcast_id', $this->podcast->id)
+                    ->where("MATCH (title, description_markdown) AGAINST ('{$query}')");
+            }
         } else {
             $episodes = (new EpisodeModel())
                 ->where('podcast_id', $this->podcast->id)
-- 
GitLab