diff --git a/src/Command/SearchImportCommand.php b/src/Command/SearchImportCommand.php index 0ec1557a..ba1bece3 100644 --- a/src/Command/SearchImportCommand.php +++ b/src/Command/SearchImportCommand.php @@ -77,22 +77,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $config = $this->searchService->getConfiguration(); $indexingService = ($shouldDoAtomicReindex ? $this->searchServiceForAtomicReindex : $this->searchService); - foreach ($entitiesToIndex as $key => $entityClassName) { - if (is_subclass_of($entityClassName, Aggregator::class)) { - unset($entitiesToIndex[$key]); - $entitiesToIndex = array_merge($entitiesToIndex, $entityClassName::getEntities()); - } - } - - $entitiesToIndex = array_unique($entitiesToIndex); - foreach ($entitiesToIndex as $entityClassName) { if (!$this->searchService->isSearchable($entityClassName)) { continue; } - $manager = $this->managerRegistry->getManagerForClass($entityClassName); - $repository = $manager->getRepository($entityClassName); $sourceIndexName = $this->searchService->searchableAs($entityClassName); if ($shouldDoAtomicReindex) { @@ -101,38 +90,42 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->searchClient->copyIndex($sourceIndexName, $temporaryIndexName, ['scope' => ['settings', 'synonyms', 'rules']]); } - $page = 0; - do { - $entities = $repository->findBy( - [], - null, - $config['batchSize'], - $config['batchSize'] * $page - ); - - $responses = $this->formatIndexingResponse( - $indexingService->index($manager, $entities) - ); - foreach ($responses as $indexName => $numberOfRecords) { - $output->writeln(sprintf( - 'Indexed %s / %s %s entities into %s index', - $numberOfRecords, - count($entities), - $entityClassName, - '' . $indexName . '' - )); - } + foreach (is_subclass_of($entityClassName, Aggregator::class) ? $entityClassName::getEntities() : [$entityClassName] as $entityClass) { + $manager = $this->managerRegistry->getManagerForClass($entityClass); + $repository = $manager->getRepository($entityClass); + + $page = 0; + do { + $entities = $repository->findBy( + [], + null, + $config['batchSize'], + $config['batchSize'] * $page + ); + + $responses = $this->formatIndexingResponse($indexingService->index($manager, $entities)); + + foreach ($responses as $indexName => $numberOfRecords) { + $output->writeln(sprintf( + 'Indexed %s / %s %s entities into %s index', + $numberOfRecords, + count($entities), + $entityClass, + '' . $indexName . '' + )); + } + + $page++; + $repository->clear(); + } while (count($entities) >= $config['batchSize']); - $page++; $repository->clear(); - } while (count($entities) >= $config['batchSize']); + } if ($shouldDoAtomicReindex && isset($indexName)) { $output->writeln("Moving $indexName -> $sourceIndexName\n"); $this->searchClient->moveIndex($indexName, $sourceIndexName); } - - $repository->clear(); } $output->writeln('Done!'); diff --git a/src/Engine.php b/src/Engine.php index ec391eb5..2b0315f5 100644 --- a/src/Engine.php +++ b/src/Engine.php @@ -53,8 +53,8 @@ public function index($searchableEntities, $requestOptions) } $data[$indexName][] = $searchableArray + [ - 'objectID' => $entity->getId(), - ]; + 'objectID' => $entity->getId(), + ]; } $result = []; diff --git a/tests/BaseTest.php b/tests/BaseTest.php index b85e0078..dfbaf364 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -59,7 +59,7 @@ protected function createComment($id = null) { $comment = new Comment(); $comment->setContent('Comment content'); - $comment->setPost(new Post(['title' => 'What a post!'])); + $comment->setPost(new Post(['title' => 'What a post!', 'content' => 'my content'])); if (!is_null($id)) { $comment->setId($id); diff --git a/tests/TestCase/CommandsTest.php b/tests/TestCase/CommandsTest.php index efdd75be..ed396499 100644 --- a/tests/TestCase/CommandsTest.php +++ b/tests/TestCase/CommandsTest.php @@ -88,22 +88,13 @@ public function testSearchClear() public function testSearchImportAggregator() { - $now = new \DateTime(); - $this->connection->insert($this->indexName, [ - 'title' => 'Test', - 'content' => 'Test content', - 'published_at' => $now->format('Y-m-d H:i:s'), - ]); - $this->connection->insert($this->indexName, [ - 'title' => 'Test2', - 'content' => 'Test content2', - 'published_at' => $now->format('Y-m-d H:i:s'), - ]); - $this->connection->insert($this->indexName, [ - 'title' => 'Test3', - 'content' => 'Test content3', - 'published_at' => $now->format('Y-m-d H:i:s'), - ]); + for ($i = 1; $i <= 2; $i++) { + $this->om->persist($comment = $this->createComment()); + $this->om->persist($comment->getPost()); + $this->om->persist($this->createImage()); + } + + $this->om->flush(); $command = $this->application->find('search:import'); $commandTester = new CommandTester($command); @@ -117,7 +108,8 @@ public function testSearchImportAggregator() $this->assertContains('Done!', $output); $iteration = 0; - $expectedResult = 3; + $expectedResult = 6; + do { $searchPost = $this->searchService->rawSearch(ContentAggregator::class); sleep(1);