Sitecore Search API Boost by Date Range
Posted by Bhavik Patel on 2/9/2017
With Sitecore’s Search API, boosting by date range may seem like a simple a straightforward thing to do. Well, it is if you choose the right (or, rather, suggested) way to do it.
Let’s start with the way in which it will not work:
dateBoostPredicate = dateBoostPredicate.Or(x => x.PublishDate >= start && x.PublishDate < end).Boost(10);
This results in the following query to Lucene (obtained from Sitecore’s search log):
(+publish_date:[20161202t214020466z TO *] +publish_date:[* TO 20170302t214020466z})
As you can see above, the boost at the end of the condition was not added. Essentially, I was expecting it to look something like this:
(+publish_date:[20161202t214020466z TO *] +publish_date:[* TO 20170302t214020466z})^10
After decompiling Sitecore’s DLLs and learning that boosting was not implemented for this particular wildcard query, I reached out to Sitecore support, who pointed me in the right direction. Voila, welcome the “between” extension method. So, I modified my query as follows:
dateBoostPredicate = dateBoostPredicate.Or(x => (x.PublishDate.Between(start, end, Inclusion.Lower)).Boost(10));
And the resulting query issued to Lucene was:
publish_date:[20161202t214020466z TO 20170302t214020466z]^10.0
Perfect! Using the “between” extension method definitely seems obvious after seeing the solution, but it wasn’t when I first attempted the boost. In any case, hope that someone finds this useful someday. A special thanks to Sitecore Support for the assist!