Skip to content

Commit

Permalink
fix error using class name for index name
Browse files Browse the repository at this point in the history
e.g. with the set up below it was giving the error "Index SilverStripe\CMS\Model\SiteTree not found, must be one of [Page]"
      indexes:
        Page:
          includeClasses:
            - SilverStripe\CMS\Model\SiteTree
  • Loading branch information
matt-in-a-hat committed Jul 17, 2024
1 parent 865592d commit 78c6712
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions src/Jobs/AlgoliaReindexAllJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,49 +104,58 @@ public function process()
return;
}

$algoliaService = Injector::inst()->create(AlgoliaService::class);
$task = new AlgoliaReindex();

$batchSize = $task->config()->get('batch_size');
$batching = $this->config()->get('use_batching');

foreach ($remainingChildren as $class => $ids) {
$take = array_slice($ids, 0, $batchSize);
$this->indexData[$class] = array_slice($ids, $batchSize);
foreach ($algoliaService->indexes as $indexName => $index) {
$classes = (isset($index['includeClasses'])) ? $index['includeClasses'] : [];

if (!empty($take)) {
$this->currentStep += count($take);
$errors = [];
if (!in_array($class, $classes)) {
continue;
}

try {
if ($batching) {
if ($task->indexItems($class, DataObject::get($class)->filter('ID', $take), false)) {
$this->addMessage('Successfully indexing ' . $class . ' [' . implode(', ', $take) . ']');
} else {
$this->addMessage('Error indexing ' . $class . ' [' . implode(', ', $take) . ']');
}
} else {
$items = DataObject::get($class)->filter('ID', $take);
$take = array_slice($ids, 0, $batchSize);
$this->indexData[$class] = array_slice($ids, $batchSize);

if (!empty($take)) {
$this->currentStep += count($take);
$errors = [];

foreach ($items as $item) {
if ($task->indexItem($item)) {
$this->addMessage('Successfully indexed ' . $class . ' [' . $item->ID . ']');
try {
if ($batching) {
if ($task->indexItems($indexName, DataObject::get($class)->filter('ID', $take), false)) {
$this->addMessage('Successfully indexing ' . $class . ' [' . implode(', ', $take) . ']');
} else {
$this->addMessage('Error indexing ' . $class . ' [' . $item->ID . ']');
$this->addMessage('Error indexing ' . $class . ' [' . implode(', ', $take) . ']');
}
} else {
$items = DataObject::get($class)->filter('ID', $take);

foreach ($items as $item) {
if ($task->indexItem($item)) {
$this->addMessage('Successfully indexed ' . $class . ' [' . $item->ID . ']');
} else {
$this->addMessage('Error indexing ' . $class . ' [' . $item->ID . ']');
}
}
}
}

$errors = $task->getErrors();
} catch (Throwable $e) {
$errors[] = $e->getMessage();
}
$errors = $task->getErrors();
} catch (Throwable $e) {
$errors[] = $e->getMessage();
}

if (!empty($errors)) {
$this->addMessage(implode(', ', $errors));
$task->clearErrors();
if (!empty($errors)) {
$this->addMessage(implode(', ', $errors));
$task->clearErrors();
}
} else {
unset($this->indexData[$class]);
}
} else {
unset($this->indexData[$class]);
}
}
}
Expand Down

0 comments on commit 78c6712

Please sign in to comment.