Skip to content

Commit b9264c8

Browse files
committed
Add option to meili:import command to override batch size
1 parent 682a272 commit b9264c8

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/Command/MeiliSearchImportCommand.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ protected function configure(): void
4040
null,
4141
InputOption::VALUE_NONE,
4242
'Update settings related to indices to the search engine'
43-
);
43+
)
44+
->addOption('batch-size', null, InputOption::VALUE_REQUIRED);
4445
}
4546

4647
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -64,6 +65,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6465
}
6566

6667
$entitiesToIndex = array_unique($indexes->toArray(), SORT_REGULAR);
68+
$batchSize = $input->getOption('batch-size');
69+
$batchSize = ctype_digit($batchSize) ? (int) $batchSize : $config->get('batchSize');
6770

6871
/** @var array $index */
6972
foreach ($entitiesToIndex as $index) {
@@ -82,8 +85,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8285
$entities = $repository->findBy(
8386
[],
8487
null,
85-
$config->get('batchSize'),
86-
$config->get('batchSize') * $page
88+
$batchSize,
89+
$batchSize * $page
8790
);
8891

8992
$responses = $this->formatIndexingResponse($this->searchService->index($manager, $entities));
@@ -125,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
125128
}
126129

127130
++$page;
128-
} while (count($entities) >= $config->get('batchSize'));
131+
} while (count($entities) >= $batchSize);
129132

130133
$manager->clear();
131134
}

tests/Integration/CommandsTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
128128
EOD, $clearOutput);
129129
}
130130

131+
public function testSearchImportWithCustomBatchSize(): void
132+
{
133+
for ($i = 0; $i <= 10; ++$i) {
134+
$this->createPage($i);
135+
}
136+
137+
$importCommand = $this->application->find('meili:import');
138+
$importCommandTester = new CommandTester($importCommand);
139+
$importCommandTester->execute([
140+
'--indices' => 'pages',
141+
'--batch-size' => '2',
142+
]);
143+
144+
$importOutput = $importCommandTester->getDisplay();
145+
146+
$this->assertSame(<<<'EOD'
147+
Importing for index MeiliSearch\Bundle\Test\Entity\Page
148+
Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index
149+
Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index
150+
Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index
151+
Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index
152+
Indexed 2 / 2 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index
153+
Indexed 1 / 1 MeiliSearch\Bundle\Test\Entity\Page entities into sf_phpunit__pages index
154+
Done!
155+
156+
EOD, $importOutput);
157+
}
158+
131159
/**
132160
* Importing 'Tag' and 'Link' into the same 'tags' index.
133161
*/

0 commit comments

Comments
 (0)