Skip to content

Commit

Permalink
make pagination work with the Scout method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen-G committed Jun 15, 2021
1 parent d202cfa commit a192d10
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [2.2.0]

### Added
- Wildcard syntax

### Fixed
- Pagination now works with the default Laravel Scout `paginate()` method

## [2.1.1]

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function hits(): array

public function count(): int
{
return (int) $this->rawResults['hits']['total'];
return $this->rawResults['hits']['total']['value'];
}
}
5 changes: 2 additions & 3 deletions src/Infrastructure/Scout/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ public function search(Builder $builder): Results
*/
public function paginate(Builder $builder, $perPage, $page): Results
{
$limit = $perPage;
$offset = $limit * $perPage;
$offset = $perPage * ($page - 1);

$normalizedBuilder = ScoutSearchCommandBuilder::wrap($builder);
$normalizedBuilder->setOffset($offset);
$normalizedBuilder->setLimit($limit);
$normalizedBuilder->setLimit($perPage);
self::$lastQuery = $normalizedBuilder->buildQuery();
return $this->adapter->search($normalizedBuilder);
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function test_it_finds_all_items_if_no_queries_are_provided(): void
])
->andReturn([
'hits' => [
'total' => '1',
'total' => ['value' => 1],
'hits' => [$hit],
],
]);
Expand Down Expand Up @@ -95,7 +95,7 @@ public function test_it_accepts_must_should_filter_and_where_queries(): void
])
->andReturn([
'hits' => [
'total' => '2',
'total' => ['value' => 2],
'hits' => [
$this->hit(),
$this->hit(),
Expand Down Expand Up @@ -137,7 +137,7 @@ public function test_it_accepts_a_query_for_paginated_search(): void
])
->andReturn([
'hits' => [
'total' => '1',
'total' => ['value' => 1],
'hits' => [$this->hit()],
],
]);
Expand Down Expand Up @@ -175,7 +175,7 @@ public function test_it_accepts_a_sortable_query(): void
])
->andReturn([
'hits' => [
'total' => '1',
'total' => ['value' => 1],
'hits' => [$this->hit()],
],
]);
Expand Down Expand Up @@ -209,7 +209,7 @@ public function test_it_must_provide_offset_and_limit_for_pagination(): void
])
->andReturn([
'hits' => [
'total' => '1',
'total' => ['value' => 1],
'hits' => [$this->hit()],
],
]);
Expand Down Expand Up @@ -245,7 +245,7 @@ public function test_it_builds_with_default_fields(): void
])
->andReturn([
'hits' => [
'total' => '1',
'total' => ['value' => 1],
'hits' => [$this->hit()],
],
]);
Expand Down Expand Up @@ -280,7 +280,7 @@ public function test_it_adds_fields_to_query(): void
])
->andReturn([
'hits' => [
'total' => '1',
'total' => ['value' => 1],
'hits' => [$this->hit()],
],
]);
Expand Down

0 comments on commit a192d10

Please sign in to comment.