Skip to content

Commit

Permalink
Add support for reindexing all subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Jul 6, 2020
1 parent d20e92f commit ebd1014
Showing 1 changed file with 51 additions and 19 deletions.
70 changes: 51 additions & 19 deletions src/Tasks/AlgoliaReindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,54 @@ public function run($request)
Environment::increaseMemoryLimitTo();
Environment::increaseTimeLimitTo();

$algoliaService = Injector::inst()->create(AlgoliaService::class);
$targetClass = SiteTree::class;
$additionalFiltering = '';
$targetClass = '';
$filter = '';

if ($request->getVar('onlyClass')) {
$targetClass = $request->getVar('onlyClass');
}

if ($request->getVar('filter')) {
$additionalFiltering = $request->getVar('filter');
$filter = $request->getVar('filter');
}

if (!$request->getVar('forceAll') && !$filter) {
$filter = 'AlgoliaIndexed IS NULL';
}

if ($request->getVar('forceAll')) {
$items = Versioned::get_by_stage(
$targetClass,
'Live',
$additionalFiltering
);
if ($targetClass) {
$this->indexClass($targetClass, $filter);
} else {
$items = Versioned::get_by_stage(
$targetClass,
'Live',
($additionalFiltering)
? $additionalFiltering
: 'AlgoliaIndexed IS NULL OR AlgoliaIndexed < (NOW() - INTERVAL 2 HOUR)'
);
$algoliaService = Injector::inst()->create(AlgoliaService::class);

// find all classes we have to index and do so
foreach ($algoliaService->indexes as $index) {
$classes = (isset($index['includeClasses'])) ? $index['includeClasses'] : null;

if ($classes) {
foreach ($classes as $candidate) {
$this->indexClass($candidate, $filter);
}
}
}
}
}

public function indexClass($targetClass, $filter = '')
{
$inst = $targetClass::create();

if ($inst->hasExtension(Versioned::class)) {
$items = Versioned::get_by_stage($targetClass, 'Live', $filter);
} else {
$items = $inst::get();

if ($filter) {
$items = $items->where($filter);
}
}

$algoliaService = Injector::inst()->create(AlgoliaService::class);
$count = 0;
$skipped = 0;
$errored = 0;
Expand All @@ -67,8 +87,9 @@ public function run($request)
$indexer = Injector::inst()->create(AlgoliaIndexer::class);

echo sprintf(
'Found %s pages remaining to index, will export in batches of %s, grouped by type. %s',
'Found %s %s remaining to index, will export in batches of %s, %s batches total %s',
$total,
$targetClass,
$batchSize,
$batchesTotal,
PHP_EOL
Expand Down Expand Up @@ -106,7 +127,18 @@ public function run($request)
// Set AlgoliaUUID, in case it wasn't previously set
if (!$item->AlgoliaUUID) {
$item->assignAlgoliaUUID();
$item->write();

try {
$item->write();
} catch (Exception $e) {
var_dump($e);
die();
Injector::inst()->get(LoggerInterface::class)->error($e);

$errored++;

continue;
}
}

$batchKey = get_class($item);
Expand Down

0 comments on commit ebd1014

Please sign in to comment.