diff --git a/src/Plugin.php b/src/Plugin.php index 4cc87c5..c1ae565 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -86,13 +86,13 @@ static function (ModelEvent $event) use ($autoSyncIndices): void { // If an element is active, then we should update it in the index if ($query->id($sender->id)->exists()) { Queue::push(new SyncJob([ - 'indexName' => $index->handle, + 'indexHandle' => $index->handle, 'identifier' => $sender->id, ])); } } elseif ($query->status(null)->id($sender->id)->exists()) { Queue::push(new DeleteJob([ - 'indexName' => $index->handle, + 'indexHandle' => $index->handle, 'identifier' => $sender->id, ])); } @@ -111,7 +111,7 @@ static function (Event $event) use ($autoSyncIndices): void { $query = $index->query; if ($query->status(null)->id($sender->id)->exists()) { Queue::push(new DeleteJob([ - 'indexName' => $index->handle, + 'indexHandle' => $index->handle, 'identifier' => $sender->id, ])); } diff --git a/src/console/controllers/SyncController.php b/src/console/controllers/SyncController.php index 5dbadbc..bd882ac 100644 --- a/src/console/controllers/SyncController.php +++ b/src/console/controllers/SyncController.php @@ -3,6 +3,7 @@ namespace fostercommerce\meilisearch\console\controllers; use craft\console\Controller; +use fostercommerce\meilisearch\models\Index; use fostercommerce\meilisearch\Plugin; use Meilisearch\Exceptions\TimeOutException; use yii\console\ExitCode; @@ -39,13 +40,10 @@ public function actionAll(): int return ExitCode::OK; } - public function actionIndex(string $indexName): int + public function actionIndex(string $indexHandle): int { - $index = Plugin::getInstance()->settings->getIndices($indexName)[0] ?? null; - - if ($index === null) { - throw new \RuntimeException("Index {$indexName} does not exist."); - } + /** @var Index $index */ + $index = Plugin::getInstance()->settings->getIndices($indexHandle); foreach (Plugin::getInstance()->sync->sync($index, null) as $chunkSize) { $this->stdout("Synchronized {$chunkSize} documents to {$index->handle}" . PHP_EOL); @@ -54,13 +52,10 @@ public function actionIndex(string $indexName): int return ExitCode::OK; } - public function actionFlush(string $indexName): int + public function actionFlush(string $indexHandle): int { - $index = Plugin::getInstance()->settings->getIndices($indexName)[0] ?? null; - - if ($index === null) { - throw new \RuntimeException("Index {$indexName} does not exist."); - } + /** @var Index $index */ + $index = Plugin::getInstance()->settings->getIndices($indexHandle); Plugin::getInstance()->sync->flush($index); $this->stdout("Flushed all documents from {$index->handle}" . PHP_EOL); diff --git a/src/jobs/Delete.php b/src/jobs/Delete.php index 13bba91..55c826d 100644 --- a/src/jobs/Delete.php +++ b/src/jobs/Delete.php @@ -3,31 +3,53 @@ namespace fostercommerce\meilisearch\jobs; use craft\queue\BaseJob; +use fostercommerce\meilisearch\models\Index; use fostercommerce\meilisearch\Plugin; class Delete extends BaseJob { - public ?string $indexName = null; + public ?string $indexHandle = null; public string|int $identifier; + /** + * @param array $config + */ + public function __construct(array $config = []) + { + if (isset($config['indexName'])) { + // Index name is deprecated + $config['indexHandle'] = $config['indexName']; + unset($config['indexName']); + } + + parent::__construct($config); + } + public function execute($queue): void { - $indices = Plugin::getInstance()->settings->getIndices($this->indexName); + $indices = Plugin::getInstance()->settings->getIndices($this->indexHandle); - foreach ($indices as $i => $index) { - Plugin::getInstance()->sync->delete($index, $this->identifier); - $this->setProgress($queue, $i / count($indices)); + if ($indices instanceof Index) { + $indices = [$indices]; } + + $indices = collect($indices); + + $totalIndices = $indices->count(); + $indices->each(function ($index, $i) use ($queue, $totalIndices): void { + Plugin::getInstance()->sync->delete($index, $this->identifier); + $this->setProgress($queue, $i / $totalIndices); + }); } protected function defaultDescription(): ?string { $description = "Deleting {$this->identifier} from"; - if ($this->indexName === null) { + if ($this->indexHandle === null) { return "{$description} all indices"; } - return "{$description} {$this->indexName}"; + return "{$description} {$this->indexHandle}"; } } diff --git a/src/jobs/Sync.php b/src/jobs/Sync.php index 4da7cf6..3ac2e2e 100644 --- a/src/jobs/Sync.php +++ b/src/jobs/Sync.php @@ -8,25 +8,41 @@ class Sync extends BaseJob { - public ?string $indexName = null; + public ?string $indexHandle = null; public null|string|int $identifier = null; + /** + * @param array $config + */ + public function __construct(array $config = []) + { + if (isset($config['indexName'])) { + // Index name is deprecated + $config['indexHandle'] = $config['indexName']; + unset($config['indexName']); + } + + parent::__construct($config); + } + public function execute($queue): void { - $indices = Plugin::getInstance()->settings->getIndices($this->indexName); + $indices = Plugin::getInstance()->settings->getIndices($this->indexHandle); + + if ($indices instanceof Index) { + $indices = [$indices]; + } + + $indices = collect($indices); // Only get page count if we're not attempting to synchronize a specific item. $totalPages = $this->identifier === null - ? collect($indices) - ->reduce( - fn ($total, Index $index): int => $total + ($index->getPageCount() ?? 0), - 0 - ) + ? $indices->reduce(static fn ($total, Index $index): int => $total + ($index->getPageCount() ?? 0), 0) : 0; $currentPage = 0; - foreach ($indices as $index) { + $indices->each(function ($index) use ($queue, $totalPages, $currentPage): void { foreach (Plugin::getInstance()->sync->sync($index, $this->identifier) as $chunkSize) { ++$currentPage; @@ -34,7 +50,7 @@ public function execute($queue): void $this->setProgress($queue, $currentPage / $totalPages); } } - } + }); $this->setProgress($queue, 1); } @@ -43,17 +59,17 @@ protected function defaultDescription(): ?string { if ($this->identifier !== null) { $description = "Sync {$this->identifier} in"; - if ($this->indexName === null) { + if ($this->indexHandle === null) { return "{$description} all indices"; } - return "{$description} {$this->indexName}"; + return "{$description} {$this->indexHandle}"; } - if ($this->indexName === null) { + if ($this->indexHandle === null) { return 'Sync all indices'; } - return "Sync {$this->indexName}"; + return "Sync {$this->indexHandle}"; } } diff --git a/src/models/Settings.php b/src/models/Settings.php index c6f77b6..d698a60 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -62,20 +62,22 @@ public function setAttributes($values, $safeOnly = true): void } /** - * @return array + * If $indexHandle is set, returns an `Index`. Otherwise, returns an array of indices + * + * @return ($indexHandle is non-empty-string ? Index : Index[]) */ - public function getIndices(?string $indexName = null): array + public function getIndices(?string $indexHandle = null): mixed { - if ($indexName !== null) { + if ($indexHandle !== null) { $result = array_filter([ - $this->indices[$indexName] ?? null, + $this->indices[$indexHandle] ?? null, ]); if ($result === []) { - throw new \RuntimeException("Index '{$indexName}' not found"); + throw new \RuntimeException("Index '{$indexHandle}' not found"); } - return $result; + return $result[0]; } return array_values($this->indices); diff --git a/src/services/Search.php b/src/services/Search.php index 344d90e..5147b56 100644 --- a/src/services/Search.php +++ b/src/services/Search.php @@ -2,6 +2,7 @@ namespace fostercommerce\meilisearch\services; +use fostercommerce\meilisearch\models\Index; use fostercommerce\meilisearch\Plugin; use Meilisearch\Search\SearchResult; use yii\base\Component; @@ -18,17 +19,11 @@ public function init(): void /** * @param array $searchParams * @param array $options - * @return array{ - * results: array>, - * pagination: array - * } */ - public function search(string $indexName, string $query, array $searchParams = [], array $options = []): array + public function search(string $indexHandle, string $query, array $searchParams = [], array $options = []): SearchResult { - $index = Plugin::getInstance()->settings->getIndices($indexName)[0] ?? null; - if ($index === null) { - throw new \RuntimeException('Index not found'); - } + /** @var Index $index */ + $index = Plugin::getInstance()->settings->getIndices($indexHandle); // TODO handle raw search - If $options['raw'] is truthy. @@ -37,17 +32,6 @@ public function search(string $indexName, string $query, array $searchParams = [ ->index($index->indexId) ->search($query, $searchParams, $options); - $offset = ($result->getHitsPerPage() * ($result->getPage() - 1)); - - return [ - 'results' => $result->getHits(), - 'pagination' => [ - 'first' => $offset + 1, - 'last' => $offset + count($result->getHits()), - 'total' => $result->getTotalHits(), - 'currentPage' => $result->getPage(), - 'totalPages' => $result->getTotalPages(), - ], - ]; + return $result; } } diff --git a/src/variables/Search.php b/src/variables/Search.php index 14edb2e..d8e8ed7 100644 --- a/src/variables/Search.php +++ b/src/variables/Search.php @@ -27,18 +27,17 @@ public function search(string $indexHandle, string $query, array $searchParams = ...$searchParams, ]; $results = Plugin::getInstance()->search->search($indexHandle, $query, $searchParams, $options); - - + $offset = ($results->getHitsPerPage() * ($results->getPage() - 1)); return [ - 'results' => $results['results'], + 'results' => $results->getHits(), 'pagination' => Craft::createObject([ 'class' => Paginate::class, - 'first' => $results['pagination']['first'], - 'last' => $results['pagination']['last'], - 'total' => $results['pagination']['total'], - 'currentPage' => $results['pagination']['currentPage'], - 'totalPages' => $results['pagination']['totalPages'], + 'first' => $offset + 1, + 'last' => $offset + $results->getHitsCount(), + 'total' => $results->getTotalHits(), + 'currentPage' => $results->getPage(), + 'totalPages' => $results->getTotalPages(), ]), ]; }