Skip to main content

Filter search results by date range in Sitecore SXA Search

Hey blog viewers, Welcome to Learn Sitecore. 

Sitecore SXA OOTB Search module contains many OOTB features such as search results, sort results and different filter types etc... To render the search results in a page, Search results rendering from OOTB will help. It contains the following details for the component display.

Data Source: The data source template contains a single line text field. The content given in this field will be used to render when the search results are zero for specified scope.

Rendering Parameter fields: In the rendering parameter fields we need to choose the following for the search component to work. 

Search Signature: This field will help to map the search results to the correct rendering. If we add only one search results rendering to a page, this field can left blank but when more than one rendering of type search results added to the same page, this field shouldn't be blank. If the field left blank for both renderings, then which ever loads first from search results API, those results will be shown in the page for both renderings.

Scope: Here we will specify the query to filter the results. This item can be created in scopes node under site settings.

Page Size: This is used to specify the page size of the results. The same will be used in combination to the load more component that comes OOTB search feature.

Default language filtering: Select the language of search results to be shown in the page

Sort results: The configuration selected here is used to sort the results only when the sort results component is not added to the page

Auto fire search when no criteria is set: If it is unselected the search will not be triggered until the user triggers it.

We have seen about the basic details of search results component. Let's dive in to the filtering section of results where the scope is defined. Generally there are many OOTB search tokens are available to build the query but here we will be focusing on only the custom query token to apply field filter for the date.

When it comes to filtering the search results by date, there will be different scenarios that will come into picture. Let's see some of them now.

Upcoming Events: To filter only the upcoming events for the pool of events, we need to apply date range filter to the date filed defined in the events template. Solr comes with a query to filter upcoming events. That is [NOW TO *]. But to apply this in our custom query token, we need to specify that the token is used for date comparison. So Sitecore provides a token (#datecompare#) to specify this. For upcoming events the below shown query can be used.  

Today Events: To filter only today events instead of NOW in the above query, replace it with today's date in the default content search date format. For example "2022-06-24T00:00:00Z"

<setting name="ContentSearch.DateFormat" value="yyyy-MM-dd'T'HH:mm:ss'Z'" patch:source="Sitecore.XA.Foundation.Search.Solr.config"/>

Past Events: It is simply opposite to the upcoming events. The value should be [* TO NOW #datecompare#]
From Specific date to a specific date: It will be same approach but we need to update the From and To dates in the above query.

Happy Learning !!




Comments

Popular posts from this blog

Remove bucket folders from page URL

 Hey blog viewers, Welcome to Learn Sitecore.  In this blog we will see how to remove bucket folders from page URL. It is the best practice for the Sitecore application to have a clean URL to meet it's SEO purposes. So let's go through how we can achieve this.  First, in order to remove the bucket folders from the URL we have to decide the URL structure we need to keep and follow for the pages stored under bucket folder. For the instance I am taking the following URL https://developersite.com/news/2022/06/17/news-1. In this URL 2022/06/17 represents the bucket folders. This structure of bucket folders can be different based on the structure that one will follow up on the requirement. After removing the bucket folders from URL, it will be https://developersite.com/news/news-1 . In order to do this we first need to build this customized URL. For that we would need to create a new link provider which overrides the GetItemUrl method present in the link providers pro...

Custom field validation rule for date range in Sitecore

 Hello learners, Welcome to Learn Sitecore. In this blog we will see how to create custom field validation rule for a field of an item. Providing a field validation rule to a field provides a great flexibility to the developers as well as good experience for content authors. By adding it choosing an invalid date by content author and showing it in the website can be eliminated at the first step itself. It is recommended to use field validations for different fields based on the business requirements. To implement a custom field rule validation, navigate to  /sitecore/system/Settings/Validation Rules/Field Rules and insert a validation rule. After that fill the data into Text and Data sections as follows. Text Section Data Section In the Type field add the data as class reference name, assembly name. Now create a class file that extends the standard validator class. Write the logic as per your requirement in the Evaluate function. For the instance I have added a sample code wh...

Adding Custom Classes in body tag of page in Sitecore SXA

Hello learners, Welcome to Learn Sitecore. In this blog we will see how to add custom classes in the body tag of page in Sitecore SXA. This is useful when we need to add classes in the body tag based of the evaluation of some conditions.  For example let's consider a scenario where the requirement is to add a class or classes to the body tag of the page when the user is logged into the website. In this scenario, we can not use the Sitecore OOTB body class field which is present on the page because it will add class to the body tag irrespective of user logged in or not.  So to achieve this requirement, we have two ways. They are  1. Override the Sitecore OOTB pipeline processor available in Sitecore SXA as shown  Decorate Page Pipeline 2. Add a new processor to the Decorate page pipeline without disturbing the existing processors.  We will see how to add a new processor into the decorate page pipeline in this post.  As we can not re-use the body class f...