Skip to content

Commit

Permalink
chore: Refactor index handling and improve search pagination; Replace…
Browse files Browse the repository at this point in the history
… indexName with indexHandle; Update Sync and Delete jobs for consistent index handling
  • Loading branch information
johnnynotsolucky committed Nov 14, 2024
1 parent d2711e2 commit 35aac4a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 70 deletions.
6 changes: 3 additions & 3 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]));
}
Expand All @@ -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,
]));
}
Expand Down
19 changes: 7 additions & 12 deletions src/console/controllers/SyncController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
36 changes: 29 additions & 7 deletions src/jobs/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, mixed> $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}";
}
}
42 changes: 29 additions & 13 deletions src/jobs/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,49 @@

class Sync extends BaseJob
{
public ?string $indexName = null;
public ?string $indexHandle = null;

public null|string|int $identifier = null;

/**
* @param array<array-key, mixed> $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;

if ($totalPages > 0) {
$this->setProgress($queue, $currentPage / $totalPages);
}
}
}
});

$this->setProgress($queue, 1);
}
Expand All @@ -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}";
}
}
14 changes: 8 additions & 6 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ public function setAttributes($values, $safeOnly = true): void
}

/**
* @return array<int, Index>
* 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);
Expand Down
26 changes: 5 additions & 21 deletions src/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,17 +19,11 @@ public function init(): void
/**
* @param array<non-empty-string, mixed> $searchParams
* @param array<non-empty-string, mixed> $options
* @return array{
* results: array<int, array<mixed, mixed>>,
* pagination: array<non-empty-string, int|null>
* }
*/
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.

Expand All @@ -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;
}
}
15 changes: 7 additions & 8 deletions src/variables/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]),
];
}
Expand Down

0 comments on commit 35aac4a

Please sign in to comment.