From 19f854c8bbc57a4a3c9c7df4d2a5c451123d006a Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 9 Aug 2024 09:54:22 +0200 Subject: [PATCH] Working elastic pagination info --- lib/Service/ElasticSearchService.php | 5 +++-- lib/Service/SearchService.php | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/Service/ElasticSearchService.php b/lib/Service/ElasticSearchService.php index fd02b699..ef16edd4 100644 --- a/lib/Service/ElasticSearchService.php +++ b/lib/Service/ElasticSearchService.php @@ -226,17 +226,18 @@ public function mapAggregationResults(array $result): array }//end mapAggregationResults() - public function searchObject(array $filters, array $config): array + public function searchObject(array $filters, array $config, int &$totalResults = 0): array { $body = $this->parseFilters(filters: $filters); $client = $this->getClient(config: $config); - $result = $client->search(params: [ 'index' => $config['index'], 'body' => $body ]); + $totalResults = $result['hits']['total']['value']; + $return = ['results' => array_map(callback: [$this, 'formatResults'], array: $result['hits']['hits'])]; if(isset($result['aggregations']) === true) { $return['facets'] = array_map([$this, 'mapAggregationResults'], $result['aggregations']); diff --git a/lib/Service/SearchService.php b/lib/Service/SearchService.php index 3feb75f8..57d495f9 100644 --- a/lib/Service/SearchService.php +++ b/lib/Service/SearchService.php @@ -83,8 +83,12 @@ public function search(array $parameters, array $elasticConfig, array $dbConfig, $localResults['results'] = []; $localResults['facets'] = []; + $totalResults = 0; + $limit = isset($parameters['.limit']) === true ? $parameters['.limit'] : 30; + $page = isset($parameters['.page']) === true ? $parameters['.page'] : 1; + if($elasticConfig['location'] !== '') { - $localResults = $this->elasticService->searchObject($parameters, $elasticConfig); + $localResults = $this->elasticService->searchObject(filters: $parameters, config: $elasticConfig, totalResults: $totalResults,); } $directory = $this->directoryService->listDirectory(limit: 1000); @@ -92,7 +96,15 @@ public function search(array $parameters, array $elasticConfig, array $dbConfig, // $directory = $this->objectService->findObjects(filters: ['_schema' => 'directory'], config: $dbConfig); if(count($directory) === 0) { - return $localResults; + return [ + 'results' => $localResults['results'], + 'facets' => $localResults['facets'], + 'count' => count($localResults['results']), + 'limit' => $limit, + 'page' => $page, + 'pages' => ceil($totalResults / $limit), + 'total' => $totalResults + ]; } $results = $localResults['results']; @@ -147,10 +159,10 @@ public function search(array $parameters, array $elasticConfig, array $dbConfig, 'results' => $results, 'facets' => $aggregations, 'count' => count($results), - 'limit' => 30, - 'page' => 1, - 'pages' => 1, - 'total' => 10 + 'limit' => $limit, + 'page' => $page, + 'pages' => ceil($totalResults / $limit), + 'total' => $totalResults ]; }