Sitecore Search API Boost by Date Range

Bhavik Patel • 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!

Bhavik Patel

Bhavik is a .NET developer with over 9 years of experience, focusing on Sitecore CMS for the last 3 years. He has worked on several Sitecore CMS projects in both single-site and multi-site scenarios, and truly enjoys how extensible it is. He has a passion for problem solving and using technology to create engaging websites and applications. Bhavik is a Sitecore Technology MVP, 2015 and 2016.

Explore topics:

Other Blog Posts from Bhavik Patel

Subscribe for insights, trends and big ideas