entityManager->getRepository(Article::class)->findBy(['indexStatus' => IndexStatusEnum::TO_BE_INDEXED]); $batchCount = 0; $processedCount = 0; foreach ($articles as $item) { $batchCount++; // Collect batch of entities for indexing $batchItems[] = $item; // Process batch when limit is reached if ($batchCount >= self::BATCH_SIZE) { $this->flushAndPersistBatch($batchItems); $processedCount += $batchCount; $batchCount = 0; $batchItems = []; } } // Process any remaining items if (!empty($batchItems)) { $this->flushAndPersistBatch($batchItems); $processedCount += count($batchItems); } $output->writeln("$processedCount items indexed in Elasticsearch."); return Command::SUCCESS; } private function flushAndPersistBatch(array $items): void { // Persist batch to Elasticsearch $this->itemPersister->replaceMany($items); } }