Skip to content

Commit

Permalink
Working elastic pagination info
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Aug 9, 2024
1 parent da30031 commit 19f854c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/Service/ElasticSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
24 changes: 18 additions & 6 deletions lib/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,28 @@ 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);

// $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'];
Expand Down Expand Up @@ -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
];
}

Expand Down

0 comments on commit 19f854c

Please sign in to comment.