在 Sitecore 里使用 Solr 搜索 SortOrder 关联的 Item

打印 上一主题 下一主题

主题 531|帖子 531|积分 1593

在 C# 使用 Solr 搜索

sitecore 的配置信息文件可直接丢进 \App_Config 下,sitecore 会自动检测配置文件更新并加载到内存中。
通常情况下,配置信息文件是放在 \App_Config\Include\ 下, 为你项目名。
通过配置启用 SortOrder 字段并获取 SortOrder

sitecore 默认是移除了 SortOrder 字段的,不过可通过打个补丁修改配置信息,如下配置 xml 启用 SortOrder 字段。
但是这种启用 SortOrder 字段有个不好的地方,当字段值为空时,在 Solr 里是找不到此字段的,且值类型为 string 类型。
EnableSortOrder_Patch.config
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  3.   <system.web>
  4.   </system.web>
  5.   <sitecore>
  6.       <contentSearch>
  7.           <indexConfigurations>
  8.               <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
  9.                   <documentOptions type="Sitecore.ContentSearch.SolrProvider.SolrDocumentBuilderOptions, Sitecore.ContentSearch.SolrProvider">
  10.                       <exclude hint="list:AddExcludedField">
  11.                           <__SortOrder>
  12.                               <patch:delete />
  13.                           </__SortOrder>
  14.                       </exclude>
  15.                   </documentOptions>
  16.               </defaultSolrIndexConfiguration>
  17.           </indexConfigurations>
  18.       </contentSearch>
  19.   </sitecore>
  20. </configuration>
复制代码
C# Code
  1. // ./SearchResultModel.cs
  2. using Sitecore.ContentSearch;
  3. public class SearchResultModel
  4. {
  5.     [IndexField(BuiltinFields.Name)]
  6.     public virtual string ItemName { get; set; }
  7.     // 注意此处需要填 SortOrder 的 Item name, 而不是 Title(通常在 sitecore 里直接看到就是 Title) 或者 Display Name
  8.     // 可通过它的 ID 找出证实一下 {BA3F86A2-4A1C-4D78-B63D-91C2779C1B5E}
  9.     // 或通过路径:/sitecore/templates/System/Templates/Sections/Appearance/Appearance/__Sortorder
  10.     [IndexField("__Sortorder")]
  11.     public virtual int SortOrder { get; set; }
  12.     [IndexField(BuiltinFields.LatestVersion)]
  13.     [ScriptIgnore]
  14.     public virtual bool IsLatestVersion { get; set; }
  15. }
  16. // ----------------------------------------------
  17. // ./Sample.cs
  18. using Sitecore.ContentSearch;
  19. using Sitecore.Globalization;
  20. using Sitecore.ContentSearch.Linq.Utilities;
  21.    
  22. var indexName = "sitecore_web_index";
  23. var language = Sitecore.Globalization.Language.Parse("en");
  24. using (IProviderSearchContext context = ContentSearchManager.GetIndex(indexName)
  25. {
  26.     var predicate = PredicateBuilder.True<SearchResultModel>();
  27.     if (!Sitecore.Context.PageMode.IsNormal)
  28.         predicate = predicate.And(z => z.IsLatestVersion);
  29.     predicate = predicate.And(z => z.Language.Equals(language.Name, StringComparison.OrdinalIgnoreCase));
  30.     var query = context.GetQueryable<SearchResultModel>()
  31.         .Filter(predicate);
  32.     // sitecore 排序的规则为:先按 SortOrder 升序排序,再按 Item name 升序排序
  33.     query = query
  34.         .OrderBy(z => z.SortOrder)
  35.         .ThenBy(z => z.ItemName);
  36.     return query.Select(x => x.Item)?.GetResults().Hits.Select(z => z.Document);
  37. }
复制代码
*通过使用通过使用 IComputedIndexField 的获取 SortOrder 的方法

此方法与前面不同的地方在于,当字段值为空时,在 Solr 里仍然可以搜索到此字段,且值为 100,同时值类型为 int 类型。推荐使用这种方式。
AddSortOrderField_Patch.config
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  3.   <system.web>
  4.   </system.web>
  5.   <sitecore>
  6.     <contentSearch>
  7.       <indexConfigurations>
  8.         <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
  9.           <documentOptions>
  10.             <fields hint="raw:AddComputedIndexField">
  11.               <field fieldName="SortOrder" returnType="int">LinkReit.Feature.Content.ChannelCard.ComputedFields.SortOrderField, LinkReit.Feature.Content.ChannelCard</field>
  12.             </fields>
  13.           </documentOptions>
  14.         </defaultSolrIndexConfiguration>
  15.       </indexConfigurations>
  16.     </contentSearch>
  17.   </sitecore>
  18. </configuration>
复制代码
C# Code
  1. // ./SortOrderField.cs
  2. using Sitecore.Data.Items;
  3. using Sitecore.ContentSearch;
  4. using Sitecore.ContentSearch.ComputedFields;
  5. public class SortOrderField : IComputedIndexField
  6. {
  7.     public object ComputeFieldValue(IIndexable indexable)
  8.     {
  9.         var item = (Item)(indexable as SitecoreIndexableItem);
  10.         if (item == null) return null;
  11.         
  12.         return item.Appearance.Sortorder;
  13.     }
  14.     public string FieldName { get; set; }
  15.     public string ReturnType { get; set; }
  16. }
  17. // ----------------------------------------------
  18. // ./SearchResultModel.cs
  19. using Sitecore.ContentSearch;
  20. public class SearchResultModel
  21. {
  22.     [IndexField(BuiltinFields.Name)]
  23.     public virtual string ItemName { get; set; }
  24.     // 此处 IndexFieldAttribute 构造参数需要填写的是你配置的 SortOrder 的 fieldName
  25.     [IndexField("SortOrder")]
  26.     public virtual int SortOrder { get; set; }
  27.     [IndexField(BuiltinFields.LatestVersion)]
  28.     [ScriptIgnore]
  29.     public virtual bool IsLatestVersion { get; set; }
  30. }
  31. // ----------------------------------------------
  32. // ./Sample.cs
  33. using Sitecore.ContentSearch;
  34. using Sitecore.Globalization;
  35. using Sitecore.ContentSearch.Linq.Utilities;
  36.    
  37. var indexName = "sitecore_web_index";
  38. var language = Sitecore.Globalization.Language.Parse("en");
  39. using (IProviderSearchContext context = ContentSearchManager.GetIndex(indexName)
  40. {
  41.     var predicate = PredicateBuilder.True<SearchResultModel>();
  42.     if (!Sitecore.Context.PageMode.IsNormal)
  43.         predicate = predicate.And(z => z.IsLatestVersion);
  44.     predicate = predicate.And(z => z.Language.Equals(language.Name, StringComparison.OrdinalIgnoreCase));
  45.     var query = context.GetQueryable<SearchResultModel>()
  46.         .Filter(predicate);
  47.     // sitecore 排序的规则为:先按 SortOrder 升序排序,再按 Item name 升序排序
  48.     query = query
  49.         .OrderBy(z => z.SortOrder)
  50.         .ThenBy(z => z.ItemName);
  51.     return query.Select(x => x.Item)?.GetResults().Hits.Select(z => z.Document);
  52. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

水军大提督

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表