- Patch #145242 by douggreen et al: refactor node_rank to modules can add scoring factors.
parent
658b27c9ec
commit
11aeff6016
|
@ -1970,3 +1970,18 @@ function comment_unpublish_by_keyword_action($comment, $context) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_ranking().
|
||||
*/
|
||||
function comment_ranking() {
|
||||
return array(
|
||||
'comments' => array(
|
||||
'title' => t('Number of comments'),
|
||||
'join' => 'LEFT JOIN {node_comment_statistics} node_comment_statistics ON node_comment_statistics.nid = i.sid',
|
||||
// Inverse law that maps the highest reply count on the site to 1 and 0 to 0.
|
||||
'score' => '2.0 - 2.0 / (1.0 + node_comment_statistics.comment_count * %f)',
|
||||
'arguments' => array(variable_get('node_cron_comments_scale', 0)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1141,6 +1141,39 @@ function node_perm() {
|
|||
return $perms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gather the rankings from the the hook_ranking implementations.
|
||||
*/
|
||||
function _node_rankings() {
|
||||
$rankings = array(
|
||||
'total' => 0, 'join' => array(), 'score' => array(), 'args' => array(),
|
||||
);
|
||||
if ($ranking = module_invoke_all('ranking')) {
|
||||
foreach ($ranking as $rank => $values) {
|
||||
if ($node_rank = variable_get('node_rank_'. $rank, 5)) {
|
||||
// If the table defined in the ranking isn't already joined, then add it.
|
||||
if (isset($values['join']) && !isset($rankings['join'][$values['join']])) {
|
||||
$rankings['join'][$values['join']] = $values['join'];
|
||||
}
|
||||
|
||||
// Add the rankings weighted score multiplier value, handling NULL gracefully.
|
||||
$rankings['score'][] = '%f * COALESCE(('. $values['score'] .'), 0)';
|
||||
|
||||
// Add the the administrator's weighted score multiplier value for this ranking.
|
||||
$rankings['total'] += $node_rank;
|
||||
$rankings['arguments'][] = $node_rank;
|
||||
|
||||
// Add any additional arguments used by this ranking.
|
||||
if (isset($values['arguments'])) {
|
||||
$rankings['arguments'] = array_merge($rankings['arguments'], $values['arguments']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $rankings;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of hook_search().
|
||||
*/
|
||||
|
@ -1170,23 +1203,14 @@ function node_search($op = 'search', $keys = NULL) {
|
|||
'#value' => '<em>' . t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '</em>'
|
||||
);
|
||||
|
||||
$ranking = array('node_rank_relevance' => t('Keyword relevance'),
|
||||
'node_rank_recent' => t('Recently posted'));
|
||||
if (module_exists('comment')) {
|
||||
$ranking['node_rank_comments'] = t('Number of comments');
|
||||
}
|
||||
if (module_exists('statistics') && variable_get('statistics_count_content_views', 0)) {
|
||||
$ranking['node_rank_views'] = t('Number of views');
|
||||
}
|
||||
|
||||
// Note: reversed to reflect that higher number = higher ranking.
|
||||
$options = drupal_map_assoc(range(0, 10));
|
||||
foreach ($ranking as $var => $title) {
|
||||
$form['content_ranking']['factors'][$var] = array(
|
||||
'#title' => $title,
|
||||
foreach (module_invoke_all('ranking') as $var => $values) {
|
||||
$form['content_ranking']['factors']['node_rank_'. $var] = array(
|
||||
'#title' => $values['title'],
|
||||
'#type' => 'select',
|
||||
'#options' => $options,
|
||||
'#default_value' => variable_get($var, 5),
|
||||
'#default_value' => variable_get('node_rank_'. $var, 5),
|
||||
);
|
||||
}
|
||||
return $form;
|
||||
|
@ -1228,60 +1252,22 @@ function node_search($op = 'search', $keys = NULL) {
|
|||
$keys = search_query_insert($keys, 'language');
|
||||
}
|
||||
|
||||
// Build ranking expression (we try to map each parameter to a
|
||||
// uniform distribution in the range 0..1).
|
||||
$ranking = array();
|
||||
$arguments2 = array();
|
||||
$join2 = '';
|
||||
// Used to avoid joining on node_comment_statistics twice
|
||||
$stats_join = FALSE;
|
||||
$total = 0;
|
||||
if ($weight = (int)variable_get('node_rank_relevance', 5)) {
|
||||
// Average relevance values hover around 0.15
|
||||
$ranking[] = '%d * i.relevance';
|
||||
$arguments2[] = $weight;
|
||||
$total += $weight;
|
||||
}
|
||||
if ($weight = (int)variable_get('node_rank_recent', 5)) {
|
||||
// Exponential decay with half-life of 6 months, starting at last indexed node
|
||||
$ranking[] = '%d * POW(2, (GREATEST(MAX(n.created), MAX(n.changed), MAX(c.last_comment_timestamp)) - %d) * 6.43e-8)';
|
||||
$arguments2[] = $weight;
|
||||
$arguments2[] = (int)variable_get('node_cron_last', 0);
|
||||
$join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid';
|
||||
$stats_join = TRUE;
|
||||
$total += $weight;
|
||||
}
|
||||
if (module_exists('comment') && $weight = (int)variable_get('node_rank_comments', 5)) {
|
||||
// Inverse law that maps the highest reply count on the site to 1 and 0 to 0.
|
||||
$scale = variable_get('node_cron_comments_scale', 0.0);
|
||||
$ranking[] = '%d * (2.0 - 2.0 / (1.0 + MAX(c.comment_count) * %f))';
|
||||
$arguments2[] = $weight;
|
||||
$arguments2[] = $scale;
|
||||
if (!$stats_join) {
|
||||
$join2 .= ' LEFT JOIN {node_comment_statistics} c ON c.nid = i.sid';
|
||||
}
|
||||
$total += $weight;
|
||||
}
|
||||
if (module_exists('statistics') && variable_get('statistics_count_content_views', 0) &&
|
||||
$weight = (int)variable_get('node_rank_views', 5)) {
|
||||
// Inverse law that maps the highest view count on the site to 1 and 0 to 0.
|
||||
$scale = variable_get('node_cron_views_scale', 0.0);
|
||||
$ranking[] = '%d * (2.0 - 2.0 / (1.0 + MAX(nc.totalcount) * %f))';
|
||||
$arguments2[] = $weight;
|
||||
$arguments2[] = $scale;
|
||||
$join2 .= ' LEFT JOIN {node_counter} nc ON nc.nid = i.sid';
|
||||
$total += $weight;
|
||||
}
|
||||
// Get the ranking expressions.
|
||||
$rankings = _node_rankings();
|
||||
|
||||
// When all search factors are disabled (ie they have a weight of zero),
|
||||
// the default score is based only on keyword relevance and there is no need to
|
||||
// adjust the score of each item.
|
||||
if ($total == 0) {
|
||||
$select2 = 'i.relevance AS score';
|
||||
// The default score is based only on keyword relevance.
|
||||
if ($rankings['total'] == 0) {
|
||||
$total = 1;
|
||||
$arguments2 = array();
|
||||
$join2 = '';
|
||||
$select2 = 'i.relevance AS score';
|
||||
}
|
||||
else {
|
||||
$select2 = implode(' + ', $ranking) . ' AS score';
|
||||
$total = $rankings['total'];
|
||||
$arguments2 = $rankings['arguments'];
|
||||
$join2 = implode(' ', $rankings['join']);
|
||||
$select2 = '('. implode(' + ', $rankings['score']) .') AS score';
|
||||
}
|
||||
|
||||
// Do search.
|
||||
|
@ -1302,6 +1288,7 @@ function node_search($op = 'search', $keys = NULL) {
|
|||
$node->body .= module_invoke('taxonomy', 'nodeapi', $node, 'update index');
|
||||
|
||||
$extra = node_invoke_nodeapi($node, 'search result');
|
||||
|
||||
$results[] = array(
|
||||
'link' => url('node/' . $item->sid, array('absolute' => TRUE)),
|
||||
'type' => check_plain(node_get_types('name', $node)),
|
||||
|
@ -1310,7 +1297,7 @@ function node_search($op = 'search', $keys = NULL) {
|
|||
'date' => $node->changed,
|
||||
'node' => $node,
|
||||
'extra' => $extra,
|
||||
'score' => $item->score / $total,
|
||||
'score' => $total ? ($item->score / $total) : 0,
|
||||
'snippet' => search_excerpt($keys, $node->body),
|
||||
);
|
||||
}
|
||||
|
@ -1318,6 +1305,41 @@ function node_search($op = 'search', $keys = NULL) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_ranking().
|
||||
*/
|
||||
function node_ranking() {
|
||||
// Create the ranking array and add the basic ranking options.
|
||||
$ranking = array(
|
||||
'relevance' => array(
|
||||
'title' => t('Keyword relevance'),
|
||||
// Average relevance values hover around 0.15
|
||||
'score' => 'i.relevance',
|
||||
),
|
||||
'sticky' => array(
|
||||
'title' => t('Content is sticky at top of lists'),
|
||||
// The sticky flag is either 0 or 1, which is automatically normalized.
|
||||
'score' => 'n.sticky',
|
||||
),
|
||||
'promote' => array(
|
||||
'title' => t('Content is promoted to the front page'),
|
||||
// The promote flag is either 0 or 1, which is automatically normalized.
|
||||
'score' => 'n.promote',
|
||||
),
|
||||
);
|
||||
|
||||
// Add relevance based on creation or changed date.
|
||||
if ($node_cron_last = variable_get('node_cron_last', 0)) {
|
||||
$ranking['recent'] = array(
|
||||
'title' => t('Recently posted'),
|
||||
// Exponential decay with half-life of 6 months, starting at last indexed node
|
||||
'score' => '(POW(2, GREATEST(n.created, n.changed) - %d) * 6.43e-8)',
|
||||
'arguments' => array($node_cron_last),
|
||||
);
|
||||
}
|
||||
return $ranking;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_user().
|
||||
*/
|
||||
|
|
|
@ -160,3 +160,91 @@ class SearchMatchTestCase extends DrupalWebTestCase {
|
|||
$this->assertEqual(!count($scores) || (min($scores) > 0.0 && max($scores) <= 1.0001), TRUE, "Query scoring '$query'");
|
||||
}
|
||||
}
|
||||
|
||||
class SearchRankingTestCase extends DrupalWebTestCase {
|
||||
/**
|
||||
* Implementation of getInfo().
|
||||
*/
|
||||
function getInfo() {
|
||||
return array(
|
||||
'name' => t('Search engine ranking'),
|
||||
'description' => t('Indexes content and tests ranking factors.'),
|
||||
'group' => t('Search'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation setUp().
|
||||
*/
|
||||
function setUp() {
|
||||
parent::setUp('search');
|
||||
}
|
||||
|
||||
function testRankings() {
|
||||
// Enable modules that are tested.
|
||||
$this->drupalModuleEnable('search');
|
||||
$this->drupalModuleEnable('statistics');
|
||||
$this->drupalModuleEnable('comment');
|
||||
|
||||
// Login with sufficient privileges.
|
||||
$this->drupalLogin($this->drupalCreateUser(array('post comments without approval', 'create page content')));
|
||||
|
||||
// Build a list of the rankings to test.
|
||||
$node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views');
|
||||
|
||||
// Create nodes for testing.
|
||||
foreach ($node_ranks as $node_rank) {
|
||||
$settings = array('type' => 'page', 'title' => 'Drupal rocks', 'body' => "Drupal's search rocks");
|
||||
foreach (array(0, 1) as $num) {
|
||||
if ($num == 1) {
|
||||
switch ($node_rank) {
|
||||
case 'sticky':
|
||||
case 'promote':
|
||||
$settings[$node_rank] = 1;
|
||||
break;
|
||||
case 'relevance':
|
||||
$settings['body'] .= " really rocks";
|
||||
break;
|
||||
case 'recent':
|
||||
$settings['created'] = time() + 3600;
|
||||
break;
|
||||
case 'comments':
|
||||
$settings['comment'] = 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$nodes[$node_rank][$num] = $this->drupalCreateNode($settings);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the search index.
|
||||
node_update_index();
|
||||
search_update_totals();
|
||||
|
||||
// Add a comment to one of the nodes.
|
||||
$edit = array('subject' => 'my comment title', 'comment' => 'some random comment');
|
||||
$this->drupalGet('comment/reply/'. $nodes['comments'][1]->nid);
|
||||
$this->drupalPost(NULL, $edit, t('Preview'));
|
||||
$this->drupalPost(NULL, $edit, t('Save'));
|
||||
|
||||
// Enable counting of statistics.
|
||||
variable_set('statistics_count_content_views', 1);
|
||||
|
||||
// Then View one of the nodes a bunch of times.
|
||||
for ($i = 0; $i < 5; $i ++) {
|
||||
$this->drupalGet('node/' . $nodes['views'][1]->nid);
|
||||
}
|
||||
|
||||
// Test each of the possible rankings.
|
||||
foreach ($node_ranks as $node_rank) {
|
||||
// Disable all relevancy rankings except the one we are testing.
|
||||
foreach ($node_ranks as $var) {
|
||||
variable_set('node_rank_'. $var, $var == $node_rank ? 10 : 0);
|
||||
}
|
||||
|
||||
// Do the search and assert the results.
|
||||
$set = node_search('search', 'rocks');
|
||||
$this->assertEqual($set[0]['node']->nid, $nodes[$node_rank][1]->nid, 'Search ranking "'. $node_rank .'" order.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -314,3 +314,20 @@ function statistics_nodeapi(&$node, $op, $arg = 0) {
|
|||
db_query('DELETE FROM {node_counter} WHERE nid = %d', $node->nid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of hook_ranking().
|
||||
*/
|
||||
function statistics_ranking() {
|
||||
if (variable_get('statistics_count_content_views', 0)) {
|
||||
return array(
|
||||
'views' => array(
|
||||
'title' => t('Number of views'),
|
||||
'join' => 'LEFT JOIN {node_counter} node_counter ON node_counter.nid = i.sid',
|
||||
// Inverse law that maps the highest view count on the site to 1 and 0 to 0.
|
||||
'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * %f)',
|
||||
'arguments' => array(variable_get('node_cron_views_scale', 0)),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue