I'm working on adding an additional column to a grid in an existing backend extension. I need to add an indexer to to this extension indexing the health (the amount of sells) of (roughly) the last 3 months. (90 days.) I'm splitting the health per month. (So, a field each for 1-30, 31-60 and 61-90 days ago.)
Having basically zero knowledge of Magento indexers I decided to create a simple featureless extension (a functioning adminhtml page and nothing more) and attempt to implement an indexer on that to learn how indexing works.
I've already looked at a few guides trying to figure out how to do this but none of them have fully functioning examples.
- Mexbs: Creating custom Magento Index https://archive.is/rE0oz
- AionHill: How to implement extremely fast custom indexing in Magento?
https://archive.is/LMiF6 - Magento Stack Exchange: Custom Indexer, Magento way ?
Custom Indexer, Magento way ? - Stack Overflow: Magento – Add index to table column using upgrade
script
https://stackoverflow.com/questions/27009691/magento-add-index-to-table-column-using-upgrade-script
They include code needed to create an indexer without detailing how to implement it.
How would I create an indexer, implement it and use it for this kind of data? What type of index do I need to index this information?
I'm looking for an explanation of Magento indexes and a full implementation. Being able to get an index up and running with this kind of data in either extension works for me.
Best Answer
In order to declare an index you need to add this to
config.xml
inside theglobal
tag:The
model
tag in the code above contains the alias for your indexer model. It's similar to the one you use forMage::getModel(...)
.Then you need to create the class referenced in the
model
tag above:If your index needs to run only from the indexes screen or command line then you are set.
If you need your index to be triggered at different events it gets more complicated.
You need to implement the methods that I left blank above
_registerEvent
and_processEvent
.But these are really specific depending on your events.
You can take a look at these methods from the core indexers to see what happens.
The flat product indexer for example: https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Catalog/Model/Product/Indexer/Flat.php