From d0b6901dfb13cd93b338d596ec7f1e003114b172 Mon Sep 17 00:00:00 2001 From: Mohamed Alsharaf Date: Thu, 6 Jul 2023 16:50:25 +1200 Subject: [PATCH] Feature: Add option for clear/reindex documents by class names Fix ##86 --- .../IndexingDocumentTypeInterface.php | 13 ++++ src/Jobs/ClearIndexJob.php | 65 ++++++++++++++++++- src/Tasks/SearchClearIndex.php | 4 +- 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 src/Interfaces/IndexingDocumentTypeInterface.php diff --git a/src/Interfaces/IndexingDocumentTypeInterface.php b/src/Interfaces/IndexingDocumentTypeInterface.php new file mode 100644 index 0000000..a5d1f2c --- /dev/null +++ b/src/Interfaces/IndexingDocumentTypeInterface.php @@ -0,0 +1,13 @@ + '%$' . IndexingInterface::class, ]; - public function __construct(?string $indexName = null, ?int $batchSize = null) - { + public function __construct( + ?string $indexName = null, + ?int $batchSize = null, + string $types = '', + bool $reindex = false + ) { parent::__construct(); if (!$indexName) { @@ -41,8 +50,10 @@ public function __construct(?string $indexName = null, ?int $batchSize = null) $batchSize = $batchSize ?: IndexConfiguration::singleton()->getBatchSize(); $this->setIndexName($indexName); + $this->setDocumentTypes($types); $this->setBatchSize($batchSize); $this->setBatchOffset(0); + $this->setReindex($reindex); if (!$this->getBatchSize() || $this->getBatchSize() < 1) { throw new InvalidArgumentException('Batch size must be greater than 0'); @@ -78,6 +89,11 @@ public function process(): void } $this->currentStep++; + + if ($this->getIndexService() instanceof IndexingDocumentTypeInterface) { + $this->getIndexService()->setDocumentTypes($this->getDocumentTypes()); + } + $total = $this->getIndexService()->getDocumentTotal($this->getIndexName()); $numRemoved = $this->getIndexService()->removeAllDocuments($this->getIndexName()); $totalAfter = $this->getIndexService()->getDocumentTotal($this->getIndexName()); @@ -97,6 +113,8 @@ public function process(): void $this->getIndexName() )); + $this->scheduleReindex(); + return; } @@ -137,6 +155,16 @@ public function getIndexName(): ?string return $this->indexName; } + public function getDocumentTypes(): array + { + return $this->documentTypes; + } + + public function shouldReindex(): bool + { + return $this->reindex; + } + private function setBatchOffset(?int $batchOffset): void { $this->batchOffset = $batchOffset; @@ -152,4 +180,37 @@ private function setIndexName(?string $indexName): void $this->indexName = $indexName; } + private function setDocumentTypes(string $types): void + { + $this->documentTypes = $types !== '' ? array_map('trim', explode(',', $types)) : []; + } + + private function setReindex(bool $reindex): void + { + $this->reindex = $reindex; + } + + private function scheduleReindex(): void + { + // Skip if reindex not requested + if (!$this->shouldReindex()) { + return; + } + + $targetClasses = $this->getDocumentTypes(); + + // ReindexJob expects false (all class names) or collection of class names + if (!count($targetClasses)) { + $targetClasses = false; + } + + $job = ReindexJob::create($targetClasses, $this->getIndexName()); + + if (IndexConfiguration::singleton()->shouldUseSyncJobs()) { + SyncJobRunner::singleton()->runJob($job, false); + } else { + QueuedJobService::singleton()->queueJob($job); + } + } + } diff --git a/src/Tasks/SearchClearIndex.php b/src/Tasks/SearchClearIndex.php index 12bc51c..9ee0975 100644 --- a/src/Tasks/SearchClearIndex.php +++ b/src/Tasks/SearchClearIndex.php @@ -52,12 +52,14 @@ public function run($request): void // phpcs:ignore SlevomatCodingStandard.TypeH Environment::increaseTimeLimitTo(); $targetIndex = $request->getVar('index'); + $targetClasses = $request->getVar('classes'); + $reindex = (bool)$request->getVar('reindex'); if (!$targetIndex) { throw new InvalidArgumentException("Must specify an index in the 'index' parameter."); } - $job = ClearIndexJob::create($targetIndex); + $job = ClearIndexJob::create($targetIndex, null, $targetClasses, $reindex); if ($this->getConfiguration()->shouldUseSyncJobs()) { SyncJobRunner::singleton()->runJob($job, false);