3

I have some items present in an item folder and want them to retrieve from Lucence index. There is a requirement that we have to order items in the Sitecore based on the order set on the Sitecore CMS, I have to retrieve them and show on the page.

Here I'm using content search API and in the search query, I'm passing sort options based on the sortorder as below.

 #region Sort Options
  SortOption sortOption = new SortOption() { SortFieldName = "__sortorder", SortOrder = SortOrder.Ascending };
  #endregion

I did the indexing but the results are partially order by sort order field for example, I have set order as 1,2,3,4,5,6,7,8,9,10,11,12,13,14 Output of the items coming as : 1,10,11,12,13,14,2,3,4,5,6,....

Also tried another option, I have added the sort order field in my search model as below to do sort based upon the field value

 [JsonProperty]
 [IndexField("__sortorder")]
 public string sortOrder { get; set; }

But in output the sort order field value is coming but with Pipe (|) appended as shown below:

Output

Please suggest a way to retrieve the Sitecore items based on the sortorder field from the lucene indexes

1
  • 1
    That is a normal sorting for strings. You can add a computed Integer field based on regular sort order field and use it for numeric sorting.
    – Daniil
    Jul 19, 2018 at 6:51

2 Answers 2

2

Standard sortorder field is listed under the exclude fields in the default index configuration of all the providers including Lucene, Solr and Azure Search.

You have to comment out or remove it from there and then rebuild index again.

enter image description here

2
  • I have commented this line already.. Jul 20, 2018 at 11:28
  • Can you try to update the query api call add OrderBy like this var hits = query.GetResults().Hits.Select(x => x.Document) .OrderBy(x => x.Fields["__sortorder"]); Jul 20, 2018 at 11:35
1

Issue is basically sorting is happening on the sortorder field which is returning the data in string format. So to resolve this issue I have added a field in Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config file as below under <fieldNames hint="raw:AddFieldByFieldName"> root.

<field fieldName="__sortorder" storageType="YES" indexType="TOKENIZED"    vectorType="NO" boost="1f" type="System.Int32" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider"></field>

In the above line, I have added the type to int32 format then sort order field results in integer format instead of string format.

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.