Sitecore Lucene 从索引中排除项目

Sitecore Lucene Exclude Item From Index

我试图让内容编辑者可以选择从搜索页面中排除项目。正在搜索的模板上有一个复选框,指示它是否应该显示。我看到了一些涉及从 Sitecore.Search.Crawlers.DatabaseCrawler 继承并覆盖 AddItem 方法 (Excluding items selectively from Sitecore's Lucene search index - works when rebuilding with IndexViewer, but not when using Sitecore's built-in tools) 的答案。不过,从控制面板重建索引时,这似乎并没有受到影响。我已经能够在 Sitecore.ContentSearch.SitecoreItemCrawler 中找到一个名为 RebuildFromRoot 的方法。有谁确切知道该问题中的 DatabaseCrawler 方法何时被命中?我有一种感觉,我需要同时使用自定义 SitecoreItemCrawler 和 DatabaseCrawler,但我并不肯定。任何见解将不胜感激。我正在使用 Sitecore 8.0(修订版 150621)。

继承 Sitecore 中默认的 Lucene 爬虫实现并覆盖 IsExcludedFromIndex 方法,返回 true 以排除该项目被编入索引:

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 方法从索引中删除项目,如果某个项目在未来某个日期更新。

使用补丁文件替换您需要的任何索引的爬虫。

<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>

之后您将不得不重建索引(从控制面板就可以),以便排除这些项目。