Skip to content

Instantly share code, notes, and snippets.

@hannesl
Created January 10, 2012 08:14
Show Gist options
  • Select an option

  • Save hannesl/1587809 to your computer and use it in GitHub Desktop.

Select an option

Save hannesl/1587809 to your computer and use it in GitHub Desktop.
A Drupal search result ranking scheme for just the post date.
/**
* Implements of hook_ranking(). Copied from node_ranking().
*/
function MYMODULE_ranking() {
$ranking = array();
// Add ranking based on just the creation date (node_ranking() also uses modified
// date).
if ($node_cron_last = variable_get('node_cron_last', 0)) {
$ranking['MYMODULE_recent'] = array(
'title' => t('Recently posted, ignore modification date'),
// Exponential decay with half-life of 6 months, starting at last indexed node
'score' => 'POW(2.0, (n.created - :node_cron_last) * 6.43e-8)',
'arguments' => array(':node_cron_last' => $node_cron_last),
);
}
return $ranking;
}
@hannesl
Copy link
Author

hannesl commented Jan 10, 2012

In Drupal 7, you can use a number of different ranking schemes at config/search/settings to control the order of the search results. The "Post date" scheme provided by core also takes the modification date into account which may not be what you want. Put this hook in a module and you'll get a new scheme what ranks results on just the post date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment