Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 25, 2024
1 parent db1b4ed commit af4f342
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@

class TypesenseEngine extends Engine
{
/**
* The maximum amount of results that can be fetched per page.
*
* @var int
*/
private int $maxPerPage = 250;

/**
* The Typesense client instance.
*
Expand All @@ -37,7 +30,14 @@ class TypesenseEngine extends Engine
protected array $searchParameters = [];

/**
* The maximum amount of results that can be fetched for pagination.
* The maximum number of results that can be fetched per page.
*
* @var int
*/
private int $maxPerPage = 250;

/**
* The maximum number of results that can be fetched during pagination.
*
* @var int
*/
Expand Down Expand Up @@ -201,7 +201,7 @@ protected function deleteDocument(TypesenseCollection $collectionIndex, $modelId
*/
public function search(Builder $builder)
{
// If the limit exceeds Typesense's capabilities, perform a paginated search
// If the limit exceeds Typesense's capabilities, perform a paginated search...
if ($builder->limit >= $this->maxPerPage) {
return $this->performPaginatedSearch($builder);
}
Expand All @@ -226,17 +226,19 @@ protected function performPaginatedSearch(Builder $builder)
$page = 1;
$limit = min($builder->limit ?? $this->maxPerPage, $this->maxPerPage, $this->maxTotalResults);
$remainingResults = min($builder->limit ?? $this->maxTotalResults, $this->maxTotalResults);

$results = new Collection;

while ($remainingResults > 0) {
$search_res = $this->performSearch(
$builder,
$this->buildSearchParameters($builder, $page, $limit)
);

$results = $results->concat($search_res['hits'] ?? []);

if ($page === 1) {
$total_found = $search_res['found'] ?? 0;
$totalFound = $search_res['found'] ?? 0;
}

$remainingResults -= $limit;
Expand All @@ -250,7 +252,7 @@ protected function performPaginatedSearch(Builder $builder)
return [
'hits' => $results->all(),
'found' => $results->count(),
'out_of' => $total_found,
'out_of' => $totalFound,
'page' => 1,
'request_params' => $this->buildSearchParameters($builder, 1, $builder->limit ?? $this->maxPerPage),
];
Expand Down

0 comments on commit af4f342

Please sign in to comment.