Tag Archives: Search

EPiServer 7/Search: Prevent content from being indexed

Let’s say you have a page or a page type that you do not want to get indexed by the EPiServer Search service. The solution is to implement the ISearchable interface:

bool AllowReIndexChildren { get; }
bool IsSearchable { get; }

It requires you to implement two boolean properties, AllowReIndexChildren and IsSearchable. By letting IsSearchable return false you’ll prevent the page from being indexed. You could either implement it as a page property, having the editor choose which pages should be excluded:

[Display(Name = "Exclude page from site search",
    Description = "Check this box to prevent page from being indexed by the site's search engine.",
    GroupName = SystemTabNames.Settings,
    Order = 100)]
public virtual bool IsSearchable
{
    get
    {
        bool isSearchable = this.GetPropertyValue(p => p.IsSearchable);
        return !isSearchable;
    }
    set
    {
        this.SetPropertyValue(p => p.IsSearchable, value);
    }
}

Or you could prevent the the entire page type from being indexed:

[ContentType(DisplayName = "Sneaky page that won't get indexed.", GUID = "00000000-0000-0000-0000-000000000000")]
public class SneakyPage : PageData, ISearchable
{
    public bool AllowReIndexChildren { get { return true; } }
    public bool IsSearchable { get { return false; } }
}

But what about content that is already in the index? Ticking the box to prevent indexing on a page that has previouly been index will only prevent updates to this item, not prevent it from showing in search results.

In order to make sure indexed items are removed add the code below to Global.asax

protected void Application_Start()
{
    DataFactory.Instance.PublishedContent += OnPublishedContent;
}

private void OnPublishedContent(object sender, ContentEventArgs contentEventArgs)
{
    ISearchable seachable = contentEventArgs.Content as ISearchable;
    if (seachable == null) {
        return;
    }

    if (seachable.IsSearchable) {
        return;
    }

    CultureInfo language = null;
    ILocalizable localizable = contentEventArgs.Content as ILocalizable;

    if (localizable != null) {
        language = localizable.Language;
    }

    string searchId = string.Concat(contentEventArgs.Content.ContentGuid, "|", language);
    IndexRequestItem indexItem = new IndexRequestItem(searchId, IndexAction.Remove);
    SearchHandler.Instance.UpdateIndex(indexItem);
}