Skip to content
This repository has been archived by the owner on Aug 3, 2018. It is now read-only.

Commit

Permalink
Add a command to reset product index
Browse files Browse the repository at this point in the history
  • Loading branch information
pamil committed Aug 30, 2017
1 parent 03333ad commit cabdcb9
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 12 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ script:
- bin/phpspec run --no-interaction
- bin/phpunit
- tests/Application/bin/console sylius:elastic-search:update-product-index --env=test -vvv && tests/Application/bin/console sylius:elastic-search:update-product-index --env=test -vvv
- tests/Application/bin/console sylius:elastic-search:reset-product-index --env=test --force -vvv && tests/Application/bin/console sylius:elastic-search:reset-product-index --env=test --force -vvv
#- bin/behat --strict -vvv --no-interaction
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,12 @@ Whole response:
The current implementation does not support updating Elasticsearch when an entity is updated.
In order to stay up-to-date, run the following command:
```
bin/console sylius:elastic-search:update-product-index
```
If you want to recreate the index, run (it will drop it):
```
bin/console sylius:elastic-search:reset-product-index
```
104 changes: 104 additions & 0 deletions src/Command/ResetProductIndexCommand.php
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!');
}
}
14 changes: 3 additions & 11 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?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">

<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">
<imports>
<import resource="services/command.xml"/>
<import resource="services/controller.xml"/>
</imports>

<services>
<service id="sylius_elastic_search.form_registry.filters" class="Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistry" />
<service id="sylius_elastic_search.event_listener" class="Sylius\ElasticSearchPlugin\EventListener\ProductPublisher">
Expand Down Expand Up @@ -42,13 +41,6 @@
<argument>%sylius_elastic_search.view.taxon.class%</argument>
</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>

<service id="sylius_elastic_search.filter.pager" class="Sylius\ElasticSearchPlugin\Filter\Widget\Pager">
<tag name="ongr_filter_manager.filter" type="sylius_elastic_search.custom_pager" />
</service>
Expand Down
19 changes: 19 additions & 0 deletions src/Resources/config/services/command.xml
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>

0 comments on commit cabdcb9

Please sign in to comment.