I need to search the content that has a date equal to today so that the search results component would only show the items that are only valid for today and later I'm using SXA 1.7
Search Results Boosing
You can use search results boosting for this. And here are two approaches:
- It won't show you only the items with an actual date but will boost the result with the current date at the top of your results. Then if you know that you will have only one item which is marked with the current date you can select the page size of the Search Results rendering to 1 and have just that one item displayed.
- You will implement boosting rule differently then we are doing this in the SXA. Without
c.Name != string.Emptyadded to each predicate at the end to have rest of the results.
Such rule doesn't exist out of the box and you will need to create it on your own. But thanks to the dynamic search results boosting in SXA you can do that.
- Implement new rule - take a look at
Sitecore.XA.Foundation.Search.Rules.WhenFieldAndQueryMatches<T>class. I believe you can even use a copy of that class with a new name. The FieldName property is important.
- Add a processor to
ResolveBoostingQuerySXA pipeline - you need a code which will resolve boosting. The most helpful will be probably
Sitecore.XA.Foundation.Search.Pipelines.ResolveBoostingQuery.ResolveFieldAndQueryMatchRuleclass. All you need to do is to compare field value with a date from your item to
- Add a new item of template
Condition. We have SXA ones located here:
And just have fun:
Search Results Tokens
The other approach is to write your own custom search results token. In SXA we already have a couple of custom ones. Just click
Build query in the scope item, type SXA and you will see something like this:
You can write your own ones. All you need to do is to write custom processor to resolve token - take a look at
Sitecore.XA.Foundation.Search.Pipelines.ResolveSearchQueryTokens.CurrentTemplate class. For more advance example take a look here
I also found this solution that uses Solr syntax to search on the date