Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix live indexing of inventory and prices into search engine after legacy Magento reindex. #2

Merged
merged 3 commits into from
Mar 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/elasticsuite-catalog/Plugin/Indexer/AbstractIndexerPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile_ElasticSuiteCatalog
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticSuiteCatalog\Plugin\Indexer;

/**
* Generic indexer plugin, handling fulltext index process
*
* @category Smile
* @package Smile_ElasticSuiteCatalog
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class AbstractIndexerPlugin
{
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;

/**
* ReindexProductsAfterSave constructor.
*
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry The indexer registry
*/
public function __construct(\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry)
{
$this->indexerRegistry = $indexerRegistry;
}

/**
* Process full-text reindex for product ids
*
* @param mixed $ids The product ids to reindex
*/
protected function processFullTextIndex($ids)
{
$fullTextIndexer = $this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID);

if (!$fullTextIndexer->isScheduled()) {
if (is_array($ids) && (!empty($ids))) {
$fullTextIndexer->reindexList($ids);
}
if (!is_array($ids) && is_numeric($ids)) {
$fullTextIndexer->reindexRow($ids);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticSuiteCatalog\Plugin\Indexer\Category\Save;
use Smile\ElasticSuiteCatalog\Plugin\Indexer\AbstractIndexerPlugin;

/**
* Plugin that proceed products reindex after category reindexing
Expand All @@ -19,23 +20,8 @@
* @package Smile_ElasticSuiteCatalog
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class ReindexProductsAfterSave
class ReindexProductsAfterSave extends AbstractIndexerPlugin
{
/**
* @var \Magento\Framework\Indexer\IndexerRegistry
*/
private $indexerRegistry;

/**
* ReindexProductsAfterSave constructor.
*
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry The indexer registry
*/
public function __construct(\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry)
{
$this->indexerRegistry = $indexerRegistry;
}

/**
* Reindex category's products after reindexing the category
*
Expand All @@ -51,10 +37,8 @@ public function aroundReindex(
) {
$proceed();

$fullTextIndexer = $this->indexerRegistry->get(\Magento\CatalogSearch\Model\Indexer\Fulltext::INDEXER_ID);

if (!$fullTextIndexer->isScheduled() && (!empty($subject->getAffectedProductIds()))) {
$fullTextIndexer->reindexList($subject->getAffectedProductIds());
if (!empty($subject->getAffectedProductIds())) {
$this->processFullTextIndex($subject->getAffectedProductIds());
}

return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile_ElasticSuiteCatalog
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticSuiteCatalog\Plugin\Indexer\Price;

use Smile\ElasticSuiteCatalog\Plugin\Indexer\AbstractIndexerPlugin;

/**
* Price indexer operations related plugin.
* Used to index products into ES after their price information are indexed by legacy Magento indexer.
*
* @category Smile
* @package Smile_ElasticSuiteCatalog
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class ReindexProductsAfterPriceUpdate extends AbstractIndexerPlugin
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* Process row indexation into ES after the precedent stock index
*
* @param \Magento\Catalog\Model\Indexer\Product\Price $subject The Price indexer
* @param \Closure $proceed The ::execute() function of $subject
* @param int[] $productIds The product ids being reindexed
*
* @return void
*/
public function aroundExecute(
\Magento\Catalog\Model\Indexer\Product\Price $subject,
\Closure $proceed,
$productIds
) {
$proceed($productIds);

$this->processFullTextIndex($productIds);

return;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* Process row indexation into ES after the precedent stock index
*
* @param \Magento\Catalog\Model\Indexer\Product\Price $subject The Price indexer
* @param \Closure $proceed The ::executeRow() function of $subject
* @param int $productId The product id being reindexed
*
* @return void
*/
public function aroundExecuteRow(
\Magento\Catalog\Model\Indexer\Product\Price $subject,
\Closure $proceed,
$productId
) {
$proceed($productId);

$this->processFullTextIndex($productId);

return;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* Process list indexation into ES after the precedent stock index
*
* @param \Magento\Catalog\Model\Indexer\Product\Price $subject The Price indexer
* @param \Closure $proceed The ::execute() function of $subject
* @param int[] $productIds The product ids being reindexed
*
* @return void
*/
public function aroundExecuteList(
\Magento\Catalog\Model\Indexer\Product\Price $subject,
\Closure $proceed,
array $productIds
) {
$proceed($productIds);

$this->processFullTextIndex($productIds);

return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* DISCLAIMER
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
* @category Smile
* @package Smile_ElasticSuiteCatalog
* @author Romain Ruaud <romain.ruaud@smile.fr>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticSuiteCatalog\Plugin\Indexer\Stock;
use Smile\ElasticSuiteCatalog\Plugin\Indexer\AbstractIndexerPlugin;

/**
* Stock (CatalogInventory) indexer operations related plugin.
* Used to index products into ES after their stock information are indexed by legacy Magento CatalogInventory indexer.
*
* @category Smile
* @package Smile_ElasticSuiteCatalog
* @author Romain Ruaud <romain.ruaud@smile.fr>
*/
class ReindexProductsAfterStockUpdate extends AbstractIndexerPlugin
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* Process row indexation into ES after the precedent stock index
*
* @param \Magento\CatalogInventory\Model\Indexer\Stock $subject The CatalogInventory indexer
* @param \Closure $proceed The ::execute() function of $subject
* @param int[] $productIds The product ids being reindexed
*
* @return void
*/
public function aroundExecute(
\Magento\CatalogInventory\Model\Indexer\Stock $subject,
\Closure $proceed,
$productIds
) {
$proceed($productIds);

$this->processFullTextIndex($productIds);

return;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* Process row indexation into ES after the precedent stock index
*
* @param \Magento\CatalogInventory\Model\Indexer\Stock $subject The CatalogInventory indexer
* @param \Closure $proceed The ::executeRow() function of $subject
* @param int $productId The product id being reindexed
*
* @return void
*/
public function aroundExecuteRow(
\Magento\CatalogInventory\Model\Indexer\Stock $subject,
\Closure $proceed,
$productId
) {
$proceed($productId);

$this->processFullTextIndex($productId);

return;
}

/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* Process list indexation into ES after the precedent stock index
*
* @param \Magento\CatalogInventory\Model\Indexer\Stock $subject The CatalogInventory indexer
* @param \Closure $proceed The ::execute() function of $subject
* @param int[] $productIds The product ids being reindexed
*
* @return void
*/
public function aroundExecuteList(
\Magento\CatalogInventory\Model\Indexer\Stock $subject,
\Closure $proceed,
array $productIds
) {
$proceed($productIds);

$this->processFullTextIndex($productIds);
}
}
10 changes: 9 additions & 1 deletion src/elasticsuite-catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@
<type name="Magento\CatalogSearch\Block\Result">
<plugin name="smile_es_spellchecker" type="Smile\ElasticSuiteCatalog\Block\Plugin\ResultPlugin" />
</type>

<type name="\Magento\Catalog\Model\Category">
<plugin name="smile_elasticsuite_catalog_reindex_products_after_category_reindex" type="\Smile\ElasticSuiteCatalog\Plugin\Indexer\Category\Save\ReindexProductsAfterSave" />
</type>

<type name="Magento\CatalogInventory\Model\Indexer\Stock">
<plugin name="smile_elasticsuite_catalog_reindex_products_after_stock_reindex" type="\Smile\ElasticSuiteCatalog\Plugin\Indexer\Stock\ReindexProductsAfterStockUpdate"/>
</type>

<type name="Magento\Catalog\Model\Indexer\Product\Price">
<plugin name="smile_elasticsuite_catalog_reindex_products_after_price_reindex" type="\Smile\ElasticSuiteCatalog\Plugin\Indexer\Price\ReindexProductsAfterPriceUpdate"/>
</type>

</config>