Saturday, 6 May 2017

Sitecore Search Faceting & Date range faceting

This blog post is divided in following 2 parts:

Part 1: Search faceting and Date Range search facet available out of the box.
Part 2: Date range faceting based on custom DateTime Field.

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 faceting.

We can see these OOB facets under:
Location: /sitecore/system/Settings/Buckets/Facets

You can read about them here.

This post will discuss about one of the above facet i.e. “Date range facet “.
We will see 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 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 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

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

Part 2: 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”)

Then, add a custom class derived from the “BaseDateRangeFieldProcessor”.  

I 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 me.

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.
Comments and feedback would be appreciated.

No comments:

Post a Comment