Skip to content

Commit

Permalink
fix: Doctrine batch iterating for Solr indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Apr 26, 2022
1 parent d8fc516 commit 255699a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/Console/NodesEmptyTrashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$q = $qb->select('n')
->andWhere($countQb->expr()->eq('n.status', Node::DELETED))
->getQuery();
$iterableResult = $q->iterate();

while (($row = $iterableResult->next()) !== false) {
foreach ($q->toIterable() as $row) {
/** @var NodeHandler $nodeHandler */
$nodeHandler = $this->handlerFactory->getHandler($row[0]);
$nodeHandler = $this->handlerFactory->getHandler($row);
$nodeHandler->removeWithChildrenAndAssociations();
$io->progressAdvance();
++$i;
Expand Down
7 changes: 4 additions & 3 deletions src/SearchEngine/Indexer/DocumentIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ public function reindexAll(): void
$q = $this->managerRegistry->getRepository(Document::class)
->createQueryBuilder('d')
->getQuery();
$iterableResult = $q->iterate();

if (null !== $this->io) {
$this->io->progressStart((int) $countQuery->getSingleScalarResult());
}

while (($row = $iterableResult->next()) !== false) {
$solarium = $this->solariumFactory->createWithDocument($row[0]);
foreach ($q->toIterable() as $row) {
$solarium = $this->solariumFactory->createWithDocument($row);
$solarium->createEmptyDocument($update);
$solarium->index();
foreach ($solarium->getDocuments() as $document) {
Expand All @@ -76,6 +75,8 @@ public function reindexAll(): void
if (null !== $this->io) {
$this->io->progressAdvance();
}
// detach from Doctrine, so that it can be Garbage-Collected immediately
$this->managerRegistry->getManager()->detach($row);
}

$buffer->flush();
Expand Down
11 changes: 6 additions & 5 deletions src/SearchEngine/Indexer/NodesSourcesIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public function reindexAll(int $batchCount = 1, int $batchNumber = 0): void

$baseQb = $this->getAllQueryBuilder()->addSelect('n');
if ($batchCount > 1) {
$limit = round($count / $batchCount);
$offset = $batchNumber * $limit;
$limit = (int) round($count / $batchCount);
$offset = (int) $batchNumber * $limit;
if ($batchNumber === $batchCount - 1) {
$limit = $count - $offset;
$baseQb->setMaxResults($limit)->setFirstResult($offset);
Expand All @@ -109,21 +109,22 @@ public function reindexAll(int $batchCount = 1, int $batchNumber = 0): void
$count = $limit;
}
$q = $baseQb->getQuery();
$iterableResult = $q->iterate();

if (null !== $this->io) {
$this->io->progressStart($count);
}

while (($row = $iterableResult->next()) !== false) {
$solarium = $this->solariumFactory->createWithNodesSources($row[0]);
foreach ($q->toIterable() as $row) {
$solarium = $this->solariumFactory->createWithNodesSources($row);
$solarium->createEmptyDocument($update);
$solarium->index();
$buffer->addDocument($solarium->getDocument());

if (null !== $this->io) {
$this->io->progressAdvance();
}
// detach from Doctrine, so that it can be Garbage-Collected immediately
$this->managerRegistry->getManager()->detach($row);
}

$buffer->flush();
Expand Down

0 comments on commit 255699a

Please sign in to comment.