Sitecore Search Faceting & Date Range Faceting

Sitecore Hints & Tips

Search faceting and Date Range search facet available out of the box

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

Sitecore OOTB 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.

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:

  • Sitecore fields: All standard Sitecore fields.
  • Computed fields: A field whose index value is calculated while indexing. They are stored in the index.
  • Virtual fields: A field in the search index that doesn't exist in the physical index, but is computed at search run-time.

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

Virtual fields

The daterange field defined here is calculated based on the field "_smallcreateddate":

On decompiling:

Daterange field

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":

Daterange field
Daterange field

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":

  1. today
  2. yesterday
  3. thisweek
  4. lastweek
  5. thismonth
  6. lastmonth
  7. thisyear
  8. older

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:

Daterange field

Adding own index field to be used for computing date range facets

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"):

Daterange field

Then, add a custom class derived from the "BaseDateRangeFieldProcessor":

Daterange field

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.

Daterange field

Now, the date range faceting can also be done based on the above created DateTime field:

Daterange 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!

— Chaitanya Marwah - May 2017

Want more?

Back to the Blog