From f7d276110584d7fe1fa10a91472979d6f171b189 Mon Sep 17 00:00:00 2001 From: Jade Date: Thu, 18 Jun 2026 10:39:28 +0200 Subject: [PATCH 1/2] Limit search query count when there are a lot of them --- src/Models/SearchQuery.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Models/SearchQuery.php b/src/Models/SearchQuery.php index 98c392326..087e88e4c 100644 --- a/src/Models/SearchQuery.php +++ b/src/Models/SearchQuery.php @@ -3,6 +3,8 @@ namespace Rapidez\Core\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\DB; use Rapidez\Core\Models\Scopes\IsActiveScope; use Rapidez\Core\Models\Traits\Searchable; @@ -24,9 +26,22 @@ protected static function booting() protected function makeAllSearchableUsing(Builder $query) { + // Decide on a minimum popularity to make sure we don't index too much. + // Simply using an orderBy with a limit on the query doesn't work as the limit gets stripped off. + $minPopularity = Cache::driver('array')->rememberForever( + 'minPopularity', + fn() => DB::table('search_query') + ->orderByDesc('popularity') + ->limit(1) + ->offset(10000) + ->first() + ->popularity ?? 0 + ); + return $query - ->where(fn ($subQuery) => $subQuery - ->where('num_results', '>', 0) + ->where( + fn($subQuery) => $subQuery + ->where(fn($subSubQuery) => $subSubQuery->where('num_results', '>', 0)->where('popularity', '>', $minPopularity)) ->orWhereNotNull('redirect') ); } From 11db646f30fc2358aa2c1b3021d653e15c8d9f29 Mon Sep 17 00:00:00 2001 From: Jade-GG Date: Thu, 18 Jun 2026 08:40:07 +0000 Subject: [PATCH 2/2] Apply fixes from Duster --- src/Models/SearchQuery.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Models/SearchQuery.php b/src/Models/SearchQuery.php index 087e88e4c..60218847c 100644 --- a/src/Models/SearchQuery.php +++ b/src/Models/SearchQuery.php @@ -30,7 +30,7 @@ protected function makeAllSearchableUsing(Builder $query) // Simply using an orderBy with a limit on the query doesn't work as the limit gets stripped off. $minPopularity = Cache::driver('array')->rememberForever( 'minPopularity', - fn() => DB::table('search_query') + fn () => DB::table('search_query') ->orderByDesc('popularity') ->limit(1) ->offset(10000) @@ -40,9 +40,9 @@ protected function makeAllSearchableUsing(Builder $query) return $query ->where( - fn($subQuery) => $subQuery - ->where(fn($subSubQuery) => $subSubQuery->where('num_results', '>', 0)->where('popularity', '>', $minPopularity)) - ->orWhereNotNull('redirect') + fn ($subQuery) => $subQuery + ->where(fn ($subSubQuery) => $subSubQuery->where('num_results', '>', 0)->where('popularity', '>', $minPopularity)) + ->orWhereNotNull('redirect') ); } }