Partial search for sku with solr in magento

In Magento, the default solr searching. When searching for sku, which can be a number (ex:128972997648926) or a serial number(ex: DSEGI34SW23OL31),it will take the number as it is, and will not try to do stemming or any other searching technique to analyze the number to improve the search result. This leads to no results or unrelated results when doing a partial search for a number. For example, you want to find the item with searial number DSEGI34SW23OL31, and you search for 4SW23OL31, it will not return you the item you wanted.

To make the solr search to find the item when only providing a partial sku number, we need to do some configuration in the schema.xml and the solrconfig.xml files.

First we need to add a fieldType in the types tag in schema.xml. This field type defines the indexing and querying rules to search for partial matches. Here we define the new fieldType partial_sku

		<fieldType name="partial_sku" class="solr.TextField">
			  <analyzer type="index">
				  <tokenizer class="solr.WhitespaceTokenizerFactory"/>
				  <filter class="solr.NGramFilterFactory" minGramSize="3" maxGramSize="1000" side="front" />
				  <filter class="solr.NGramFilterFactory" minGramSize="3" maxGramSize="1000" side="back" />
				  <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true"/>
				  <filter class="solr.LowerCaseFilterFactory"/>
				  <filter class="solr.TrimFilterFactory" />
				  <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
				  <filter class="solr.SnowballPorterFilterFactory" language="German" protected="protwords_de.txt"/>
			  <analyzer type="query">
				  <tokenizer class="solr.StandardTokenizerFactory"/>
				  <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0" splitOnCaseChange="1"/>
				  <filter class="solr.LowerCaseFilterFactory"/>
				  <filter class="solr.TrimFilterFactory" />
				  <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
				  <filter class="solr.SnowballPorterFilterFactory" language="English" protected="protwords_en.txt"/>

Second, we will define a new field in the fields tag in schema.xml, name it partial_sku with the type partial_sku we defined in the above.

<field name="partial_sku" type="partial_sku" indexed="true" stored="true"/>

Third, we will add a copyFiled just before the tag in schema.xml, this will copies the sku value to partial_sku.

<copyField source="sku" dest="partial_sku" />

Finally, we need to let the requestHandler in solrconfig.xml knows the new field patial_sku and give a weight to it. In the solrconfig.xml, look for requestHandler with name=”magento_en” or any other requestHandler you know you are using, add partial_sku^1.0 in the str tags with name=”qf” and name=”pf”. It should look like the below.

<str name="qf">fulltext_1_en^1.0 fulltext_2_en^2.0 fulltext_3_en^3.0 fulltext_4_en^4.0 fulltext_5_en^5.0 partial_sku^1.0</str>
<str name="pf">fulltext_1_en^1.0 fulltext_2_en^2.0 fulltext_3_en^3.0 fulltext_4_en^4.0 fulltext_5_en^5.0 partial_sku^1.0</str>

Afer we did the configurations in the schema.xml and the solrconfig, we need to restart the solr server and reindex the catalog search index, and then if we type a partil sku in the search bar, it should give the item we wanted.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search