PHP Magento Tips

Reindex Magento Programmatically

Rate this posting:
{[['']]}

The Magento indexing mechanism is one of the core functionalities that improve a site performance.

You can find the indexing information from Magento admin under system -> Index Management:


However, when programming bulk imports of data it is ideal to stop the indexing until the import is completed. For two reasons, first the indexing will slow the process, as on each line of data imported a reindex of data will be performed. Secondly it can generate errors due to the amount of data that is constantly changing.

Switching off Indexes:

The indexes can be switched off by disabling the reindexing on save option.

Given that the index process is referenced by the variable $process to disable the reindex on save the command is

    $process->setMode(Mage_Index_Model_Process::MODE_MANUAL)->save();

This will set the indexer to 'Manual Update' Mode on Save action. Setting the indexer to "Manual Update" mode will prevent it from automatically indexing on save/edit/delete.

Switching on Indexes:

The indexes can be switched on by enabling the reindexing on save option.

Given that the index process is referenced by the variable $process to disable the reindex on save the command is

    $process->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();

This will set the indexer to 'Update on Save' Mode. Setting the indexer to 'Update on Save' mode will enable the indexing on save/edit/delete.

Reindex All:

In Magento Admin site it is common to select all the processes and run a full reindex process. To replicate this process in programming terms, one needs to iterate over all index processes and execute the command reindexEverything().

    $indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection(); 

    foreach ($indexingProcesses as $process) {

          $process->reindexEverything();

    } 

Or  you can run your custom script for Reindex All. Create file called reindexall.php inside your Magento Root directory and put below code.

<?php

// when you get status "Processing" instead of "Ready" in your index management run  this script and it's working fine
 // change the index order as per your requirement currently it's 7 for Catalog Search Index  you can set as per your requirement

 require_once 'app/Mage.php';
 umask( 0 );
 Mage :: app( "default" );

 $indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
 foreach ($indexingProcesses as $process) {
  $process->reindexEverything();
}

?> 
Running a specific index:

Indexes can be loaded using either the process ID or the process code using the functions:

by ID:

    $process = Mage::getSingleton('index/indexer')->getProcessById();
   

by CODE:

    $process = Mage::getSingleton('index/indexer')->getProcessByCode(<process_code>);
   

The process code and ID can be found by checking the table index_process.

Tag : Caching, Magento
1 Comments On "Reindex Magento Programmatically"

I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Typo3, kindly contact us http://www.maxmunus.com/contact
MaxMunus Offer World Class Virtual Instructor led training on Typo3. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us:
Name : Arunkumar U
Email : arun@maxmunus.com
Skype id: training_maxmunus
Contact No.-+91-9738507310
Company Website –http://www.maxmunus.com


Back To Top