Search facets help us filter the search results and limit the search results after running the actual search. Sitecore Lucene and Solr both support faceting, Sitecore 8.2 and some other lower versions provide out of the box (OOTB) faceting.
We can see these OOTB facets under: Location: /sitecore/system/Settings/Buckets/Facets
You can read about them here.
This post will discuss one facet, the "Date range facet". We will outline below how the date range facet works and how we can leverage this date range facet in our project.
Sitecore date range facets are used by the Sitecore client in "Media Library", which can be leveraged in our website as well. Location: /Sitecore/Media Library.
We can see in the above image, how Sitecore has refined the results into various date ranges like: older, this month, this week etc. Also, apart from date range we can see other facets like language, template etc. available.
Sitecore Lucene & Solr search use indexes and we can have 3 different kind of fields that can be indexed in Sitecore:
Here, the faceting based on date ranges uses a field called "daterange". It is a Sitecore virtual field - since the value of this field may change by each passing second, we can't compute it and store it in index at the time of indexing. Example: If I added an image in the media library yesterday, then after 2 days the result should be 0 matches for yesterday and 1 match for this week, without the need to rebuild the index. This computation should be done on run time of search. Although, we will require a backing field based on which the virtual field value is generated.
The virtual fields are defined in following config:
File name: Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config
Location: (root)/Website/App_Config/Include
The daterange field defined here is calculated based on the field "_smallcreateddate":
On decompiling:
This "_smallcreateddate" is the Sitecore standard field "Created" found under Statistics.
We can see the indexed value stored on the solr admin page for created date is called as "_smallcreateddate":
This means that the faceting for date range is done based on the item creation date.
Now, after further drilling it was found that the following date range facets are available in "BaseDateRangeFieldProcessor":
Which can be used directly by leveraging Sitecore date range faceting. Your result can be faceted based on date range by just adding the below line of code:
As mentioned in part one, we have the date range virtual field configured in the file: Sitecore.ContentSearch.Solr.DefaultIndexConfiguration.config, we will now tell Sitecore to use our own date field and NOT the standard "_smallcreatteddate" field.
First, we need to add our own field in the configuration: (called it as "mydaterange"):
Then, add a custom class derived from the "BaseDateRangeFieldProcessor":
We have called it as "CustomDateRangeFieldProcessor" and as you can see, the FieldName is same as mentioned in the config and the date to be used is called "date_tdt" or we can call it as "date", which is just a DateTime field created by us.
Now, the date range faceting can also be done based on the above created DateTime field:
We can also create our own facets based on the requirement and add them under: /sitecore/system/Settings/Buckets/Facets
That's it for this blog post. We hope you enjoyed our hints and tips!
Subscribe for the latest news and events from our talented team and community network.