Solving Magento Fulltext Search Index for Child SKUs
As an experienced developer, I understand your frustration when core features like searching configurable products by child SKU aren’t part of the stock Magento functionality. Let’s walk through a practical solution that indexes child SKUs properly in the fulltext search index.
Understanding the Problem
Magento’s default implementation in app/code/core/Mage/CatalogSearch/Model/Resource/Fulltext.php only indexes specific types of attributes (int, varchar, text, decimal, datetime). Unfortunately, the “sku” attribute is considered a “static” attribute and isn’t included by default.
A Proper Solution: Indexing Child SKUs
The key to solving this lies in the product entity table where child SKUs are stored. We’ll need to extend Magento’s core functionality to include these static attributes in the fulltext index.
Step 1: Override the Fulltext Resource Model
Create a new module (let’s call it “ImprovedSearch”) and override the Mage_CatalogSearch_Model_Resource_Fulltext model. In your ImprovedSearch module, create a file named Fulltext.php:
<?php
class ImprovedSearch_Model_Resource_Fulltext extends Mage_CatalogSearch_Model_Resource_Fulltext
{
/**
* Get searchable attributes for fulltext index
*
* @param string $type
* @return array
*/
protected function _getSearchableAttributes($type)
{
if ($type == 'static') {
return array('sku'); // Add the SKU attribute here
}
return parent::_getSearchableAttributes($type);
}
}
Step 2: Update the Search Configuration
In app/etc/modules/ImprovedSearch.xml, add the following configuration to ensure your new model is used:
<config>
<modules>
<ImprovedSearch>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_CatalogSearch/>
</depends>
</ImprovedSearch>
</modules>
<global>
<models>
<catalogsearch>
<class>ImprovedSearch_Model</class>
<resourceModel>catalogsearch_resource</resourceModel>
</catalogsearch>
<catalogsearch_resource>
<class>ImprovedSearch_Model_Resource</class>
<entities>
<fulltext>
<table>catalogsearch_fulltext</table>
<columns>
<entity_id>entity_id</entity_id>
<attribute_id>attribute_id</attribute_id>
<store_id>store_id</store_id>
<title>title</title>
<description>description</description>
<sku>sku</sku> <!-- Add the SKU column here -->
</columns>
</fulltext>
</entities>
</catalogsearch_resource>
</models>
</global>
</config>
Step 3: Rebuild the Index
To apply these changes, you’ll need to rebuild the fulltext index:
- Log in to your Magento admin panel.
- Navigate to System > Index Management.
- Click “Reindex all” or select the “Catalog Search Index” and click “Reindex”.
Conclusion
By extending the core functionality as shown above, you can successfully index child SKUs in your Magento fulltext search. This approach properly addresses your requirement without resorting to workarounds like adding hidden attributes.
Remember to thoroughly test these changes in a development environment before deploying them to your production site. If you need further assistance or want to learn more about optimizing Magento’s search functionality, feel free to explore our Magento Development Services at Laravel Company.
Happy coding!