5

I can reindex a specific indexer method by following way in Magento 1.x

$process = Mage::getModel('index/indexer')->getProcessByCode('catalog_product_price');
$process->reindexAll();

How to reindex catalog_product_price indexer in Magento 2.x

1 Answer 1

3

We can try with Magento\Framework\Indexer\IndexerInterfaceFactory.

    /**
     * @var \Magento\Framework\Indexer\IndexerInterfaceFactory
     */
    protected $indexerFactory;


    /**
     * @param IndexerInterfaceFactory $indexerFactory
     */
    public function __construct(
        \Magento\Framework\Indexer\IndexerInterfaceFactory $indexerFactory,
    ) {
        $this->indexerFactory = $indexerFactory;

    }

    /**
     * @param string $indexerId
     * @return \Magento\Framework\Indexer\IndexerInterface
     */
    public function getIndexer($indexerId)
    {
        return $this->indexerFactory->create()->load($indexerId);
    }

    public function executeIndex() {

        this->getIndexer('catalog_product_price')->reindexAll();
    }
10
  • I already have a custruct function: public function __construct( \Magento\Framework\App\Action\Context $context) { return parent::__construct($context); } How to combine it with your construct function? Commented Dec 7, 2016 at 12:57
  • Need to add \Magento\Framework\Indexer\IndexerInterfaceFactory to the current constructor. Commented Dec 7, 2016 at 12:59
  • Will I have to call executeIndex function anywhere or it will be called automatically? Actually I have execute() method and within this function I want to reindex "catalog_product_price". Commented Dec 7, 2016 at 13:06
  • Technically, you can call $this->indexerFactory->create()->load('catalog_product_price')->reindexAll() in the execute method. Commented Dec 7, 2016 at 13:08
  • How can I reindex all indexer method I mean I want to reindex all instead of specific indexer method Commented Dec 7, 2016 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.