Imagine my surprise when I read that Magento, an advanced ecommerce platform, doesn’t rebuild the search index when product data has changed. A client of mine had to go to Index Management every day to keep the full text search updated, and that’s not acceptable.
This is blog post 9/30 for #30DaysOfCreativity.
So I googled around for a while to find out how we could go about coding a PHP script that did this. Once we have that, we can easily run it a couple of times per day using a simple cron-job. I found one thread on the Magento Forum titled “Rebuild search index within code?”—exactly what we were looking for.
The credit goes to the user Daim for this one, so feel free to thank him in the mentioned thread or a PM. As with most things Magento, the solution is fairly simple once you’ve actually seen a working piece of code.
Here’s the complete script that rebuilds the search index. I’m merely copy & pasting it from the thread for reference, changing the success and error notifications to something more proper:
require 'app/Mage.php';
if (!Mage::isInstalled()) {
echo "Application is not installed yet, please complete install wizard first.";
exit;
}
// Only for urls
// Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);
Mage::app();
try {
Mage::getSingleton('catalogsearch/fulltext')->rebuildIndex();
echo "Search index successfully rebuilt.";
}
catch (Mage_Core_Exception $e) {
echo "Failed to rebuild search index: " . $e->getMessage();
}
catch (Exception $e) {
echo "Failed to rebuild the search index. Non-Magento exception thrown.";
}
The snippet that actually rebuilds the index is of course this one:
Mage::getSingleton('catalogsearch/fulltext')->rebuildIndex();
This is running fine on Magento Community Edition 1.4.0.1. We’ve set up a cron-job that rebuilds the index twice every day. I’m guessing it will run equally well on a Magento 1.3 installation since the post was written in June 2009 and 1.4 had not been released back then.
So if you’re running a Magento shop, I suggest you run this script or a similar one approximately as frequently as you do changes to your products. This will keep your full text search index up-to-date and your customers will be able to find what they’re looking for.
UPDATE:
While it seems to run fine in Magento 1.4, there’s a better way of doing it which is to simply run one of the shell-scripts that are provided in the /shell folder of your Magento root.
php /var/www/vhosts/your_magento_site/htdocs/shell/indexer.php reindexall
Source: Yireo.com
Hi,
I.m new to magento. now only i studying the folder structure of magento, i installed magento 1.4.1.1 could you please tell me about the use of shell and downloader folder . i.e. the shell folder is used only for the re-index or it is used for any other reason