Hey guys,

This week, we will start a new topic in Magento Indexer. Give you all the best from Magento Tutorial.

We will spend three hours learning this lesson with these sections:

  • Why Magento reindexes
  • Using the Index Management Page
  • Creating a custom indexer in Magento
  • Processing data and events in indexer

1. Why Magento reindexes

Indexing is how Magento transforms data such as products and categories to improve the performance of your web store. As data changes, the transformed data must be updated—or reindexed. Magento has a sophisticated architecture that stores merchant data (including catalog data, prices, users, stores, and so on) in many database tables. To optimize web store performance, Magento accumulates data into special tables using indexers.

For example, suppose you change the price of an item from $4.99 to $3.99. Magento must reindex the price change to display it on your web store.

Without indexing, Magento would have to calculate the price of every product on the fly—taking into account shopping cart price rules, bundle pricing, discounts, tier pricing, and so on. Loading the price for a product would take a long time, possibly resulting in cart abandonment.

2. Using the Index Management Page

To understand how indexing works and to reindex when necessary, you must log in to the Admin Panel as an administrator and click System > Index Management. The following page displays:

magento indexer

The page displays differently if you enabled the flat catalog options. (In the Admin Panel, click System >Configuration > CATALOG > Catalog. In the right pane, expand Frontend and, from the Use Flat Catalog Category and Use Flat Catalog Category lists, click Yes.)

The following figure shows an example of how the System Management page displays when the flat catalog options are enabled.

index management

The following table discusses the meaning of the columns on this page.

Column name Description
Index Name of the indexer.
Description Description of the indexer.
Mode In the preceding figures, most of the indicated indexers are set to Update on Save, which means a reindex is required when a change is saved in the Admin Panel. To change this option, see How to Manually Reindex.
Status Displays one of the following:

  • Reindex Required if a change has been made that requires reindexing. If a check box and Reindex Required link displays in that row, you can manually reindex as discussed in How to Manually Reindex.
  • Ready if the index is up-to-date.
Update Required Indicates whether or not you must manually reindex. (The value in the Mode column indicates whether the index must be manually updated.)
Updated At Displays the date and time an index was last manually updated, or Never if the index has never been updated.
Action Click the Reindex Data link to reindex that indexer only.

 

3. Creating a custom indexer in Magento

magento custom indexer

Step 1: Register indexer in Magento system

In file config.xml, add an indexer declaration

Code example: app\code\local\Magestore\Lesson29\etc\config.xml

<config>

...
<global>
<index>
<config>
</global>
...
<index>
<indexer>
<lesson29_indexer>
<model>lesson29/indexer</model>
<depends>
<catalog_url />
</depends>
</lesson29_indexer>
</indexer>
</index>
</global>
</config>

In which: 

  • Lesson29_indexer: indexer code
  • model: model used to process indexer
  • depends: require depended indexer to be processed before this indexer (main indexer) processes

Step 2: Write code to process indexer

After resgistering indexer in Magento system, create a model to process custom indexer. Note that this model must extend from Mage_Index_Model_Indexer_Abstract

Code example: app\code\local\Magestore\Lesson29\Model\Indexer.php

/**

* Indexer Model
*
* @category Magestore
* @package Magestore_Lesson29
* @author Magestore Developer
*/
class Magestore_Lesson29_Model_Indexer extends Mage_Index_Model_Indexer_Abstract {

protected$_matchedEntities=array(
'test_entity'=>array(
Mage_Index_Model_Event::TYPE_SAVE
)
);

publicfunction getName(){
return Mage::helper('lesson29')->__('Lesson 29 Indexer');
}

publicfunction getDescription(){
return Mage::helper('lesson29')->__('Lesson 29 Indexer Description');
}

protectedfunction _registerEvent(Mage_Index_Model_Event $event){
// custom register event
return$this;
}

protectedfunction _processEvent(Mage_Index_Model_Event $event){
// process index event
}

publicfunction reindexAll(){
// reindex all data
}

}

In model, we can override the following methods:

  • getName: Name of custom index
  • getDescription: description for custom index
  • _registerEvent: Event registration
  • _processEvent: Function to process event
  • reindexAll: reindex all items

4. Processing data and events in indexer

File example: app\code\core\Mage\Catalog\Model\Indexer\Url.php

This file contains processEvent function. It is used to refresh product url rewrite whenever event is started in matchEvent function.

/**

* Process event
*
* @param Mage_Index_Model_Event $event
*/
protected function _processEvent(Mage_Index_Model_Event $event)
{
$data = $event->getNewData();
if (!empty($data['catalog_url_reindex_all'])) {
$this->reindexAll();
}

/* @var $urlModel Mage_Catalog_Model_Url */
$urlModel = Mage::getSingleton('catalog/url');

// Force rewrites history saving
$dataObject = $event->getDataObject();
if ($dataObject instanceof Varien_Object && $dataObject->hasData('save_rewrites_history')) {
$urlModel->setShouldSaveRewritesHistory($dataObject->getData('save_rewrites_history'));
}

if(isset($data['rewrite_product_ids'])) {
$urlModel->clearStoreInvalidRewrites(); // Maybe some products were moved or removed from website
foreach ($data['rewrite_product_ids'] as $productId) {
$urlModel->refreshProductRewrite($productId);
}
}
if (isset($data['rewrite_category_ids'])) {
$urlModel->clearStoreInvalidRewrites(); // Maybe some categories were moved
foreach ($data['rewrite_category_ids'] as $categoryId) {
$urlModel->refreshCategoryRewrite($categoryId);
}
}
}

Lesson 29 – Magento Indexer ends here, after learning this Magento tutorial, you should know:

  • How Indexer works in Magento
  • How to create a new indexer

Thank you for reading this lesson in Magento Open Course. Hope you love our Magento Tutorial as well.

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

Write A Comment