Skip to content

Commit

Permalink
Fix import for aggregated models (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviano committed Jan 18, 2021
1 parent c38b1d4 commit 3968aa5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 56 deletions.
65 changes: 29 additions & 36 deletions src/Command/SearchImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 <comment>%s / %s</comment> %s entities into %s index',
$numberOfRecords,
count($entities),
$entityClassName,
'<info>' . $indexName . '</info>'
));
}
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 <comment>%s / %s</comment> %s entities into %s index',
$numberOfRecords,
count($entities),
$entityClass,
'<info>' . $indexName . '</info>'
));
}

$page++;
$repository->clear();
} while (count($entities) >= $config['batchSize']);

$page++;
$repository->clear();
} while (count($entities) >= $config['batchSize']);
}

if ($shouldDoAtomicReindex && isset($indexName)) {
$output->writeln("Moving <info>$indexName</info> -> <comment>$sourceIndexName</comment>\n");
$this->searchClient->moveIndex($indexName, $sourceIndexName);
}

$repository->clear();
}

$output->writeln('<info>Done!</info>');
Expand Down
4 changes: 2 additions & 2 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function index($searchableEntities, $requestOptions)
}

$data[$indexName][] = $searchableArray + [
'objectID' => $entity->getId(),
];
'objectID' => $entity->getId(),
];
}

$result = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 9 additions & 17 deletions tests/TestCase/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 3968aa5

Please sign in to comment.