doesn’t have indexes: • The prices in product list are calculated on the fly depending on catalog rules, tier prices for customer groups • Stock availability for configurable and bundle products can be calculated only after loading the product collection • Layered navigation data is build in real-time for product attributes information • Anchor categories recursively collects subcategories for filtering product list
data for entity representation on the frontend lists. • Indexer Generates index data on event or manual by process. • Index Event The moment when entity or related to it information is changed and that affects its index data. • Index Process Wrapper for indexer and contains information about its mode and status • Main Controller Forwards events to Index Process
indexed entity or related to it information was changed • Delete When indexed entity or related to it one was deleted • Mass Update When batch of entities was updated. (Update Attributes on Product Grid)
Pending Indicates that indexer is up to date • Running Index currently in process of full rebuilding index data. • Require Reindex Status for notifying admin user, that index is not up to date and should be rebuild.
Model Mage_Index_Model_Mysql4_Abstract Matches event data and runs appropriate method in resource model for re-indexing Works directly with database for generation of the indexed data. Usually all the data operated via MySQL queries.
Invoke reindexAll method from index model/resource model, because it is better to let admin user know when the index was rebuild. • Process entity events directly with indexer, instead of passing data through the main controller. You never know which index may depend on this event.
Adding new attribute to catalog product entity called is_featured • Creating table that will contain product ids of products that are marked as featured products.
Mage_Index_Model_Indexer_Abstract { // … other code protected function _construct() { $this->_init(‘your_module/indexer_featured'); } } Defining Indexer Resource Model Resource model
Mage_Index_Model_Indexer_Abstract { // … other code public function getName() { return Mage::helper(‘your_module')->__('Featured Product'); } public function getDescription() { return Mage::helper(‘‘your_module')->__('Indexes something'); } } Defining Indexer Information Indexer Name in the admin Indexer Description in the admin
Mage_Index_Model_Indexer_Abstract { // … other code protected function _processEvent(Mage_Index_Model_Event $event) { if ($event->getData('product_id') || $event->getData('product_ids')) { $this->callEventHandler($event); } } } Processing Event Calling processor in resource model Entity Type Event Type catalogProductSave($event) catalogProductMassAction($event)
!== null) { if (!is_array($productId)) { $productId = array($productId); } $select->where('entity_id IN(?)', $productId); $this->_getWriteAdapter()->delete( $this->getTable(‘your_module/featured'), array( 'product_id IN(?)' => $productId ) ); } else { $this->_getWriteAdapter()->truncate($this->getTable(‘your_module/featured')); } Indexing Method If it is partial re-index, then delete only related indexed data Otherwise clear all indexed data
$select->insertIgnoreFromSelect( $this->getTable(‘your_module/featured'), array('product_id') ); $this->_getWriteAdapter()->query($sqlStatement); } } Indexing Method Fulfill index data from select we created before
extends Mage_Index_Model_Mysql4_Abstract { // … other code public function reindexAll() { $this->_reindexEntity(); } } Handling Events Full index re-build
extends Mage_Index_Model_Mysql4_Abstract { // … other code public function catalogProductSave($event) { $this->_reindexEntity($event->getData('product_id')); } public function catalogProductMassAction($event) { $this->_reindexEntity($event->getData('product_ids')); } } Reindexing Events Single Save Product Event Mass Save Product Event
• Observing and event catalog_product_collection_apply_limitations_after – Joining index table to product collection select – Create sub-select filter for collection