Magento Reindex Programmatically

The Magento indexing mechanism is one of the core functionalities that improve a site performance. 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

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

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

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

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().

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

Running a specific index

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

by ID:

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

by CODE:

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

The process code and ID can be found by checking the table index_process. The Magento provided indexes normally have the following ID and code.

Process NameIDCode
Product Attributes1catalog_product_attribute
Product Prices2catalog_product_price
Catalog URL Rewrites3catalog_url
Product Flat Data4catalog_product_flat
Category Flat Data5catalog_category_flat
Category Products6catalog_category_product
Catalog Search Index7catalogsearch_stock
Stock Status8cataloginventory_stock
Tag Aggregation Data9tag_summary

 

References:

Be Sociable, Share!
  • you can also reindex from the terminal using:

    php /path/to/magento/shell/indexer.php reindexall

    • Anonymous

      Thanks for sharing the information. Yes it is actually possible to reindex everything or a single index from the terminal.

      To reindex just one index from the terminal the command is:
      php /path/to/magento/shell/indexer.php –reindex 

  • Dushyantjoshia

    Thanx,it worked

  • Pingback: terrance()

  • Joe Harman

    Maybe you can give me asome guidance, we are running our Magento webservers on AWS. Magento is running a multisite setup.

    1 Load Balancer Connected to 4 Apache Webservers, 1 MySQL database running Percona MySQL

    When using php indexer.php –reindexall and monitoring the incoming queries to MySQL I noticed that my queries were originating from all 4 of the webservers, even though I only started the indexing process on 1 of the webservers.

    Do you know how isolate the indexing process to only the server it is initiated on? Have you found any performance differences in running these as comma delimited (php indexer.php –reindex catalog_product_price,catalog_url,catalog_product_flat)?

  • zambala.net

    How do you reset Indexing back to zero again?
    The URLs for some of the products have become ridiculously large
    “……/boots/kangaroo-craftsman-4905/”
    I would like to reset them and flush the Database without having to reenter products so that it reads “”……/boots/kangaroo-craftsman/” and empties the bloated tables of all the unnecessary records.

    • I don’t know your installation to guide you properly, so please take the following advise with caution and make sure to run the steps on a local environment (development one) first.

      The answer to your issue is to remove the entries from the Catalog URL Rewrites table and update the Product URL keys (if it is set in the backend). You can do the former from the database, by truncating or deleting specific records in core_url_rewrite, or using the Magento backend (daunting but safer). You will need to reindex the Catalog URL Rewrites index to regenerate the entries. Touching the database always carries some risk, so make sure to backup your database or the tables being touched. You might also want to reference the following thread https://magento.stackexchange.com/questions/84314/catalog-url-rewrites-error.

      However, I think you have another problem rather than the indexes. When a reindex is performed, the products are not updated. It shouldn’t be creating new URL keys for your products. A new URL key is created when either the product name or a new product with an existing name is created. You should check whether you have any duplicate products or if you have any processes that are updating the product names.

      Note that most likely all the product updates (the increments in the suffix number you are adding multiple internal redirects. This can be one of the issues with the performance.

  • Keep in mind that you have to call “Mage::dispatchEvent($indexerCode . ‘_shell_reindex_after’);” when using Magento Enterprise.
    The enterprise catalog module only uses the dummy indexer (Enterprise_Catalog_Model_Index_Dummy_Category) and triggers the correct indexing only on this event call.