reindexing magento programmatically.Magento re-index data by SSH or by script.
reindexing magento programmatically.Magento re-index data by SSH or by script

Good way to re-index data in magento without admin panel is SSH.Also I have been locked in admin panel and unable to reindex all data the SSH is the good practice to do.Sometime if we don’t have access to SSH then we can do Reindexing magento programmatically.So please follow details instruction at first I have explained in the way of SSH and latter I have explained how we can managed through script or Programmatically.So lets see how to Manage magento re-index data by SSH or by programmatically.

SSH Instruction:


1: First you can login into your SSH connection and then go to shell folder of magento installation.

cd shell
//if you are in instaltion directory.


2: Then get full list of magento indexer command.

php -f indexer.php help


Out put will be like :


Usage:  php -f indexer.php -- [options]

  --status             Show Indexer(s) Status
  --mode               Show Indexer(s) Index Mode
  --mode-realtime      Set index mode type "Update on Save"
  --mode-manual        Set index mode type "Manual Update"
  --reindex            Reindex Data
  info                          Show allowed indexers
  reindexall                    Reindex Data by all indexers
  help                          This help

       Comma separated indexer codes or value "all" for all indexers


3: Then do reindex data by.

php -f indexer.php reindexall


4: If you like to reindex for some specific process then first check all reindex code available in magento.Basicall by default there are 9 reindex code present in magento.

php -f indexer.php info


Out put will be like :

    catalog_product_attribute     Product Attributes
    catalog_product_price         Product Prices
    catalog_url                   Catalog URL Rewrites
    catalog_product_flat          Product Flat Data
    catalog_category_flat         Category Flat Data
    catalog_category_product      Category Products
    catalogsearch_fulltext        Catalog Search Index
    cataloginventory_stock        Stock Status
    tag_summary                   Tag Aggregation Data


5: so finally you can do your reindex process one by one if you like to do for specific process.

php indexer.php --reindex catalog_product_attribute
    php indexer.php --reindex catalog_product_price
    php indexer.php --reindex catalog_url
    php indexer.php --reindex catalog_product_flat
    php indexer.php --reindex catalog_category_flat
    php indexer.php --reindex catalog_category_product
    php indexer.php --reindex catalogsearch_fulltext
    php indexer.php --reindex cataloginventory_stock
    php indexer.php --reindex tag_summary

Programmatically instructions:

Generally by default magento provides 9 index processes. You can see the above result which I have shown the output from SSH command or you can see in index_process table.If you would like to update programmatically the use the indexer process codes which I have explained individually.

  1. catalog_product_attribute : Product Attributes (If you updated attributes which are used layered navigation)
  2. catalog_product_price : Product Prices (If you updated prices)
  3. catalog_url : Catalog URL Rewrites (If you updated Product or Catalag url)
  4. catalog_product_flat : Product Flat Data (If in your store you have used Flat tables and you updated product attributes which are used frontend logic)
  5. catalog_category_flat : Category Flat Data (If you updated category attributes)
  6. catalog_category_product : Category Products (If you added or removed products from catalog)
  7. catalogsearch_fulltext : Catalog Search Index (If you updated searchable attributes of product)
  8. cataloginventory_stock : Stock Status (If you updated stock options of product)
  9. tag_summary : Tag Aggregation Data (If you updated Product tag)

So lets start our coding part:

first you have to fetch the indexer model by Mage::getModel(‘index/indexer’) and use above code in the function getProcessByCode() as parameters.So finale code will be


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('some_indexer_code_given_above')
    $indexer->reindexEverything();

Examples:

For Product Attributes:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('catalog_product_attribute')
    $indexer->reindexEverything();

For Product Prices:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('catalog_product_price')
    $indexer->reindexEverything();

For Catalog URL Rewrites:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('catalog_url')
    $indexer->reindexEverything();

For Product Flat Data:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('catalog_product_flat')
    $indexer->reindexEverything();

For Category Flat Data:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('catalog_category_flat')
    $indexer->reindexEverything();

For Category Products:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('catalog_category_product')
    $indexer->reindexEverything();

For Catalog Search Index:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('catalogsearch_fulltext')
    $indexer->reindexEverything();

For Stock Status:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('cataloginventory_stock')
    $indexer->reindexEverything();

For Tag Aggregation Data:


    $indexer = Mage::getModel('index/indexer')->getProcessByCode('tag_summary')
    $indexer->reindexEverything();

Also if you would like to refresh all at once then use the following code.


    for ($i = 0; $i < = 8; $i++) {  
        $process = Mage::getModel('index/process')->load($i);  
        $process->reindexAll();  
    }

And also after doing everything flush all the cache to get better result by .

    Mage::app()->getCacheInstance()->flush();
    Mage::app()->cleanCache();

Final script:


< ?php
    require_once("app/Mage.php");
    Mage::app('default');
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    try{
        $indexerByShell = Mage::getBaseDir('shell/indexer.php');
        if(file_exists($indexerByShell))  
        {  
            $indexListByCode = array(
                           "catalog_product_attribute",
                           "catalog_product_price",
                           "catalog_product_flat",
                           "catalog_category_flat",
                           "catalog_category_product",
                           "catalog_url",
                           "catalogsearch_fulltext",
                           "cataloginventory_stock"
                    );
            //reindex using magento command line  
            foreach($indexListByCode as $indexer)  
            {  
                echo "reindex $indexer \n ";  
                exec("php $indexerByShell --reindex $indexer");  
            }
        }
    }catch(Exception $e){
        echo $e;
    }
?>

Reindexing in magento programmatically
Tagged on:                                         

One thought on “Reindexing in magento programmatically

  • September 21, 2016 at 1:42 am
    Permalink

    This rebuilds the whole table. Is there a way to just update the entries in index_event table?

    How would you write a shell script to just process the index_event table?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *