Magento Enterprise edition comes with option to use Apache Solr as the main search engine. If you have had an opportunity to use this you will know that it far exceeds the abilities of the built in MYSQL search. Setting up Apache SOLR to work with Magento is surprisingly rather simple as Magento provides the configuration files required, however customizing how the two interact beyond the default configuration requires a much deeper understand of how Solr actually works. That being said I recently came about a shortcoming with the default Magento/Solr configuration that had me doing just that.
The shortcoming I’m referring to is searching by SKU, and depending on your product configuration it might go unnoticed. The issue arises when trying to search for a partial match on a SKU that contains a string of numbers. Basically with the default configuration, provided the SKU field will not return results for a partial search based on numbers, it treats it as an all or nothing when comparing numbers for this field. Searching for a product with a SKU of “product123456” by the exact SKU will return exactly one match, where searching by just “product” will return all products with “product” in the SKU, and yet searching for “product12345” will return no results (unless another product has that exact SKU) not even the original result which is very counter-intuitive, and not what a user would expect.
The solution I have found to work around this is to alter the Solr configuration to add new field called “sku_partial” which is a clone of the SKU field but uses a new field type with analyzer’s that will allow for partial results when searching. This can be accomplished by editing two files (solrconfig.xml and schema.xml both located in “solr/conf/” in your Solr install directory) with just 4 changes.
In schema.xml add the following changes:
1) Towards the bottom of the document inside the “schema” node add:
<copyField source="sku" dest="sku_partial" />
Note: this sets the source of our new “sku_partial field”
Note: this is the new field type which uses n-gram analyzer’s to tokenize the SKU into smaller “tokens” in order to have partial matches, so 123456 will match on 123,1234,12345 etc
4) Next in solrconf.xml locate the “requestHandler” node(s) that are used for your stores locale (search for “magento_en” for English, and “magento_fr” for French) and change the line(s) following lines from:
Note: here were adding telling Solr/Magento that it can search our new field we added
That’s it, restart Solr to load the new configuration, re-index the catalog search index to populate the new field and your ready for better search results.
BONUS: Using Stop words to improve search relevancy :
To help improve your search relevancy use what Solr refers to as “Stop words”, which is basically words that should be ignored and not matched on during a search. The configuration files that come with Magento include a stopwords.txt file with a list of the basics (“to”, “the”, “and”, “at” etc). However while working on this project I discovered that this file isn’t actually being used, and instead uses the locale specific files. So to ensure you’re getting accurate and relevant search results be sure to add your desired “Stop words” to the “stopwords_YOURLOCALE.txt” file located in your “solr/conf” directory.