Search code examples
lucenesitecoresitecore8

Sitecore Lucene Exclude Item From Index


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


Solution

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