Skip to content

Commit

Permalink
Merge pull request #39 from wilr/wilr-patch-2
Browse files Browse the repository at this point in the history
Make providing a index optional
  • Loading branch information
wilr authored Mar 15, 2021
2 parents e339b93 + 0b43df9 commit 6aac96d
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/Service/AlgoliaQuerier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'])) {
Expand All @@ -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) {
//
Expand All @@ -63,9 +80,4 @@ public function fetchResults($selectedIndex, $query, $searchParameters = [])

return $output;
}

public function cleanUpOldResult($className, $objectID)
{
// @todo
}
}

0 comments on commit 6aac96d

Please sign in to comment.