Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add option for clear/reindex documents by class names #87

Draft
wants to merge 1 commit into
base: 3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Interfaces/IndexingDocumentTypeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SilverStripe\SearchService\Interfaces;

interface IndexingDocumentTypeInterface
{

/**
* Set collection of document types (class names) that should be cleared from Elastic index
*/
public function setDocumentTypes(array $types): self;

}
65 changes: 63 additions & 2 deletions src/Jobs/ClearIndexJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\SearchService\Interfaces\BatchDocumentRemovalInterface;
use SilverStripe\SearchService\Interfaces\IndexingDocumentTypeInterface;
use SilverStripe\SearchService\Interfaces\IndexingInterface;
use SilverStripe\SearchService\Service\IndexConfiguration;
use SilverStripe\SearchService\Service\SyncJobRunner;
use SilverStripe\SearchService\Service\Traits\ServiceAware;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJobService;

/**
* @property int|null $batchOffset
* @property int|null $batchSize
* @property string|null $indexName
* @property array $documentTypes
* @property bool $reindex
*/
class ClearIndexJob extends AbstractQueuedJob implements QueuedJob
{
Expand All @@ -30,8 +35,12 @@ class ClearIndexJob extends AbstractQueuedJob implements QueuedJob
'IndexService' => '%$' . 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) {
Expand All @@ -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');
Expand Down Expand Up @@ -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());
Expand All @@ -97,6 +113,8 @@ public function process(): void
$this->getIndexName()
));

$this->scheduleReindex();

return;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

}
4 changes: 3 additions & 1 deletion src/Tasks/SearchClearIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading