diff --git a/src/Models/SearchQuery.php b/src/Models/SearchQuery.php index 98c392326..60218847c 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,10 +26,23 @@ 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) - ->orWhereNotNull('redirect') + ->where( + fn ($subQuery) => $subQuery + ->where(fn ($subSubQuery) => $subSubQuery->where('num_results', '>', 0)->where('popularity', '>', $minPopularity)) + ->orWhereNotNull('redirect') ); } }