在 C# 使用 Solr 搜索

sitecore 的配置信息文件可直接丢进 <Instance>\App_Config 下,sitecore 会自动检测配置文件更新并加载到内存中。

通常情况下,配置信息文件是放在 <Instance>\App_Config\Include\<Project> 下,<Project> 为你项目名。


通过配置启用 SortOrder 字段并获取 SortOrder

sitecore 默认是移除了 SortOrder 字段的,不过可通过打个补丁修改配置信息,如下配置 xml 启用 SortOrder 字段。

但是这种启用 SortOrder 字段有个不好的地方,当字段值为空时,在 Solr 里是找不到此字段的,且值类型为 string 类型。

EnableSortOrder_Patch.config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<system.web>
</system.web>
<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>
C# Code
// ./SearchResultModel.cs
using Sitecore.ContentSearch; public class SearchResultModel
{
[IndexField(BuiltinFields.Name)]
public virtual string ItemName { get; set; } // 注意此处需要填 SortOrder 的 Item name, 而不是 Title(通常在 sitecore 里直接看到就是 Title) 或者 Display Name
// 可通过它的 ID 找出证实一下 {BA3F86A2-4A1C-4D78-B63D-91C2779C1B5E}
// 或通过路径:/sitecore/templates/System/Templates/Sections/Appearance/Appearance/__Sortorder
[IndexField("__Sortorder")]
public virtual int SortOrder { get; set; } [IndexField(BuiltinFields.LatestVersion)]
[ScriptIgnore]
public virtual bool IsLatestVersion { get; set; }
} // ---------------------------------------------- // ./Sample.cs
using Sitecore.ContentSearch;
using Sitecore.Globalization;
using Sitecore.ContentSearch.Linq.Utilities; var indexName = "sitecore_web_index";
var language = Sitecore.Globalization.Language.Parse("en");
using (IProviderSearchContext context = ContentSearchManager.GetIndex(indexName)
{
var predicate = PredicateBuilder.True<SearchResultModel>();
if (!Sitecore.Context.PageMode.IsNormal)
predicate = predicate.And(z => z.IsLatestVersion); predicate = predicate.And(z => z.Language.Equals(language.Name, StringComparison.OrdinalIgnoreCase)); var query = context.GetQueryable<SearchResultModel>()
.Filter(predicate); // sitecore 排序的规则为:先按 SortOrder 升序排序,再按 Item name 升序排序
query = query
.OrderBy(z => z.SortOrder)
.ThenBy(z => z.ItemName); return query.Select(x => x.Item)?.GetResults().Hits.Select(z => z.Document);
}

*通过使用通过使用 IComputedIndexField 的获取 SortOrder 的方法

此方法与前面不同的地方在于,当字段值为空时,在 Solr 里仍然可以搜索到此字段,且值为 100,同时值类型为 int 类型。推荐使用这种方式。

AddSortOrderField_Patch.config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<system.web>
</system.web>
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
<documentOptions>
<fields hint="raw:AddComputedIndexField">
<field fieldName="SortOrder" returnType="int">LinkReit.Feature.Content.ChannelCard.ComputedFields.SortOrderField, LinkReit.Feature.Content.ChannelCard</field>
</fields>
</documentOptions>
</defaultSolrIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
C# Code
// ./SortOrderField.cs

using Sitecore.Data.Items;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields; public class SortOrderField : IComputedIndexField
{
public object ComputeFieldValue(IIndexable indexable)
{
var item = (Item)(indexable as SitecoreIndexableItem);
if (item == null) return null; return item.Appearance.Sortorder;
} public string FieldName { get; set; } public string ReturnType { get; set; }
} // ---------------------------------------------- // ./SearchResultModel.cs
using Sitecore.ContentSearch; public class SearchResultModel
{
[IndexField(BuiltinFields.Name)]
public virtual string ItemName { get; set; } // 此处 IndexFieldAttribute 构造参数需要填写的是你配置的 SortOrder 的 fieldName
[IndexField("SortOrder")]
public virtual int SortOrder { get; set; } [IndexField(BuiltinFields.LatestVersion)]
[ScriptIgnore]
public virtual bool IsLatestVersion { get; set; }
} // ---------------------------------------------- // ./Sample.cs
using Sitecore.ContentSearch;
using Sitecore.Globalization;
using Sitecore.ContentSearch.Linq.Utilities; var indexName = "sitecore_web_index";
var language = Sitecore.Globalization.Language.Parse("en");
using (IProviderSearchContext context = ContentSearchManager.GetIndex(indexName)
{
var predicate = PredicateBuilder.True<SearchResultModel>();
if (!Sitecore.Context.PageMode.IsNormal)
predicate = predicate.And(z => z.IsLatestVersion); predicate = predicate.And(z => z.Language.Equals(language.Name, StringComparison.OrdinalIgnoreCase)); var query = context.GetQueryable<SearchResultModel>()
.Filter(predicate); // sitecore 排序的规则为:先按 SortOrder 升序排序,再按 Item name 升序排序
query = query
.OrderBy(z => z.SortOrder)
.ThenBy(z => z.ItemName); return query.Select(x => x.Item)?.GetResults().Hits.Select(z => z.Document);
}

在 Sitecore 里使用 Solr 搜索 SortOrder 关联的 Item的更多相关文章

  1. 关于Solr搜索标点与符号的中文分词你必须知道的(mmseg源码改造)

    关于Solr搜索标点与符号的中文分词你必须知道的(mmseg源码改造) 摘要:在中文搜索中的标点.符号往往也是有语义的,比如我们要搜索“C++”或是“C#”,我们不希望搜索出来的全是“C”吧?那样对程 ...

  2. 什么是Solr搜索

    什么是Solr搜索 一.Solr综述   什么是Solr搜索 我们经常会用到搜索功能,所以也比较熟悉,这里就简单的介绍一下搜索的原理. 当然只是介绍solr的原理,并不是搜索引擎的原理,那会更复杂. ...

  3. Solr搜索技术

    Solr搜索技术 今日大纲 回顾上一天的内容: 倒排索引 lucene和solr的关系 lucene api的使用 CRUD 文档.字段.目录对象(类).索引写入器类.索引写入器配置类.IK分词器 查 ...

  4. Solr系列五:solr搜索详解(solr搜索流程介绍、查询语法及解析器详解)

    一.solr搜索流程介绍 1. 前面我们已经学习过Lucene搜索的流程,让我们再来回顾一下 流程说明: 首先获取用户输入的查询串,使用查询解析器QueryParser解析查询串生成查询对象Query ...

  5. solr搜索应用

    非票商品搜索,为了不模糊查询影响数据库的性能,搭建了solr搜索应用,php从solr读取数据

  6. solr搜索结果转实体类对象的两种方法

    问题:就是把从solr搜索出来的结果转成我们想要的实体类对象,很常用的情景. 1.使用@Field注解 @Field这个注解放到实体类的属性[字段]中,例如下面 public class User{ ...

  7. spring data solr 搜索关键字高亮显示

    spring data solr 搜索关键字高亮显示 public Map<String, Object> highSearch(Map searchMap) { Map map = ne ...

  8. solr搜索分词优化

    solr服务器配置好在搜索时经常会搜出无关内容,把不该分的词给分了,导致客户找不到自己需要的内容,那么我们就从配置词典入手解决这个问题. 首先需要知道自带的词典含义: 停止词:停止词是无功能意义的词, ...

  9. solr搜索之搜索精度问题我已经尽力了!!!

    solr搞了好久了,没啥进展,没啥大的突破,但是我真的尽力了! solr7可能是把默认搜索方式去掉了,如下: 在solr7里找了半天以及各种查资料也没发现这个默认搜索方式,后来想,可能是被edisma ...

  10. 商城06——solr索引库搭建&solr搜索功能实现&图片显示问题解决

    1.   课程计划 1.搜索工程的搭建 2.linux下solr服务的搭建 3.Solrj使用测试 4.把数据库中的数据导入索引库 5.搜索功能的实现 2.   搜索工程搭建 要实现搜索功能,需要搭建 ...

随机推荐

  1. Easycode—MybatisPlus模板

    EasyCode使用指南 1.下载EasyCode插件 2.配置EasyCode 2.1.配置作者名称              2.2.配置代码内容生成模板(模板内容见文末)            ...

  2. yum install的时候提示:Loaded plugins: fastestmirror

    fastestmirror是yum的一个加速插件,这里是插件提示信息是插件不能用了. 不能用就先别用呗,禁用掉,先yum了再说. 1.修改插件的配置文件 # vi  /etc/yum/pluginco ...

  3. 058_Component Bundles

  4. git rebase之abort,continue,skip

    git rebase --abort 会放弃合并,回到rebase操作之前的状态,之前的提交的不会丢 git rebase --skip 会将引起冲突的commit丢弃掉 git rebase --c ...

  5. 关于css选择器的一点点记录

    <!-- 选择器:         #id..class.标签.>子代. 后代.+紧跟一个.~紧跟所有.:(效果)伪类 -->         <!-- 效果选择器常用属性: ...

  6. JS缓存三种方法_sessionStorage_localStorage_Cookie

    1.sessionStorage:临时的会话存储 只要当前的会话窗口未关闭,存储的信息就不会丢失,即便刷新了页面,或者在编辑器中更改了代码,存储的会话信息也不会丢失. 2.localStorage:永 ...

  7. vue项目中axios跨域设置

    最近项目中遇到一个问题,测试环境和线上环境需要调同一个接口(接口地址是线上的),本地开发的时候遇到了跨域的问题,刚开始用了fetch解决的,代码如下 方法一 step1:安装包node-fetch,然 ...

  8. 西瓜书6.2 matlab的libsvm使用

    因为python的教程没有找到详细的所以就改用matlab了 使用的是matlab r2016a,libsvm3-24,具体的安装配置教程就直接参考谦恭大大的了: https://blog.csdn. ...

  9. js 原生数据类型判断

    之前一直使用的jquery的数据类型判断,比如:isArray()等,今天看到了一个判断数据类型的简单的原生方法,分享给大家 Object.prototype.toString 方法返回对象的类型字符 ...

  10. opencv对鱼眼图像畸变矫正

    import numpy as np ''' #T_cam_imu body_T_cam0: !!opencv-matrix rows: 4 cols: 4 dt: d data: [0.003489 ...