From 78c67123a9d8dda00e7af36ea42a4bbd036d3db4 Mon Sep 17 00:00:00 2001 From: Matt Lang Date: Thu, 18 Jul 2024 11:41:24 +1200 Subject: [PATCH] fix error using class name for index name 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 --- src/Jobs/AlgoliaReindexAllJob.php | 65 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/src/Jobs/AlgoliaReindexAllJob.php b/src/Jobs/AlgoliaReindexAllJob.php index 2130ca7..0de985b 100644 --- a/src/Jobs/AlgoliaReindexAllJob.php +++ b/src/Jobs/AlgoliaReindexAllJob.php @@ -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]); } } }