Friday, 4 March 2016

Excluding Standard Values items from search results

Last time I was configuring my search index and I found that there are Standard Values items in search results. Quick research in google showed me that I can hide this items by creating computed field, which will mark these items.

Few moments later, after another glimpse at the index configuration, I found my issue. In crawler configuration I forgot to define root of the search:

<locations hint="list:AddCrawler">
  <crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
    <Database>web</Database>
    <Root>/sitecore/</Root>
  </crawler>
</locations>

I want to search only under the Home item, so I should define it like this:

<Root>/sitecore/content/home</Root>

:)

Well, it was easy one. But then I though how could I exclude Standard Values items from index, without setting root. Solution with creating computed field and then checking it while fetching results, sounds too tricky for me. So I created custom crawler which checks if item is standard values item:

/// <summary>
/// Custom Item Crawler
/// </summary>
public class CustomItemCrawler : SitecoreItemCrawler
{
    protected override void DoAdd(IProviderUpdateContext context, SitecoreIndexableItem indexable)
    {
        var item = indexable.Item;
 
        // - Don't add Standard values Item -
 
        if (!StandardValuesManager.IsStandardValuesHolder(item))
        {
            base.DoAdd(context, indexable);
        }
    }
}

And configuration:

<locations hint="list:AddCrawler">
  <crawler type="MyAssembly.Crawlers.CustomItemCrawler, MyAssembly">
    <Database>web</Database>
    <Root>/sitecore/</Root>
  </crawler>
</locations>

Share it:

Radosław Kozłowski - Senior Sitecore Developer Radoslaw Kozlowski

Author & Editor

Sitecore MVP, passionate Sitecore & .NET developer and architect. Sitecore Community evangelist.

0 comments:

Post a Comment