From 1e015af3c9de40dd0e815c6a7821979c835ead84 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 24 Mar 2018 19:04:18 +0000 Subject: [PATCH] Fixed incorrect search logic in last commit Incorrect cross-entity pagination could lead to hidden entities. --- app/Services/SearchService.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/Services/SearchService.php b/app/Services/SearchService.php index cb445eed1..056e1f077 100644 --- a/app/Services/SearchService.php +++ b/app/Services/SearchService.php @@ -64,7 +64,7 @@ class SearchService * @param string $searchString * @param string $entityType * @param int $page - * @param int $count + * @param int $count - Count of each entity to search, Total returned could can be larger and not guaranteed. * @return array[int, Collection]; */ public function searchEntities($searchString, $entityType = 'all', $page = 1, $count = 20) @@ -81,21 +81,26 @@ class SearchService $results = collect(); $total = 0; + $hasMore = false; foreach ($entityTypesToSearch as $entityType) { if (!in_array($entityType, $entityTypes)) { continue; } - $search = $this->searchEntityTable($terms, $entityType, $page, $count + 1); - $total += $this->searchEntityTable($terms, $entityType, $page, $count + 1, true); + $search = $this->searchEntityTable($terms, $entityType, $page, $count); + $entityTotal = $this->searchEntityTable($terms, $entityType, $page, $count, true); + if ($entityTotal > $page * $count) { + $hasMore = true; + } + $total += $entityTotal; $results = $results->merge($search); } return [ 'total' => $total, 'count' => count($results), - 'has_more' => $results->count() > $count, - 'results' => $results->sortByDesc('score')->slice(0, $count)->values() + 'has_more' => $hasMore, + 'results' => $results->sortByDesc('score')->values() ]; }