问题描述
我正在尝试让内容编辑器可以选择从搜索页面中排除项目.正在搜索的模板上有一个复选框,指示是否应显示该模板.我已经看到一些答案,涉及从Sitecore.Search.Crawlers.DatabaseCrawler继承并覆盖AddItem方法().但是,从控制面板重建索引时似乎没有受到影响.我已经能够在Sitecore.ContentSearch.SitecoreItemCrawler中找到一个名为RebuildFromRoot的方法.有人知道该问题的DatabaseCrawler方法何时被命中吗?我觉得我需要同时使用自定义SitecoreItemCrawler和DatabaseCrawler,但我并不满意.任何见识将不胜感激.我正在使用Sitecore 8.0(修订版150621).
I'm trying to allow a content editor to have the option to exclude items from a search page. There is a checkbox on the template being searched which indicates whether or not it should show up or not. I've seen a few answer that involve inheriting from Sitecore.Search.Crawlers.DatabaseCrawler and overriding the AddItem method (Excluding items selectively from Sitecore's Lucene search index - works when rebuilding with IndexViewer, but not when using Sitecore's built-in tools). This does not seem to be hit when rebuilding indexes from the control panel though. I have been able to hit a method in Sitecore.ContentSearch.SitecoreItemCrawler called RebuildFromRoot. Does anyone know exactly when the DatabaseCrawler method from that question is hit? I have a feeling I'll need to use both a custom SitecoreItemCrawler and DatabaseCrawler but I'm not positive. Any insight would be appreciated. I am using Sitecore 8.0 (rev. 150621).
推荐答案
继承自Sitecore中默认的Lucene爬网程序实现,并覆盖IsExcludedFromIndex
方法,返回true以排除该项目被索引:
Inherit from the default Lucene crawler implementation in Sitecore and override the IsExcludedFromIndex
method, returning true to exclude the item from being indexed:
using Sitecore.ContentSearch;
using Sitecore.Data.Items;
namespace MyProject.CMS.Custom.ContentSearch.Crawlers
{
public class CustomItemCrawler : Sitecore.ContentSearch.SitecoreItemCrawler
{
protected override bool IsExcludedFromIndex(SitecoreIndexableItem indexable, bool checkLocation = false)
{
bool isExcluded = base.IsExcludedFromIndex(indexable, checkLocation);
if (isExcluded)
return true;
Item obj = (Item)indexable;
if (obj["Exclude From Index"] != "1") //or whatever logic you need
return true;
return false;
}
protected override bool IndexUpdateNeedDelete(SitecoreIndexableItem indexable)
{
if (base.IndexUpdateNeedDelete(indexable))
{
return true;
}
Item obj = indexable;
return obj["Exclude From Index"] == "1";
}
}
}
如果某个项目在将来的某个日期更新,则需要IndexUpdateNeedDelete
方法从索引中删除项目.
The IndexUpdateNeedDelete
method is required to remove items from the index if an item in updated at a future date.
使用补丁文件替换需要为其建立索引的搜寻器.
Use a patch file to replace the crawler for which ever indexes you need.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<configuration>
<indexes>
<index id="sitecore_master_index">
<locations>
<crawler>
<patch:attribute name="type">MyProject.CMS.Custom.ContentSearch.Crawlers.CustomItemCrawler, MyProject.CMS.Custom</patch:attribute>
</crawler>
</locations>
</index>
...
</indexes>
</configuration>
</contentSearch>
</sitecore>
</configuration>
之后,您将必须重建索引(可以从控制面板中重新构建),以便排除这些项.
You will have to rebuild the indexes afterwards (from the control panel is fine) so that the items are excluded.
这篇关于Sitecore Lucene从索引中排除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!