It has been a quite long time since my last article of Magento Certificate was posted. Today I’m very happy to continue this series with a tutorial about “Custom Index”. 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:

<config>
<global>
<index>
<indexer>
<test_indexer>
<model>test/indexer</model>
<depends>
<catalog_url />
</depends>
</test_indexer>
</indexer>
</index>
</global>
...
</config>
  • 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 Magestore_Test_Model_Indexer extends Mage_Index_Model_Indexer_Abstract
{
protected $_matchedEntities = array(
'test_entity' => array(
Mage_Index_Model_Event::TYPE_SAVE
)
);

public function getName(){
return Mage::helper('test')->__('Indexer Name');
}

public function getDescription(){
return Mage::helper('test')->__('Indexer Description');
}

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

protected function _processEvent(Mage_Index_Model_Event $event){
// process index event
}

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!

Author

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

9 Comments

  1. Vinod Yadav Reply

    Great post for how to make custom indexer in Magento?

    David, I have read your all post related to Magento certification.

    I have a query for indexer.

    Which method is resposible for a full re-index in the abstract method indexer?

    • Hello,
      Thanks for reading. The method responsible for a full re-index of your custom data is reindexAll (reindexAll: reindex all items of your entity). You need to rewrite this method to re-index all your custom data.
      Let me know if you have any other question 😉

  2. Vinod Yadav Reply

    Thank for explanation,
    This question was read in a magento question set, and Answer was:

    IndexAH();

    So i confused.

    • Hi,
      Thanks for sharing with us. We’ll discover this issue more. If you have any other ideas, don’t hesitate to let me know. 🙂

  3. Hi David,
    great article! However, I think there is still a piece missing? You would have to add a custom resource model also to make the indexer work correctly, don’t you?

    • Hi Marc,
      Yes. The code process index for your custom data will be wrriten on your custom resource model. And you need to call that resource from method _processEvent, reindexAll on your indexer.

  4. how can you trigger notice to reindex custom indexer when there is update/add/delete on category or product?

    • Hi Damu,
      If you want to trigger notice to reindex, you can run command (similar in my sample):
      Mage::getModel(‘index/indexer’ ->;logEvent(
      $testModel,
      ‘test_entity’,
      Mage_Index_Model_Event::TYPE_SAVE
      );
      it will notify that you need to reindex your data

  5. Tiago Sampaio Reply

    Hi David,

    Thanks a lot for the post. It’s really very helpful.

    Regards,

    Tiago Sampaio

Write A Comment