diff --git a/src/Command/MeiliSearchImportCommand.php b/src/Command/MeiliSearchImportCommand.php index 5c92954e..8fa8571e 100644 --- a/src/Command/MeiliSearchImportCommand.php +++ b/src/Command/MeiliSearchImportCommand.php @@ -40,7 +40,8 @@ protected function configure(): void null, InputOption::VALUE_NONE, 'Update settings related to indices to the search engine' - ); + ) + ->addOption('batch-size', null, InputOption::VALUE_REQUIRED); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -64,6 +65,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $entitiesToIndex = array_unique($indexes->toArray(), SORT_REGULAR); + $batchSize = $input->getOption('batch-size'); + $batchSize = ctype_digit($batchSize) ? (int) $batchSize : $config->get('batchSize'); /** @var array $index */ foreach ($entitiesToIndex as $index) { @@ -82,8 +85,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $entities = $repository->findBy( [], null, - $config->get('batchSize'), - $config->get('batchSize') * $page + $batchSize, + $batchSize * $page ); $responses = $this->formatIndexingResponse($this->searchService->index($manager, $entities)); @@ -125,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } ++$page; - } while (count($entities) >= $config->get('batchSize')); + } while (count($entities) >= $batchSize); $manager->clear(); } diff --git a/tests/Integration/CommandsTest.php b/tests/Integration/CommandsTest.php index 84dc2cc4..6fb42d0a 100644 --- a/tests/Integration/CommandsTest.php +++ b/tests/Integration/CommandsTest.php @@ -128,6 +128,34 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void EOD, $clearOutput); } + public function testSearchImportWithCustomBatchSize(): void + { + for ($i = 0; $i <= 10; ++$i) { + $this->createPage($i); + } + + $importCommand = $this->application->find('meili:import'); + $importCommandTester = new CommandTester($importCommand); + $importCommandTester->execute([ + '--indices' => 'pages', + '--batch-size' => '2', + ]); + + $importOutput = $importCommandTester->getDisplay(); + + $this->assertSame(<<<'EOD' +Importing for index MeiliSearch\Bundle\Test\Entity\Page +Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index +Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index +Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index +Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index +Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index +Indexed 1 / 1 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index +Done! + +EOD, $importOutput); + } + /** * Importing 'Tag' and 'Link' into the same 'tags' index. */