3
0

Adding batching to index command for performance

This commit is contained in:
2023-01-09 16:06:37 +00:00
parent 3faec6370c
commit e56ea29018

View File

@@ -2,10 +2,12 @@
namespace Pcm\SearchBundle\Command;
use Doctrine\ORM\EntityManagerInterface;
use Pcm\SearchBundle\Entity\SearchIndex;
use Pcm\SearchBundle\Service\SearchService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Attribute\AsCommand;
@@ -23,7 +25,16 @@ class SearchIndexCommand extends Command
parent::__construct();
}
protected function configure(): void {}
protected function configure(): void
{
$this->addOption(
'batchsize',
'b',
InputOption::VALUE_REQUIRED,
'Number of rows to process before flushing',
2000
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
@@ -42,29 +53,46 @@ class SearchIndexCommand extends Command
$progress_bar->setProgressCharacter("<fg=green>➤</>");
$progress_bar->start();
$batchSize = $input->getOption('batchsize');
$searchEntities = null;
foreach ($searchables as $searchable) {
$entities = $this
->em
->getRepository($searchable)
->findAll();
$searchEntities[] = [$searchable, count($entities)];
$this->em->clear();
$counter = 1;
foreach ($entities as $entity) {
$search_result = $this->searchService->createSearchResult($entity);
$this->em->persist($search_result);
if (($counter % $batchSize) === 0) {
$this->em->flush();
$this->em->clear();
}
$counter++;
}
$progress_bar->advance();
}
$this->em->flush();
$this->em->clear();
}
$progress_bar->finish();
$io->writeln('');
$io->writeln('');
$table = new Table($output);
$table
->setHeaders(['Class', 'Count'])
->setRows($searchEntities)
;
$table->render();
$io->writeln('');
$io->success('Index updated');
return Command::SUCCESS;