Thursday, December 22, 2011

Reindex data in magento

There are different ways we can re-index data in magent.

Example 1: reindex data via magento admin panel.
Easiest way is to login magento admin panel, got o “Systems > Index Management” and select all indexs, action to “Reindex Data” and click on submit button.
Example 2: how to reindex data programmatically in magento?.
Magento has different 9 different indexes. If you want to refresh all of them then following code will help you.
  1. for ($i = 0; $i < = 8; $i++) {  
  2.     $process = Mage::getModel('index/process')->load($i);  
  3.     $process->reindexAll();  
  4. }  
Example 3: Out of the box data reindexing in magento?.
Create a file reindex.php and execute this file on linux command prompt. If you reindex data manually using following script it will resolve the problem like reindex is incomplete or still showing indexing from long time, etc.
PS: Change path for ‘Mage.php’ & “indexer.php” file path in following code according to environment.
  1. set_time_limit(0);  
  2. error_reporting(0);  
  3. require_once '/var/www/html/app/Mage.php';  
  4. umask(0);  
  5.   
  6. $indexer="/var/www/html/shell/indexer.php";  
  7.   
  8. if(file_exists($indexer))  
  9. {  
  10.                 $idxlist=array("catalog_product_attribute",  
  11.                                "catalog_product_price",  
  12.                                "catalog_product_flat",  
  13.                                "catalog_category_flat",  
  14.                                "catalog_category_product",  
  15.                                "catalog_url",  
  16.                                "catalogsearch_fulltext",  
  17.                                "cataloginventory_stock");  
  18.   
  19.         //reindex using magento command line  
  20.         foreach($idxlist as $idx)  
  21.         {  
  22.             echo "reindex $idx n ";  
  23.             exec("php /var/www/html/shell/indexer.php --reindex $idx");  
  24.         }  
  25.   
  26. }  
Example 4: simple command to reindex in magento?.
If you are very good in linux then go the command prompt and execute following command to reindex data.

No comments: