Blog

Sitecore things

Sitecore best practices, architectures, EXM, SPEAK...

Case insensitive sorting by using Solr in Sitecore

In my latest project, I faced an issue that the sorting on a page stopped working properly after the solution switched the search engine from Lucene to Solr.

The page sorted the items by the items’ names. After checking the indexed value, I found out that the item’s name field is sorted as textgeneral field type. In Solr, the textgeneral field type performs tokenization of the field value and this was the caused that the sorting didn’t work.

I also saw that Sitecore imported the string field type from Solr. The string type stores the word or sentence as an exact string, without performing tokenization, which seemed exactly what I needed here. So I explicitly set the name field to a string type. However, this didn’t solve issue, because the string field type is case sensitive.

My first thought was that I could create another computed field for the name filed that would convert the value to lowercase. But this wouldn’t be nice because if the page needs to sort by another field, I will have to create such field again… I opened the schema.xml file on the Solr server and checked all the field types defined in the file. It does have a lower case field type, but the problem is that Sitecore doesn’t support this field type out of the box. What I need to do is to add a new typematch field in my patch config file for the Solr configurations.

<typeMatch typeName="lowercase" type="System.String" fieldNameFormat="{0}_lc" settingType="Sitecore.ContentSearch.SolrProvider.SolrSearchFieldConfiguration, Sitecore.ContentSearch.SolrProvider" />

This means if a field is claimed as lowercase type in Sitecore’s Solr configuration, its indexing field name will append “_lc”.

In the Solr’s schema.xml file, I added a new dynamic field that tells Solr all the fields end with _lc will be indexed using lowercase field type.

<dynamicField name="*_lc"  type="lowercase"  indexed="true"  stored="true" />

After these changes, I set the name field to the new lowercase type in the configuration field. The sorting worked charming again.