Your SlideShare is downloading. ×
0
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Magento Indexes
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Magento Indexes

21,299

Published on

Presentation from Magento MeetUp on 17th June in Amsterdam

Presentation from Magento MeetUp on 17th June in Amsterdam

Published in: Technology
0 Comments
18 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
21,299
On Slideshare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
261
Comments
0
Likes
18
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Magento Indexers<br />Ivan Chepurnyi<br />Magento Trainer / Lead Developer<br />
  • 2. Agenda<br />Magento Developers Meetup<br />Overview of Indexes Functionality<br />Creation of own indexes<br />
  • 3. Let Imagine…<br />Magento Developers Meetup<br />… that Magento doesn’t have indexes:<br />The prices in product list are calculated on the fly depending on catalog rules, tier prices for customer groups<br />Stock availability for configurable and bundle products can be calculated only after loading the product collection<br />Layered navigation data is build in real-time for product attributes information<br />Anchor categories recursively collects subcategories for filtering product list<br />
  • 4. It’s all about performance…<br />Magento Developers Meetup<br />The main goal is minimizing amount of operations to display products to a customer<br />
  • 5. Definitions<br />Magento Developers Meetup<br />Indexed DataAggregated data for entity representation on the frontend lists.<br />Indexer<br /> Generates index data on event or manual by process.<br />Index EventThe moment when entity or related to it information is changed and that affects its index data.<br />Index Process<br />Wrapper for indexer and contains information about its mode and status<br />Main Controller<br /> Forwards events to Index Process<br />
  • 6. Index Workflow<br />Magento Developers Meetup<br />Event<br />Main Controller<br />Event<br />Events<br />Process<br />Manual<br />Invoke<br />Indexer<br />Indexed Data<br />
  • 7. Event Types<br />Magento Developers Meetup<br />Save<br /> When indexed entity or related to it information was changed<br />Delete<br />When indexed entity or related to it one was deleted<br />Mass UpdateWhen batch of entities was updated. (Update Attributes on Product Grid)<br />
  • 8. Observed Entities <br />Magento Developers Meetup<br />Indexed Entities<br />Product<br />Product Inventory<br />Category<br />Tag<br />Entities Scope<br />Customer Group<br />Website<br />Store Group<br />Store View<br />
  • 9. Index Process<br />Magento Developers Meetup<br />Available Statuses<br />Pending<br /> Indicates that indexer is up to date<br />Running<br /> Index currently in process of full rebuilding index data.<br />Require ReindexStatus for notifying admin user, that index is not up to date and should be rebuild. <br />
  • 10. Index Process<br />Magento Developers Meetup<br />Indexing Modes<br />Real-time<br />Manual<br />Update Index Data<br />Event<br />Event<br />Require Reindex<br />
  • 11. Indexer Flow <br />Magento Developers Meetup<br />Match Event<br />Main Controller<br />Index Process<br />Register Event Data<br />Reindex Data<br />
  • 12. Mage_Index Module<br />Magento Developers Meetup<br />Main Controller<br />Mage_Index_Model_Indexer<br />Process<br />Mage_Index_Model_Process<br />Indexer Base<br />Mage_Index_Model_Indexer_Abstract<br />
  • 13. Index Module<br />Indexers Modularity<br />Magento Developers Meetup<br />Mage_Index_Model_Indexer_Abstract<br />Catalog Module<br />Mage_Catalog_Model_Product_Indexer_Eav<br />Mage_Catalog_Model_Product_Indexer_Flat<br />Mage_Catalog_Model_Product_Indexer_Price<br />Inventory Module<br />Mage_CatalogIndex_Model_Indexer_Stock<br />…<br />
  • 14. Model<br />Mage_Index_Model_Indexer_Abstract<br />Indexer Structure<br />Magento Developers Meetup<br />Resource Model<br />Mage_Index_Model_Mysql4_Abstract<br />Matches event data and runs appropriate method in resource model for re-indexing<br />Works directly with database for generation of the indexed data. Usually all the data operated via MySQL queries.<br />
  • 15. What can you use?<br />Magento Developers Meetup<br />Mage_Index_Model_Indexer<br />getProcessByCode($indexerCode)<br />getProcessCollection()<br />processEntityAction($entity, $entityType, $eventType)<br />Mage_Index_Model_Process<br />reindexAll()<br />reindexEverything()<br />setMode($mode)<br />
  • 16. What you shouldn’t do…<br />Magento Developers Meetup<br />Invoke reindexAll method from index model/resource model, because it is better to let admin user know when the index was rebuild.<br />Process entity events directly with indexer, instead of passing data through the main controller. You never know which index may depend on this event.<br />
  • 17. Creating own indexer<br />Magento Developers Meetup<br />Defining indexer in configuration<br />Designing index data table<br />Implementing model <br />Implementing resource model<br />Applying index on the frontend<br />
  • 18. Featured Products<br />Magento Developers Meetup<br />There is easier way to create featured products functionality, but it is a simple example on what should be done for creation own indexer.<br />
  • 19. Defining index in configuration<br />Magento Developers Meetup<br />&lt;config&gt;<br />&lt;!-- …. module configurtaions --&gt;<br /> &lt;global&gt;<br /> &lt;!-- …. module configurtaions --&gt;<br /> &lt;index&gt;<br />&lt;indexer&gt;<br /> &lt;featured_products&gt;<br /> &lt;model&gt;your_module/indexer_featured&lt;/model&gt;<br /> &lt;/featured_products&gt;<br /> &lt;/indexer&gt;<br /> &lt;/index&gt;<br /> &lt;/global&gt;<br />&lt;/config&gt;<br />etc/config.xml<br />Indexer Code <br />Indexer Model <br />
  • 20. Designing index data table<br />Magento Developers Meetup<br />Adding new attribute to catalog product entity called is_featured<br />Creating table that will contain product ids of products that are marked as featured products.<br />
  • 21. Designing index data table<br />Magento Developers Meetup<br />$this-&gt;addAttribute(&apos;catalog_product&apos;, &apos;is_featured&apos;, array(<br /> &apos;type&apos; =&gt; &apos;int&apos;,<br /> &apos;label&apos; =&gt; &apos;Is featured&apos;,<br /> &apos;input&apos; =&gt; &apos;select&apos;,<br /> &apos;source&apos; =&gt; &apos;eav/entity_attribute_source_boolean&apos;,<br /> &apos;user_defined&apos; =&gt; false,<br /> &apos;required&apos; =&gt; false<br />));<br />Setup Script<br />Attribute Code <br />Yes/No Dropdown<br />
  • 22. Designing index data table<br />Magento Developers Meetup<br />$table = new Varien_Db_Ddl_Table();<br />$table-&gt;setName($this-&gt;getTable(‘module/featured&apos;));<br />$table-&gt;addColumn(&apos;product_id&apos;, Varien_Db_Ddl_Table::TYPE_INTEGER, 11, array(<br /> &apos;unsigned&apos; =&gt; true,<br /> &apos;nullable&apos; =&gt; false,<br /> &apos;primary&apos; =&gt; true<br />));<br />$this-&gt;getConnection()-&gt;createTable($table);<br />Setup Script<br />Table Alias<br />Table Column<br />
  • 23. Designing index data table<br />Magento Developers Meetup<br />$table = new Varien_Db_Ddl_Table();<br />$table-&gt;setName($this-&gt;getTable(‘module/featured&apos;));<br />$table-&gt;addColumn(&apos;product_id&apos;, Varien_Db_Ddl_Table::TYPE_INTEGER, 11, array(<br /> &apos;unsigned&apos; =&gt; true,<br /> &apos;nullable&apos; =&gt; false,<br /> &apos;primary&apos; =&gt; true<br />));<br />$this-&gt;getConnection()-&gt;createTable($table);<br />Setup Script<br />Table Alias<br />Table Column<br />
  • 24. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br /> protected $_matchedEntities = array(<br />Mage_Catalog_Model_Product::ENTITY =&gt; array(<br />Mage_Index_Model_Event::TYPE_SAVE, <br />Mage_Index_Model_Event::TYPE_MASS_ACTION<br /> )<br />);<br />}<br />Defining Matching Events<br />Entity Type<br />Event Type<br />Event Types<br />
  • 25. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br />// … other code<br />protected function _construct()<br /> {<br /> $this-&gt;_init(‘your_module/indexer_featured&apos;);<br /> }<br />}<br />Defining Indexer Resource Model<br />Resource model<br />
  • 26. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br /> // … other code<br />public function getName()<br /> {<br /> return Mage::helper(‘your_module&apos;)-&gt;__(&apos;Featured Product&apos;);<br /> }<br /> public function getDescription()<br /> {<br /> return Mage::helper(‘‘your_module&apos;)-&gt;__(&apos;Indexes something&apos;);<br />}<br />}<br />Defining Indexer Information<br />Indexer Name in the admin<br />Indexer Description in the admin<br />
  • 27. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br /> // … other code<br />protected function _registerEvent(Mage_Index_Model_Event $event)<br /> {<br /> /* @var $entity Mage_Catalog_Model_Product */<br /> $entity = $event-&gt;getDataObject();<br /> if ($entity-&gt;dataHasChangedFor(&apos;is_featured&apos;)) {<br /> $event-&gt;setData(&apos;product_id&apos;, $entity-&gt;getId());<br /> } elseif ($entity-&gt;getAttributesData()) {<br /> $attributeData = $entity-&gt;getAttributesData();<br /> if (isset($attributeData[&apos;is_featured&apos;])) {<br /> $event-&gt;setData(&apos;product_ids&apos;, $entity-&gt;getProductIds());<br /> }<br />}<br />}<br />}<br />Register Event for Processing<br />Product Save Registering<br />Mass Action Registering<br />
  • 28. Implementing Model<br />Magento Developers Meetup<br />class Your_Module_Model_Indexer_Featuredextends Mage_Index_Model_Indexer_Abstract<br />{<br /> // … other code<br /> protected function _processEvent(Mage_Index_Model_Event $event)<br /> {<br /> if ($event-&gt;getData(&apos;product_id&apos;) || $event-&gt;getData(&apos;product_ids&apos;)) {<br /> $this-&gt;callEventHandler($event);<br /> }<br />}<br />}<br />Processing Event<br />Calling processor in resource model<br />Entity Type<br />Event Type<br />catalogProductSave($event)<br />catalogProductMassAction($event)<br />
  • 29. Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Mage_Index_Model_Mysql4_Abstract<br />{<br /> protected function _construct()<br /> {<br /> $this-&gt;_setResource(‘your_module&apos;);<br /> }<br />}<br />Define resource connection<br />Your module resource prefix<br />
  • 30. Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Mage_Index_Model_Mysql4_Abstract<br />{<br /> // … other code<br /> protected function _reindexEntity($productId = null)<br /> {<br /> $select = $this-&gt;_getReadAdapter()-&gt;select();<br /> /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */<br /> $attribute = Mage::getSingleton(&apos;eav/config&apos;)<br /> -&gt;getAttribute(&apos;catalog_product&apos;, &apos;is_featured&apos;);<br /> $select-&gt;from($attribute-&gt;getBackendTable(), &apos;entity_id&apos;)<br /> -&gt;where(&apos;value = ?&apos;, 1)<br /> -&gt;where(&apos;attribute_id = ?&apos;, $attribute-&gt;getId());<br />Indexing Method<br />Retrieving only featured product ids<br />
  • 31. Implementing Resource Model<br />Magento Developers Meetup<br />if ($productId !== null) {<br /> if (!is_array($productId)) {<br /> $productId = array($productId);<br /> }<br /> $select-&gt;where(&apos;entity_id IN(?)&apos;, $productId);<br /> $this-&gt;_getWriteAdapter()-&gt;delete(<br /> $this-&gt;getTable(‘your_module/featured&apos;),<br /> array(<br /> &apos;product_id IN(?)&apos; =&gt; $productId<br /> )<br /> );<br /> } else {<br /> $this-&gt;_getWriteAdapter()-&gt;truncate($this-&gt;getTable(‘your_module/featured&apos;));<br /> }<br />Indexing Method<br />If it is partial re-index, then delete only related indexed data<br />Otherwise clear all indexed data<br />
  • 32. Implementing Resource Model<br />Magento Developers Meetup<br />$sqlStatement = $select-&gt;insertIgnoreFromSelect(<br /> $this-&gt;getTable(‘your_module/featured&apos;),<br /> array(&apos;product_id&apos;)<br /> );<br /> $this-&gt;_getWriteAdapter()-&gt;query($sqlStatement);<br /> }<br />}<br />Fulfill index data from select we created before<br />Indexing Method<br />
  • 33. Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Mage_Index_Model_Mysql4_Abstract<br />{<br /> // … other code<br /> public function reindexAll()<br /> {<br /> $this-&gt;_reindexEntity();<br /> }<br />}<br />Handling Events<br />Full index re-build<br />
  • 34. Implementing Resource Model<br />Magento Developers Meetup<br />class Your_Module_Model_Mysql4_Indexer_Featured extends Mage_Index_Model_Mysql4_Abstract<br />{<br /> // … other code<br />public function catalogProductSave($event)<br /> {<br /> $this-&gt;_reindexEntity($event-&gt;getData(&apos;product_id&apos;));<br /> }<br />public function catalogProductMassAction($event)<br /> {<br /> $this-&gt;_reindexEntity($event-&gt;getData(&apos;product_ids&apos;));<br /> }<br />}<br />Reindexing Events<br />Single Save Product Event<br />Mass Save Product Event<br />
  • 35. Applying Index for the frontend<br />Magento Developers Meetup<br />Observing and event catalog_product_collection_apply_limitations_after<br />Joining index table to product collection select<br />Create sub-select filter for collection<br />
  • 36. Liked it?<br />Magento Developers Meetup<br />Checkout our advanced <br />training programs:<br />http://www.ecomdev.org/magento-development-training-programs/advanced<br />Follow our blog posts:<br />http://www.ecomdev.org/blog<br />
  • 37. Questions?<br />

×