1

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

  • What "date" are you talking about? – Gatogordo Sep 26 '18 at 13:15
  • like if i have a start date field for my items i want the search scope to only include those which are valid starting from today and later – Mohammed Adel Sep 26 '18 at 14:03
5

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.Empty added 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.

  1. 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.
  2. Add a processor to ResolveBoostingQuery SXA pipeline - you need a code which will resolve boosting. The most helpful will be probably Sitecore.XA.Foundation.Search.Pipelines.ResolveBoostingQuery.ResolveFieldAndQueryMatchRule class. All you need to do is to compare field value with a date from your item to DateTime.Now.
  3. Add a new item of template Condition. We have SXA ones located here: /sitecore/system/Settings/Rules/Definitions/Elements/SXA.

And just have fun:

enter image description here

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:

enter image description here

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 Sitecore.XA.Foundation.Search.Pipelines.ResolveSearchQueryTokens.ItemsWithTheSameValueInField.

  • Does the second method work with azure search? – Mohammed Adel Oct 18 '18 at 16:03
  • It should. If you have any problems with it on Azure - let me known. – Dawid Rutkowski Oct 18 '18 at 18:14
2

I also found this solution that uses Solr syntax to search on the date

How does NOW/DAY in a Solr query work

So I can use this syntax in the Custom query enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.