We know that indexer is used to index EAV database (or other database models) to Flat database. Thus it makes users more easily query database and improve performance. This post will guide you how to add a custom indexer. Three main parts covered are also three main steps that you may follow.
1. Register indexer with Magento system
To register an indexer with your system, you need insert a row to table index_process as below:
Then add config to indexer to process the index. You can add the following code to config.xml file:
frontcatalog/indexer
test_indexer: indexer code of the process
model: the model to process the index
depends: require depended indexer to be processed before this indexer (main indexer) processes
2. Write code to process index
After registering indexer with your Magento system, you have to write the indexer model to process your custom index.
class Dsg_Frontcatalog_Model_Indexer extends Mage_Index_Model_Indexer_Abstract { protected $_matchedEntities = array( 'test_entity' => array(Mage_Index_Model_Event::TYPE_SAVE) ); /* (non-PHPdoc) * @see Mage_Index_Model_Indexer_Abstract::getName() * description :get custom indexer name */ public function getName(){ return Mage::helper('frontcatalog')->__('Indexer Name'); } /* (non-PHPdoc) * @see Mage_Index_Model_Indexer_Abstract::getDescription() * description : get custom indexer description */ public function getDescription(){ return Mage::helper('frontcatalog')->__('Indexer Description'); } /* (non-PHPdoc) * @see Mage_Index_Model_Indexer_Abstract::_registerEvent() * description : custom registered event */ protected function _registerEvent(Mage_Index_Model_Event $event){ // custom register event return $this; } /* (non-PHPdoc) * @see Mage_Index_Model_Indexer_Abstract::_processEvent() * used when processing the index event */ protected function _processEvent(Mage_Index_Model_Event $event){ // process index event } /* (non-PHPdoc) * @see Mage_Index_Model_Indexer_Abstract::reindexAll() * reindex all items of your entity */ public function reindexAll(){ // reindex all data } }
in this model, you may override these methods:
getName : get custom indexer name
getDescription : get custom indexer description
_registerEvent : custom registered event
_processEvent : used when processing the index event
reindexAll : reindex all items of your entity
3. Use the index
You can log in the backend, go to System > Index Management and click on Reindex Data link to process index for your custom index Or you can catch an event to process your custom index data. For example:
public function processTestSave(Varien_Event_Observer $observer){ $testModel = $observer->getEvent()->getTestEntity(); Mage::getModel('index/indexer')->processEntityAction( $testModel, 'test_entity', Mage_Index_Model_Event::TYPE_SAVE ); }
This part ends here. It’d be great to get your thoughts on my article in the comments below. Thanks for reading!