This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
forked from Lakion/SyliusElasticSearchBundle
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a command to reset product index
- Loading branch information
Showing
5 changed files
with
133 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ElasticSearchPlugin\Command; | ||
|
||
use ONGR\ElasticsearchBundle\Service\Manager; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Sylius\ElasticSearchPlugin\Factory\ProductDocumentFactoryInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class ResetProductIndexCommand extends Command | ||
{ | ||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
/** | ||
* @var Manager | ||
*/ | ||
private $manager; | ||
|
||
/** | ||
* @var ProductDocumentFactoryInterface | ||
*/ | ||
private $productDocumentFactory; | ||
|
||
/** | ||
* @param ProductRepositoryInterface $productRepository | ||
* @param Manager $manager | ||
* @param ProductDocumentFactoryInterface $productDocumentFactory | ||
*/ | ||
public function __construct( | ||
ProductRepositoryInterface $productRepository, | ||
Manager $manager, | ||
ProductDocumentFactoryInterface $productDocumentFactory | ||
) { | ||
$this->productRepository = $productRepository; | ||
$this->manager = $manager; | ||
$this->productDocumentFactory = $productDocumentFactory; | ||
|
||
parent::__construct('sylius:elastic-search:reset-product-index'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->addOption('force', 'f', null, 'To confirm running this command') | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
if (!$input->getOption('force')) { | ||
$output->writeln('WARNING! This command will drop the existing index and rebuild it from scratch. To proceed, run with "--force" option.'); | ||
|
||
return; | ||
} | ||
|
||
$this->manager->dropAndCreateIndex(); | ||
|
||
$productDocumentsCreated = 0; | ||
|
||
/** @var ProductInterface[] $products */ | ||
$products = $this->productRepository->findAll(); | ||
foreach ($products as $product) { | ||
$channels = $product->getChannels(); | ||
|
||
/** @var ChannelInterface $channel */ | ||
foreach ($channels as $channel) { | ||
$locales = $channel->getLocales(); | ||
foreach ($locales as $locale) { | ||
$productDocument = $this->productDocumentFactory->createFromSyliusSimpleProductModel( | ||
$product, | ||
$locale, | ||
$channel | ||
); | ||
|
||
$this->manager->persist($productDocument); | ||
|
||
++$productDocumentsCreated; | ||
if (($productDocumentsCreated % 100) === 0) { | ||
$this->manager->commit(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
$this->manager->commit(); | ||
|
||
$output->writeln('Product index was reset!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service class="Sylius\ElasticSearchPlugin\Command\ResetProductIndexCommand"> | ||
<argument type="service" id="sylius.repository.product" /> | ||
<argument type="service" id="es.manager.default" /> | ||
<argument type="service" id="sylius_elastic_search.factory.product" /> | ||
<tag name="console.command" /> | ||
</service> | ||
|
||
<service class="Sylius\ElasticSearchPlugin\Command\UpdateProductIndexCommand"> | ||
<argument type="service" id="sylius.repository.product" /> | ||
<argument type="service" id="es.manager.default" /> | ||
<argument type="service" id="sylius_elastic_search.factory.product" /> | ||
<tag name="console.command" /> | ||
</service> | ||
</services> | ||
</container> |