Skip to content

Instantly share code, notes, and snippets.

@RyanABailey
Last active March 14, 2016 23:38
Show Gist options
  • Save RyanABailey/4f80f44f99af90f53330 to your computer and use it in GitHub Desktop.
Save RyanABailey/4f80f44f99af90f53330 to your computer and use it in GitHub Desktop.
Lucene index category
namespace MyProject.ComputedIndexFields
{
/// <summary>
/// Index the main level section the item falls under
/// </summary>
public class IndexCategory : IComputedIndexField
{
/// <inheritdoc />
public string FieldName { get; set; }
/// <inheritdoc />
public string ReturnType { get; set; }
/// <inheritdoc />
public object ComputeFieldValue(IIndexable indexable)
{
Item item = indexable as SitecoreIndexableItem;
if (item != null && !item.Paths.IsMediaItem)
{
try
{
if (item.ParentID == SearchConstants.rootItem)
{
if (item.TemplateID == SearchConstants.landingTemplate)
{
// landing page item - return itself
return item.DisplayName;
}
else
{
// Root level item which is not landing page
return null;
}
}
return RecursiveParent(item);
}
catch (Exception)
{
return null;
}
}
return null; // Return null if nothing to index
}
private static string RecursiveParent(Item currentItem)
{
if (currentItem.ParentID == SearchConstants.sitecoreItem)
{
// went too far
return null;
}
if (currentItem.ParentID == SearchConstants.rootItem)
{
// parent item is the root, return current item
return currentItem.DisplayName;
}
return RecursiveParent(currentItem.Parent);
}
}
}
public static class SearchConstants
{
public static ID rootItem = new ID("{A7CDAD31-BE99-4E67-A86C-2D1792F5564A}"); // Home item
public static ID sitecoreItem = new ID("{11111111-1111-1111-1111-111111111111}"); // Top level item in tree (Sitecore)
public static ID landingTemplate = new ID("{A81FB275-022F-430C-8C02-A4B526F8981A}"); // landing page template ID
}
<fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
<fieldNames hint="raw:AddFieldByFieldName">
!-- Category (root parent item) for faceting-->
<field fieldName="categoryfacet" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
<Analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
</field>
</fieldNames>
</fieldMap>
<fields hint="raw:AddComputedIndexField">
<!-- Category (root parent item) for faceting -->
<field fieldName="categoryfacet" storageType="yes" indexType="untokenized"
patch:after="field[last()]">WCC.Internet.UI.ComputedIndexFields.IndexCategory, WCC.Internet.UI</field>
</fields>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment