Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/Models/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Comment on lines +33 to +38

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i remember correctly Mysql behaves badly with offsets.
Loading the first 10 000 records and throwing away 9 999 of them which is heavy on the database
https://medium.com/@twly.tw/efficient-pagination-in-mysql-avoiding-the-pitfalls-of-limit-offset-aef28f321065

);

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')
);
}
}
Loading