diff --git a/src/Service/AlgoliaQuerier.php b/src/Service/AlgoliaQuerier.php index 41ce924..b9719e8 100644 --- a/src/Service/AlgoliaQuerier.php +++ b/src/Service/AlgoliaQuerier.php @@ -3,6 +3,7 @@ namespace Wilr\SilverStripe\Algolia\Service; use Exception; +use Psr\Log\LoggerInterface; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\PaginatedList; @@ -13,19 +14,39 @@ class AlgoliaQuerier { /** + * @param string $selectedIndex * @param string $query * @param array $searchParameters * * @return PaginatedList */ - public function fetchResults($selectedIndex, $query, $searchParameters = []) + public function fetchResults($selectedIndex = null, $query = '', $searchParameters = []) { $service = Injector::inst()->get(AlgoliaService::class); + $results = false; + + if (!$selectedIndex) { + if (!function_exists('array_key_first')) { + function array_key_first(array $arr) + { + foreach ($arr as $key => $unused) { + return $key; + } + return null; + } + } - $selectedIndex = $service->environmentizeIndex($selectedIndex); - $index = $service->getClient()->initIndex($selectedIndex); - $results = $index->search($query, $searchParameters); - + $selectedIndex = array_key_first($service->indexes); + } + + try { + $selectedIndex = $service->environmentizeIndex($selectedIndex); + $index = $service->getClient()->initIndex($selectedIndex); + $results = $index->search($query, $searchParameters); + } catch (Exception $e) { + Injector::inst()->get(LoggerInterface::class)->error($e); + } + $records = ArrayList::create(); if ($results && isset($results['hits'])) { @@ -43,10 +64,6 @@ public function fetchResults($selectedIndex, $query, $searchParameters = []) if ($record && $record->canView()) { $records->push($record); - } else { - // record no longer exists so trigger a delete for this - // old record - $this->cleanUpOldResult($className, $id); } } catch (Exception $e) { // @@ -63,9 +80,4 @@ public function fetchResults($selectedIndex, $query, $searchParameters = []) return $output; } - - public function cleanUpOldResult($className, $objectID) - { - // @todo - } }