写在前面:
转载请注明出处
- Sitecore的搜索和索引总结?
Sitecore一共有三个搜索构架, 分别是Content Search, xConnectSearch和Commerce Search. Content Search顾名思义是用来搜索Sitecore内容的,一般公共或匿名用户会使用到这个搜索。xConnectSearch是用来搜索用户数据的譬如用户访问时使用的设备或访问地点等, 一般营销人员会使用这个搜索来策划更精确的营销方案。Commerce Search是用来搜索销售数据譬如用户下单了那些物品, 订单状态等。以下表格来源于Sitecore官方文档。
Content Search | xConnect Search | Commerce Search | |
---|---|---|---|
APIs | Sitecore Content Search API | xConnect Client API | Commerce Search Service |
Providers | Solr Azure Search | Solr Azure Search | Solr Azure Search |
Indexes | sitecore_core_index sitecore_master_index sitecore_web_index sitecore_marketingdefinitions_master sitecore_marketingdefinitions_web sitecore_marketing_asset_index_master sitecore_marketing_asset_index_web sitecore_testing_index sitecore_suggested_test_index sitecore_fxm_master_index sitecore_fxm_web_index | xdb xdb_rebuild | Customer Scope index Order Scope index Catalog Items Scope index |
Indexer | indexCrawler (run on the core Sitecore application) | xConnect Search Indexer (run as Windows Service or Web Job) | Commerce Minions |
- 为什么需要建立一个自定义的搜索索引 (search index)?
- 只加入自己想加入的Item类型,从而更快的返回结果或更快的整理索引(re-indexing)
- 可以加入自己想要的属性(field)
- 更好的控制何时更新索引
- 什么是Computed Index Fields? 为什么我们需要Computed Index Fields?
Computed Index Fields可以看做是一个索引 (index) 中每一个每一个item都具有的一个属性的值, 譬如被index收集的Items所在的根目录的ID, 或者Items的所有可用的语言版本(version)等。我们需要Computed Index Fields是因为通过这样的方式,我们可以直接在Index中加入一些我们想调用的属性, 这样就不需要搜索到该Item后再去访问数据库调取该Item的一些属性值了。
正文目录:
- 创建一个Lucene为引擎的Master数据库的收集Article页面类型的索引, 并创建自己的页面Crawler。强调一下, 如果希望创建Solr的Index, 需要不同方法,此处创建的是Lucene作为引擎的并且只收集Master数据库的搜索索引。
- 为我们创建的搜索索引添加一个Computed Index Field来记录此Item的GUID形式的Site ID
正文:
- Sitecore安装时自带一个自定义的Article模板, 我们将以其为此次新建的Index的唯一收集的模板类型。我们将先在Visual Studio项目中加入一个文件夹专门用来存放自定义的搜索索引。
1). 首先在项目中, “/App_Config/Include/” 目录下创建新建文件夹取名为"zzz.CustomIndexes", 在此文件夹下创建一个Config文件并取名为"SR.ContentSearch.Lucene.ArticleIndex.Master.config".
2). 在此Config文件中加入以下代码:<?xml version="1.0" encoding="utf-8" ?> <configuration xmls:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <contentSearch> <configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch"> <indexes hint="list:AddIndex"> <!--此处为在Sitecore CMS中的index的名称--> <index id="sr_article_master_index" type="Sitecore.ContentSearch.LuceneProvider.LuceneIndex, Sitecore.ContentSearch.LuceneProvider"> <param desc="name">$(id)</param> <param desc="core">$(id)</param> <param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" /> <configuration ref="contentSearch/indexConfigurations/defaultLuceneIndexConfiguration"> <fields hint="raw:AddComputedIndexField"> <!--此处为新创建的自定义的Computed Index Field, 我们将在第2步添加此Field--> <field fieldName="siteguid" storageType="YES" indexType="TOKENIZED">Sitecore8Review.Webforms.CustomComputedFields.SiteGUID, Sitecore8Review.Webforms</field> </fields> <include hint="list:IncludeTemplate"> <!--此处为希望加入的模板(tempalte)的ID, 此处用的是Sitecore安装时自带的Article模板的ID--> <Article>{0B3BB88D-A3E5-42EA-9F4F-5FA20A3AD818}</Article> </include> </configuration> <strategies hint="list:AddStrategy"> <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndAsync" /> </strategies> <locations hint="list:AddCrawler"> <!--此处为自定义的Crawler名称和Assemly名称, 我们将在下一步添加自定义的Crawler--> <content type="Sitecore8Review.Webforms.CustomCrawlers.PageCrawlers, Sitecore8Review.Webforms"> <!--收集Items的数据库, 此处为master数据库--> <Database>master</Database> <!--此处为Crawler开始收集的根目录的路径--> <Root>/sitecore/Content/SitecoreReview</Root> </content> </locations> <enableItemLanguageFallback>false</enableItemLanguageFallback> <enableFieldLanguageFallback>false</enableFieldLanguageFallback> </index> </indexes> </configuration> </contentSearch> </sitecore> </configuration>
3). 在商业逻辑层的项目中添加一个新的文件夹取名为"CustomCrawlers", 在此文件夹中添加一个新的类文件, 取名为"PageCrawlers.cs", 在此类文件中加入如下代码:
4). 保存并发布此项目到Sitecore目录下, 查看Sitecore CMS的Control Panel下的indexing manager,确认此Index已经被加入Index的列表中:
2. 为新创建的搜索索引添加一个自定义的Computed Index Field
1). 在项目的商业逻辑层添加一个新的文件夹, 取名为"CustomComputedFields", 在此文件夹中添加一个新的类文件, 取名为"SiteGUID.cs". 在此类文件中加入如下代码
2). 保存并发布这个项目到Sitecore网站目录下, 访问Sitecore CMS中的indexing manager, 重新创建"sr_article_master_index"确保没有任何错误:
恭喜完成了创建一个自定义的搜索索引(search index) 并为其添加一个自定义的Computed Index Field的任务!可以开始使用这个自定义的搜索索引为网站加速!