Magento API: Rebuild Indexes after adding new products

14,440

Solution 1

You can use such a model in Index module.

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('reindexAll');

Since you need to rebuild all the indexes, there is no filters aplied to collection. But you can filter index processes list by set of parameters (code, last time re-indexed, etc) via addFieldToFilter($field, $condition) method.

Small Suggestion

Would be great to set indexes to manual mode while you importing the products, it will help you to speed up the import process, because some of them observe product saving event , so it takes some time. You can do it in the following way:

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
// Here goes your
// Importing process
// ................
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');

Solution 2

There are at least two circumstances that prevent indexer to reindex a product on save.

One: the "Manual update" setting in the Indexes properties you find under System, Index Management. You should set it to "Update on Save" if you want a product to be indexed upon a save.

Two: the setIsMassupdate product flag that is used, for example, in DataFlow batch import procedures in order to prevent indexer to be triggered upon each product save method call.

Hope this helps. Regards, Alessandro

Share:
14,440
Alex
Author by

Alex

Updated on July 26, 2022

Comments

  • Alex
    Alex almost 2 years

    I am currently writing a script that lets me import multiple products in magento.

    $product = Mage::getModel('catalog/product');
    $product->setSku($data['sku']);
    //etc etc
    $product->save();
    

    The product gets created perfectly but it won't show up in my frontend until I either save it in the backend (without changing anything!) OR I rebuild the indexes in the backend.

    I did a diff on the relevant database tables to see what's changing when I save the product and added those fields to my import script, but it did not have any effect. The imported product has to be OK since it shows up when I rebuild the indexes via the backend manually.

    Caching is completely disabled.

    Now my question is: How can I rebuild the indexes after importing my products?

  • Jonathan Day
    Jonathan Day over 13 years
    Great snippet, thanks Ivan. Slow imports is such a common complaint about Magento, it's great to know how to improve it!
  • Keenora Fluffball
    Keenora Fluffball almost 13 years
    Really great snippet. My import speeded up at 3/4 of the time! Thank you!
  • ben
    ben over 12 years
    Just found this, and it was a great help to me. Thanks! However, there's a typo on the reindexAll lines, $precesses should be $processes. I would edit it but StackOverflow won't allow me to make a 1 char edit.
  • Zachary Schuessler
    Zachary Schuessler about 12 years
    @Ivan Chepurnyi the answer isn't fixed. Second line of first code block, sir.