この記事では、アイテムの並べ替え順序(SortOrder)で検索結果をソートする推奨方法について記載しています。計算インデックス フィールドを使用すると、コンテンツ ツリー内のアイテムと同じ順序で検索結果を表示するようにソートをカスタマイズすることができます。
アイテムの並べ替え順序による検索結果の順序付けは、すべての検索結果が同じ親アイテムを共有する場合のみ、つまり検索式が以下を含む場合のみ意味をなすことに注意してください:
.Where(item => item.Parent == parentItem.ID);
カスタム計算インデックス フィールドを作成するには、以下を実施します。
public object ComputeFieldValue(Sitecore.ContentSearch.IIndexable indexable) { Sitecore.Data.Items.Item item = (indexable as Sitecore.ContentSearch.SitecoreIndexableItem); if ((item != null)) { return item.Appearance.Sortorder; } else { return Sitecore.Configuration.Settings.DefaultSortOrder; } }
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/"> <sitecore role:require="Standalone or ContentManagement or ContentDelivery" search:require="solr"> <contentSearch> <indexConfigurations> <defaultSolrIndexConfiguration> <documentOptions> <fields hint="raw:AddComputedIndexField"> <field fieldName="sortOrder" returnType="int">[fully qualified class name],[custom assembly name]</field> </fields> </documentOptions> </defaultSolrIndexConfiguration> </indexConfigurations> </contentSearch> </sitecore> </configuration>
.OrderBy(item => item.Order).ThenBy(item => item.Name); なお、OrderはSearchResultItemから派生したカスタム クラスのプロパティです。 例: public class CustomSearchResultItem : Sitecore.ContentSearch.SearchTypes.SearchResultItem { [Sitecore.ContentSearch.IndexField("sortOrder")] public int Order { get; set; } }