【问题标题】:Sitecore Lucene Exclude Item From IndexSitecore Lucene 从索引中排除项目
【发布时间】:2016-07-12 21:04:38
【问题描述】:

我正在尝试允许内容编辑器选择从搜索页面中排除项目。正在搜索的模板上有一个复选框,指示它是否应该显示。我已经看到一些答案涉及从 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)。

【问题讨论】:

  • 内容编辑器优化搜索结果而不是让它们从索引中排除项目会更好吗?

标签: lucene sitecore sitecore8


【解决方案1】:

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

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

【讨论】:

  • 我同意 @IanGraham cmets 关于使用过滤器从搜索结果中排除,而不是直接从索引中排除,除非您有特定原因。
  • 唯一真正的原因是,每次我们点击索引时,我们都会使用一个额外的参数进行过滤,而不是增加一些(尽管微不足道)开销。不过,这肯定看起来会更容易。不过,在走那条路线之前,我想试试你的建议。
  • 您可以查看contentSearch.getGlobalLinqFilters 管道,但这可能适用于所有索引所有时代。此外,请查看indexing.filterIndex.inbound 管道以从索引中排除项目,这可能再次适用于所有索引。但是您可能不希望对核心/主/网络索引使用此功能,因为 Sitecore 在内部使用这些索引在 CMS 中进行自己的搜索。如果你顺便提供解决方案,你应该创建一个使用这个爬虫的自定义索引。
  • 您的解决方案似乎有效,但是当我尝试编辑我添加的复选框时,我看到一个错误,指出我的自定义类缺少方法 IncludeTemplates。它确实保留了我想要排除在索引之外的项目,所以它确实按照我的要求进行。我将退回到仅索引复选框值并基于此进行过滤。
  • 您使用的是哪个版本的 Sitecore?我在 SC8.0.6 和 8.1.3 中使用上述方法没有问题...我查看了 ContentSeach API,找不到任何提及或称为 IncludeTemplates 的方法。
猜你喜欢
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
相关资源
最近更新 更多