4

When bringing back items from Solr via the ContentSearch API it's not possible to maintain the content tree sort order as this isn't returned as a field by default.

How do I order by results by the sortorder field?

Further Details: Sitecore 8.1 update 2

2 Answers 2

10

I did this before on Lucene by removing the __sortorder field from the excluded fields list. And rebuild the index. I would assume this to work in Solr as well. Your config patch would look like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
          <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
            <exclude hint="list:AddExcludedField">
              <__SortOrder><patch:delete /></__SortOrder>
            </exclude>
          </documentOptions>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>

This is changing your default Solr config by adding an extra field. But it's a ootb field instead of a computed one.

6

As the sort order field isn't returned by default the best way to handle this is to add a add a new computed field to the index. This will then be available to query by in Solr.

1. Computed Index Field

Create this class within your Sitecore project:

    public class SortOrderField : IComputedIndexField
    {
        public object ComputeFieldValue(IIndexable indexable)
        {
            int DefaultSortOrderValue = 0;

            var item = (Item)(indexable as SitecoreIndexableItem);
            if (item == null) return null;

            if (string.IsNullOrEmpty(item[Sitecore.FieldIDs.Sortorder]))
            {
                return DefaultSortOrderValue;
            }

            int sortOrder;
            return int.TryParse(item[Sitecore.FieldIDs.Sortorder], out sortOrder)
                ? sortOrder
                : DefaultSortOrderValue;
        }

        public string FieldName { get; set; }

        public string ReturnType { get; set; }
    }

2. Config for Computed Field

Add this config patch file (or merge it into an existing search patch file):

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <contentSearch>
            <indexConfigurations>
                <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
                    <fields hint="raw:AddComputedIndexField">
                        <field fieldName="sortorder" returnType="int">MyCustom.Search.Indexing.SortOrderField, MyCustom.Search</field>
                    </fields>
            </indexConfigurations>
        </contentSearch>
    </sitecore>
</configuration>

3. Search Result Item Model

Add the sort order into either your base Search Result Item model so it's available for all search result types or a specific model like so:

public class ContentSearchItem : SearchResultItem
    {
        [IndexField("_displayname")]
        public string DisplayName { get; set; }

        [IndexField("__smallupdateddate")]
        public DateTime LastUpdated { get; set; }

        [IndexField("sortorder")]
        public int SortOrder { get; set; }
    }

4. Update you search query

You should now be able to update your search query to include the ordering by sortorder like so:

using (var context = ContentSearchManager.CreateSearchContext((SitecoreIndexableItem) indexItem))
    {
        var searchResultItems = context.GetQueryable<ContentSearchItem>()
            .OrderBy(x => x.SortOrder)
}

5. Rebuild your indexes

You will need to re-build your web index (and master if you want to see the changes on CA) in order for the new sortorder field to show up in the index. You may also need to re-order content in Content Editor so that the sortorder field value is set (or perhaps use Sitecore Powershell to do this for you).

There is more info here and additional steps you may need to carry out depending on your situation (thanks to Marta Imos-Merska for her post):

https://sitecore.unic.com/2016/07/07/contentsearch-instead-of-sitecore-queries-with-content-tree-order

4
  • 1
    Wouldn't it be easier to remove <__SortOrder>{BA3F86A2-4A1C-4D78-B63D-91C2779C1B5E}</__SortOrder> as an excluded field? Done that before on Lucene and worked fine.
    – Gatogordo
    Commented Dec 18, 2017 at 13:42
  • @Gatogordo I think the reason we didn’t do this is we didn’t want to modify the default solr configs but I’ll check and see. Commented Dec 18, 2017 at 13:52
  • 1
    You are updating the defaultSolrIndexConfiguration anyway.. ;)
    – Gatogordo
    Commented Dec 18, 2017 at 13:53
  • Good point :). I’ll check and see. Commented Dec 18, 2017 at 13:54

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.