3
0

4 Commits

Author SHA1 Message Date
6e2c241349 Adding formatting 2023-01-09 16:19:16 +00:00
802413f754 Fixing deprecation 2023-01-09 16:07:13 +00:00
e56ea29018 Adding batching to index command for performance 2023-01-09 16:06:37 +00:00
3faec6370c Merge tag '0.0.18' into develop
Trying to load custom Doctrine config
2022-07-29 11:27:46 +01:00
2 changed files with 37 additions and 8 deletions

View File

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

View File

@@ -14,7 +14,7 @@ class SearchableSubscriber implements EventSubscriber
{ {
public function __construct(private SearchService $searchService) {} public function __construct(private SearchService $searchService) {}
public function getSubscribedEvents() public function getSubscribedEvents(): array
{ {
return [ return [
Events::postPersist, Events::postPersist,