Sitecore Insiders
__sortorder field

SXA Search Results sorting by __Sortorder field

When it comes to sorting SXA Search Results, Sitecore has many ways of sorting the results.

But what if the user wants to sort the results like they are sorted in the content tree?

The items in the Content tree are sort by the “__Sortorder” field when we move an item, and we can do the same in the search results.

First, we have to add the field to the index, by adding the following patch.

<sitecore>
    <contentSearch search:require="solr">
<indexConfigurations>
        <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
          <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
            <fieldNames hint="raw:AddFieldByFieldName">              
              <field fieldName="__Sortorder" returnType="long" />
            </fieldNames>
          </fieldMap>
 </defaultSolrIndexConfiguration>
      </indexConfigurations>
  </contentSearch>
  </sitecore>

But that’s not enough because this field is being excluded from the index by default, so we must remove it from this list, by applying the following.

 <exclude hint="list:AddExcludedField">
              <__SortOrder>
                <patch:delete/>
              </__SortOrder>
            </exclude>

The final patch will look like this.

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
  <sitecore>
    <contentSearch search:require="solr">
      <indexConfigurations>
        <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
          <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
            <fieldNames hint="raw:AddFieldByFieldName">              
              <field fieldName="__Sortorder" returnType="long" />
            </fieldNames>
          </fieldMap>
          <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
            <exclude hint="list:AddExcludedField">
              <__SortOrder>
                <patch:delete/>
              </__SortOrder>
            </exclude>
          </documentOptions>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

Now we create our facet to point it to the _Sortorder field

Integer Facet Sortorder for Search Results

Add the facet to the Sorting Group

Include Facet with _Sortorder field in Sortorder Group

And select it on the Search Results component properties.

Search Results component properties

And here you have it, the results are now being sorted with the same order as the content tree

SXA Search Results with ordered results with the same order as in the Content tree
Content Tree items

By changing the order of an item the order in the SXA Search Results component will also change.

Change order of the items in the Content Tree changing the value of __Sortorder field
Results order changed.

change order for Search Results

Jorge Pimenta

Certified Sitecore Devoloper since 2017
Sitecore Hackathon 2019 winner in the Best Sitecore Experience Accelerator Module (Team Noesis)
Always seeking to do things more practical and smarter rather than harder.

2 comments