2

When trying sort search results in Solr using title (alphabetical) asc or desc does not work as expected and not sorted.

enter image description here

2 Answers 2

3

title_t is a text_general field in Solr. Which is tokenized field so sorting will not work. If you want sorting then the field needs to be of string type (with _s suffix)

You can try a few things-

  1. Create a computed index field that is of string type and apply sort on that.
  2. Convert the title field from text to string via Sitecore patch config and apply sort
  3. Create a copyField in Solr from text to string and then apply sort order on the string field.
2

I agree with @Surya and just putting brief details to provide more context to achieve it.

Solution 1

When you want to sort on a specific field, always make sure the field isn’t tokenized within Solr, and that the casing of the values is always the same, Test would be sorted differently then test.

This means that when you want to sort on Title, you should add an extra field that isn’t tokenized, and that is lowercase. You can create a Computed Field. so that the Solr schema does not have to be edited manually.

First of all, we create a Computed Field class, which uses the ReferenceField as a reference to the field from where it should take the value from.

namespace Example
{
    public class SortReferenceComputedField : IComputedIndexField
    {
        public string FieldName { get; set; }
        public string ReturnType { get; set; }
        public string ReferenceField { get; set; }
        public object ComputeFieldValue(IIndexable indexable)
        {
            var indexItem = indexable as SitecoreIndexableItem;
            var field = indexItem?.Item?.Fields[ReferenceField];
            if (field != null && field.HasValue)
            {
                return field.Value.ToLower();
            }
            return null;
        }
    }
}

After that, all we have to do is register the Computed Field by adding a configuration file. Please note that you can use the same SortReferenceComputedField type for each field that you want to be able to sort on.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:search="http://www.sitecore.net/xmlconfig/search/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="Standalone or ContentManagement or ContentDelivery" search:require="solr">
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
          <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
            <fields hint="raw:AddComputedIndexField">
              <field fieldName="sorttitle" referenceField="title" storageType="no" indexType="untokenized">Example.SortReferenceComputedField, Example</field>
            </fields>
          </documentOptions>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

As you can see in the example above, we create a new field called sorttitle that uses the value from the title field. For the last step, just go to the Title Asc and Title Desc sortings, and make sure that the facet that is used there, points to the newly created sorttitle field.

Solution 2 (Not recommended)

Another way is directly update the managed-schema within Solr.

It is possible with Solr, to create a new field type with the LowerCaseFilterFactory and the TrimFilterFactory as indexing analyzers, and then use a copyField definition to create the field.

Note: This is not a recommended way as you have to (manually) edit the managed-schema within Solr and changes will be overwritten when the next time managed-schema is populated from Sitecore, cumbersome to maintain the manual schema changes.

Hope it helps!

2
  • thanks it works for me , but there's odd behavior when page load first time search result displays only 3 items while i configured page size parameter to be 8 , do you have any idea? Aug 2, 2023 at 18:49
  • I'm glad to hear that it works for you, If this answer or any solved your issue or helps to resolve it, please mark it as accepted for the community. For more details sitecore.stackexchange.com/help/someone-answers Aug 3, 2023 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.