fixed an issue with scout pagination and typesense

This commit is contained in:
pushrbx 2022-06-22 20:26:09 +01:00
parent c42278bce8
commit 5569da4bb2
2 changed files with 17 additions and 2 deletions

View File

@ -190,10 +190,10 @@ abstract class SearchQueryBuilder implements SearchQueryBuilderService
if ($this->isSearchIndexUsed() && $this->isScoutBuilder($results)) { if ($this->isSearchIndexUsed() && $this->isScoutBuilder($results)) {
$paginated = $results $paginated = $results
->take($limit)
->paginate( ->paginate(
$limit, $limit,
null, "page",
null,
$page $page
); );
} else { } else {

View File

@ -7,6 +7,16 @@ use Typesense\Documents;
class TypeSenseScoutSearchService implements ScoutSearchService class TypeSenseScoutSearchService implements ScoutSearchService
{ {
private int $maxItemsPerPage;
public function __construct()
{
$this->maxItemsPerPage = (int) env('MAX_RESULTS_PER_PAGE', 25);
if ($this->maxItemsPerPage > 250) {
$this->maxItemsPerPage = 250;
}
}
/** /**
* Executes a search operation via Laravel Scout on the provided model class. * Executes a search operation via Laravel Scout on the provided model class.
* @param object|string $modelClass * @param object|string $modelClass
@ -22,6 +32,11 @@ class TypeSenseScoutSearchService implements ScoutSearchService
// which will make Typesense consider all variations of prefixes and typo corrections of the words // which will make Typesense consider all variations of prefixes and typo corrections of the words
// in the query exhaustively, without stopping early when enough results are found. // in the query exhaustively, without stopping early when enough results are found.
$options['exhaustive_search'] = true; $options['exhaustive_search'] = true;
if (array_key_exists('per_page', $options) && $options['per_page'] > 250) {
$options['per_page'] = min($this->maxItemsPerPage, 250);
}
$modelInstance = new $modelClass; $modelInstance = new $modelClass;
// get the weights of the query_by fields, if they are provided by the model. // get the weights of the query_by fields, if they are provided by the model.
if ($modelInstance instanceof JikanApiSearchableModel) { if ($modelInstance instanceof JikanApiSearchableModel) {