Created
November 23, 2017 12:36
-
-
Save hishaamn/552504d153f756c9ef15da8fc5b64b59 to your computer and use it in GitHub Desktop.
Custom Sitecore Index Crawler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CustomCrawler : SitecoreItemCrawler | |
| { | |
| protected override bool IsExcludedFromIndex(SitecoreIndexableItem indexable, bool checkLocation = false) | |
| { | |
| IDocumentBuilderOptions documentOptions = this.DocumentOptions; | |
| Assert.IsNotNull(documentOptions, "DocumentOptions"); | |
| Item item = indexable.Item; | |
| Assert.ArgumentNotNull(item, "item"); | |
| if (item == null) | |
| { | |
| return base.IsExcludedFromIndex(indexable, checkLocation); | |
| } | |
| bool isExclude = this.IsExcluded(item, this.Language); | |
| if (isExclude) | |
| { | |
| this.Index.Locator.GetInstance<IEvent>().RaiseEvent("indexing:excludedfromindex", this.index.Name, item.Uri); | |
| return true; | |
| } | |
| if (documentOptions.HasIncludedTemplates) | |
| { | |
| if (documentOptions.HasExcludedTemplates) | |
| { | |
| CrawlingLog.Log.Warn("You have specified both IncludeTemplates and ExcludeTemplates. This logic is not supported. Exclude templates will be ignored."); | |
| } | |
| var baseTemplates = item.Template.BaseTemplates; | |
| if (baseTemplates.Any(baseTemplate => documentOptions.IncludedTemplates.Contains(baseTemplate.ID.ToString()))) | |
| { | |
| return false; | |
| } | |
| this.Index.Locator.GetInstance<IEvent>().RaiseEvent("indexing:excludedfromindex", this.index.Name, item.Uri); | |
| return true; | |
| } | |
| return base.IsExcludedFromIndex(indexable, checkLocation); | |
| } | |
| private bool IsExclude(Item item) | |
| { | |
| var field = item.Fields["Include In Sitecore Index"]; | |
| return field != null && field.Value != "1"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment