Drupal 8 & Apache Solr: Boost Search Term Relevance by Publish Date

Apache Solr site search is a great upgrade for any search reliant Drupal 8 site, but getting recently published content to appear higher in search results isn't straightforward. The newest Drupal 8 compatible version of Search API Solr Search, a module that connects your configured Apache Solr Server to your new Drupal 8 site, makes adjusting the relevance of all your other node fields pretty easy. There's a nice, simple interface for adjusting the relative weight of each of your fields so that Apache Solr knows how much to boost the score that each field returns when it matches your users' search terms.

For now, though, you can't adjust boosting by recent published date without code. Luckily the hook implementation is pretty simple:

/**
 * @file
 * MYMODULE.module implements Search API Solr Search hook for recently published date boost
 */
 
/**
 * Implements hook_search_api_solr_query_alter()
 */
function MYMODULE_search_api_solr_query_alter(\Solarium\QueryType\Select\Query\Query $solarium_query, \Drupal\search_api\Query\QueryInterface $query) {
  $date_field = 'created'; // This can be any UNIX timestamp field
  $index = $query->getIndex();
  $fields = $index->getServerInstance()->getBackend()->getSolrFieldNames($index);
  $solr_field = !empty($fields[$date_field]) ? $fields[$date_field] : '';
  // Our last two values below, .08 and .05, are "a" and "b" respectively in the image below
  if ($solr_field) {
    $solarium_query->addParam('bf', "recip(abs(ms(NOW,{$solr_field})),3.16e-11,.08,.05)");
  }
}

This module relies on the Search API Solr Search module mentioned above. It's a hook implementation, so it won't do anything unless you've got Apache Solr up and running via Search API Solr Search.

The magic of this code is the last line where the boost is actually added. You can adjust the last couple of values in the addParam method to tweak your boost. Here's a quick graph that demonstrates the effect of adjusting these values (based on a low-res image of the same I found here).

Drupal 8 & Apache solr, algorithm that drives the boost

Why would you want to boost recently published content? The short answer is that it's often more relevant, especially for large scale content publishers. If someone's searching your website for Trump Justice Department, for example, they're probably looking for a January 2018 story on President Trump's complaints about his inability to issue direct orders to DOJ officials, not information related to the DOJ's contention in 1973 that the Trump Organization systematically discriminated against African American renters.

Code Drupal Drupal 8

Read This Next