I have a search result component and want to add a filter to it in order to filter the results by year or by month. i have a template that has a date field and i tried the Filter(date) component but it uses a facet that filters by a range of date, i want to filter only by either month or year Is there a way to do that?
1 Answer
No, it's not possible out of the box. We are not storing Months or Years in a separate field and that would be required to facet by one of those values.
To achieve that you would need to create a separate field in your item template which will store month or year or create a computed field which will extract month or year from the date field and store it as a month or year inside of the index.
The computed field can look like this:
public class ItemYear : AbstractComputedIndexField
{
public override object ComputeFieldValue(IIndexable indexable)
{
Item item = indexable as SitecoreIndexableItem;
DateTime date = EXTRACT DATE FROM THE ITEM;
return date.Year;
}
}
And here is the config part (it's for Lucene):
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultLuceneIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
<documentOptions type="Sitecore.ContentSearch.LuceneProvider.LuceneDocumentBuilderOptions, Sitecore.ContentSearch.LuceneProvider">
<fields hint="raw:AddComputedIndexField">
<field fieldName="year" returnType="int">NAMESPACE.ItemYear , ASSEMBLY_NAME</field>
</fields>
</documentOptions>
</defaultLuceneIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>