在 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. 记一次SpringBoot整合Redis的Bug

    SpringBoot整合Redis遇见的坑 <!--Redis配置开始--> <dependency> <groupId>org.springframework.b ...

  2. 2022-04-14内部群每日三题-清辉PMP

    1.项目经理资源有限,无法获得更多资源.项目经理应该使用什么技术来充分利用现有资源,而不会令项目完成时间延期? A.资源平滑 B.资源平衡 C.快速跟进 D.赶工 2.正在审查问题日志的项目经理注意到 ...

  3. C++ 读取文本, 读取( 单字符/ 一行/ 全部 )

    C++ 读取文本 介绍三种读取方式: 逐字符读取(注意不是字节) 读取一行 读取全部 示例代码: #include <iostream> #include <string> # ...

  4. 047_SOQL 基本查询总结

    User currentUser = [SELECT Id, Profile.Name,UserRole.Name FROM User WHERE Id = :UserInfo.getUserId() ...

  5. 攻防世界Web篇——PHP2

    可以从index.phps中找到网站源码 从源码中得出,要满足admin!=$_GET[id],urldecode($_GET[id]) == admin,两个条件才能得到flag 所以就将admin ...

  6. CSS尺寸设置的单位:px、rem、em、vw、vh

    px:pixel像素的缩写,绝对长度单位,它的大小取决于屏幕的分辨率,是开发网页中常常使用的单位. em:相对长度单位,在 `font-size` 中使用是相对于父元素的字体大小,在其他属性中使用是相 ...

  7. 一次讲清promise

    此文章主要讲解核心思想和基本用法,想要了解更多细节全面的使用方式,请阅读官方API 这篇文章假定你具备最基本的异步编程知识,例如知道什么是回调,知道什么是链式调用,同时具备最基本的单词量,例如page ...

  8. Linux软件防火墙iptables

    Netfilter组件 内核空间,集成在linux内核中 官网文档:https://netfilter.org/documentation/ 扩展各种网络服务的结构化底层框架 内核中选取五个位置放了五 ...

  9. pads:数据格式不正确,网络必须包含一个以上管脚

    1,如果已经有pcb封装,则在pads logic软件里面-元件编辑器-重新做封装,在--编辑电参数--里面匹配对应pcb封装, 2,点击-工具--,--从库中更新--,更新一下,之后导入pcb la ...

  10. Coder vs Programmer: Difference You Should Know

    In this tech-driven world, you may have heard the terms 'coder' and 'programmer' used interchangeabl ...