Schedule Index Refresh in Magento Enterprise

In CE editions of Magento there is a way to refresh a particular index by navigating to the Index Management section of the administration interface and rebuilding it there and then, through the web server.

In later versions of EE this functionality was removed in favour of incremental indexing. This is a great idea in principle, allowing the system to fully manage the state of the indexes.

Unfortunately, we ran into a problem where we needed to schedule a full rebuild of the price, stock and category products index outside of Magento. While I don’t doubt this was down to another component of the system misbehaving and not updating the index changelog appropriately, we were left in a situation where we couldn’t trigger a full index rebuild.

To achieve the refresh we manipulated the materialised view component of EE by marking the index as invalid and resetting its changelog version.

<?php

require_once __DIR__ . "/app/Mage.php";

Mage::app();

$tables = [
    'catalog_category_product_cat',
    'catalog_category_product_index',
    'cataloginventory_stock_status',
    'catalog_product_index_price'
];

foreach ($tables as $table) {
    /* @var $client Enterprise_Mview_Model_Client */
    $client = Mage::getModel('enterprise_mview/client');
    $client->init($table);

    $metadata = $client->getMetadata();
    $metadata->setInvalidStatus();
    $metadata->setVersionId('');
    $metadata->save();
}