Sitecore Lucene Exclude Item From Index

送分小仙女□ 提交于 2020-01-24 13:58:00

问题


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


回答1:


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";
        }
    }
}

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.



来源:https://stackoverflow.com/questions/38338854/sitecore-lucene-exclude-item-from-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!