Fix meilisearch:check-consistency to create missing indexes with --fix

Previously, missing indexes were silently skipped. Now with --fix,
the command creates the index and indexes all documents from the database.
Without --fix, missing indexes are reported. Also checks all organizer
and event indexes regardless of whether they already exist in Meilisearch.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-03-26 09:41:12 +01:00
parent 7e9f99a88e
commit 0aed47c86c

View File

@@ -74,19 +74,17 @@ class MeilisearchConsistencyCommand extends Command
$this->accumulate($totals, $this->checkEventIndex('event_global', $fix, $io));
$this->accumulate($totals, $this->checkEventIndex('event_admin', $fix, $io));
foreach ($this->em->getRepository(User::class)->findBy([], []) as $user) {
$idx = 'event_'.$user->getId();
if (\in_array($idx, $indexes, true)) {
$this->accumulate($totals, $this->checkEventIndex($idx, $fix, $io, $user->getId()));
}
$organizers = array_filter(
$this->em->getRepository(User::class)->findAll(),
fn (User $u) => \in_array('ROLE_ORGANIZER', $u->getRoles(), true),
);
foreach ($organizers as $user) {
$this->accumulate($totals, $this->checkEventIndex('event_'.$user->getId(), $fix, $io, $user->getId()));
}
$io->section('Order indexes');
foreach ($this->em->getRepository(Event::class)->findAll() as $event) {
$idx = 'order_event_'.$event->getId();
if (\in_array($idx, $indexes, true)) {
$this->accumulate($totals, $this->checkOrderIndex($event, $fix, $io));
}
$this->accumulate($totals, $this->checkOrderIndex($event, $fix, $io));
}
$io->section('User indexes');
@@ -159,12 +157,14 @@ class MeilisearchConsistencyCommand extends Command
*/
private function checkEventIndex(string $index, bool $fix, SymfonyStyle $io, ?int $accountId = null): array
{
if (!$this->meilisearch->indexExists($index)) {
$indexExists = $this->meilisearch->indexExists($index);
if (!$indexExists && !$fix) {
$io->text(sprintf(' [%s] Index missing', $index));
return [0, 0];
}
$meiliIds = $this->meilisearch->getAllDocumentIds($index);
if ('event_global' === $index) {
$dbEvents = $this->em->getRepository(Event::class)->findBy(['isOnline' => true, 'isSecret' => false]);
} elseif (null !== $accountId) {
@@ -179,6 +179,17 @@ class MeilisearchConsistencyCommand extends Command
$dbEvents = $this->em->getRepository(Event::class)->findAll();
}
if (!$indexExists && $fix) {
$this->meilisearch->request('POST', '/indexes', ['uid' => $index, 'primaryKey' => 'id']);
$io->text(sprintf(' [%s] Created index, indexing %d event(s)', $index, \count($dbEvents)));
foreach ($dbEvents as $event) {
$this->eventIndex->indexEvent($event);
}
return [0, \count($dbEvents)];
}
$meiliIds = $this->meilisearch->getAllDocumentIds($index);
$dbIds = array_map(fn (Event $e) => $e->getId(), $dbEvents);
$eventsById = [];
@@ -201,13 +212,27 @@ class MeilisearchConsistencyCommand extends Command
private function checkOrderIndex(Event $event, bool $fix, SymfonyStyle $io): array
{
$index = 'order_event_'.$event->getId();
$indexExists = $this->meilisearch->indexExists($index);
if (!$indexExists && !$fix) {
$io->text(sprintf(' [%s] Index missing', $index));
if (!$this->meilisearch->indexExists($index)) {
return [0, 0];
}
$meiliIds = $this->meilisearch->getAllDocumentIds($index);
$dbOrders = $this->em->getRepository(BilletBuyer::class)->findBy(['event' => $event]);
if (!$indexExists && $fix) {
$this->meilisearch->request('POST', '/indexes', ['uid' => $index, 'primaryKey' => 'id']);
$io->text(sprintf(' [%s] Created index, indexing %d order(s)', $index, \count($dbOrders)));
foreach ($dbOrders as $order) {
$this->orderIndex->indexOrder($order);
}
return [0, \count($dbOrders)];
}
$meiliIds = $this->meilisearch->getAllDocumentIds($index);
$dbIds = array_map(fn (BilletBuyer $o) => $o->getId(), $dbOrders);
$ordersById = [];
@@ -229,11 +254,14 @@ class MeilisearchConsistencyCommand extends Command
*/
private function checkUserIndex(string $index, bool $isOrganizer, bool $fix, SymfonyStyle $io): array
{
if (!$this->meilisearch->indexExists($index)) {
$indexExists = $this->meilisearch->indexExists($index);
if (!$indexExists && !$fix) {
$io->text(sprintf(' [%s] Index missing', $index));
return [0, 0];
}
$meiliIds = $this->meilisearch->getAllDocumentIds($index);
$allUsers = $this->em->getRepository(User::class)->findAll();
if ($isOrganizer) {
@@ -242,6 +270,18 @@ class MeilisearchConsistencyCommand extends Command
$dbUsers = array_filter($allUsers, fn (User $u) => $u->isVerified() && !\in_array('ROLE_ORGANIZER', $u->getRoles(), true) && !\in_array('ROLE_ROOT', $u->getRoles(), true));
}
if (!$indexExists && $fix) {
$this->meilisearch->request('POST', '/indexes', ['uid' => $index, 'primaryKey' => 'id']);
$docs = array_values(array_map(fn (User $u) => $this->userToDocument($u, $isOrganizer), $dbUsers));
$io->text(sprintf(' [%s] Created index, indexing %d user(s)', $index, \count($docs)));
if ([] !== $docs) {
$this->meilisearch->addDocuments($index, $docs);
}
return [0, \count($docs)];
}
$meiliIds = $this->meilisearch->getAllDocumentIds($index);
$dbIds = array_map(fn (User $u) => $u->getId(), $dbUsers);
$usersById = [];