在 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. vue中,解决chrome下,的warning, Added non-passive event listener to a scroll-blocking ‘mousewheel‘ event 问题

    写项目的时候,Chrome 提醒: [Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' eve ...

  2. Software--电商平台--Module 5 Order & Payment

    2018-01-10  14:11:30 电商平台 订购和支付 模块 一: 整体示意图 二:构建一个框架来处理 领域模型内部发生的事情--领域事件 IDomainEvent 标识模型中的 Domain ...

  3. ES6 - 参数默认值

    1.形参初始值, 具有默认值的参数,一般位置要靠后(潜规则) function add(a,b,c=10){ return a + b + c; } let resutl = add(1,2); // ...

  4. 半成品 java 身份证校验

    public static Boolean is18Card(String idCard18) { //证件省份 HashMap<String, String> aCity = new H ...

  5. HTML完整语法学习

    https://www.cnblogs.com/douluo/archive/2021/11/20/15582217.html

  6. 2.21(html)

  7. verilog 硬件描述语言

    第一章 绪论 verilog--数字电路设计技术--ASIC/SOC芯片设计--协议pcie SATA USB--系统知识(个人计算机,芯片组,网络连接,嵌入式系统,硬件和软件的互操作) 第二章 寄存 ...

  8. C++ 复习函数的基本知识

    C++ 复习函数的基本知识 要使用 C++ 函数,必须完成如下工作: 1. 提供函数定义: 2. 提供函数原型: 3. 调用函数. 例子: #include <iostream> usin ...

  9. prometheus 配置数据保留7天时间storage.tsdb.retention.time

    1.修改配置如下: 默认24h添加配置:retention: 168h

  10. springboot配置类@ConfigurationProperties报错Not registered via @EnableConfigurationProperties or marked as Spring component

    添加一个@Component可以解决此问题,只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能.