From a192d1072e92a181ec6b93d1e69bfbfcf7d6883f Mon Sep 17 00:00:00 2001 From: Jeroen Date: Tue, 15 Jun 2021 16:21:15 +0200 Subject: [PATCH] make pagination work with the Scout method --- changelog.md | 5 +++++ src/Application/Results.php | 2 +- src/Infrastructure/Scout/ElasticEngine.php | 5 ++--- tests/Unit/FinderTest.php | 14 +++++++------- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/changelog.md b/changelog.md index 8a4196e..6c6d929 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/Application/Results.php b/src/Application/Results.php index db77ba3..83a299e 100644 --- a/src/Application/Results.php +++ b/src/Application/Results.php @@ -22,6 +22,6 @@ public function hits(): array public function count(): int { - return (int) $this->rawResults['hits']['total']; + return $this->rawResults['hits']['total']['value']; } } diff --git a/src/Infrastructure/Scout/ElasticEngine.php b/src/Infrastructure/Scout/ElasticEngine.php index c7e6e2b..a7a5ebd 100644 --- a/src/Infrastructure/Scout/ElasticEngine.php +++ b/src/Infrastructure/Scout/ElasticEngine.php @@ -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); } diff --git a/tests/Unit/FinderTest.php b/tests/Unit/FinderTest.php index c81b813..d2c16c9 100644 --- a/tests/Unit/FinderTest.php +++ b/tests/Unit/FinderTest.php @@ -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], ], ]); @@ -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(), @@ -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()], ], ]); @@ -175,7 +175,7 @@ public function test_it_accepts_a_sortable_query(): void ]) ->andReturn([ 'hits' => [ - 'total' => '1', + 'total' => ['value' => 1], 'hits' => [$this->hit()], ], ]); @@ -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()], ], ]); @@ -245,7 +245,7 @@ public function test_it_builds_with_default_fields(): void ]) ->andReturn([ 'hits' => [ - 'total' => '1', + 'total' => ['value' => 1], 'hits' => [$this->hit()], ], ]); @@ -280,7 +280,7 @@ public function test_it_adds_fields_to_query(): void ]) ->andReturn([ 'hits' => [ - 'total' => '1', + 'total' => ['value' => 1], 'hits' => [$this->hit()], ], ]);