Issue #893302 by jhodgdon, rayasa: Search ranking based on the factor 'number of comments' & 'No of Page Views' is broken.

8.0.x
Nathaniel Catchpole 2014-02-12 10:56:06 +00:00
parent 7dcaa39580
commit 3ea2e2cdae
3 changed files with 19 additions and 32 deletions

View File

@ -1041,10 +1041,11 @@ function comment_node_update_index(EntityInterface $node, $langcode) {
} }
/** /**
* Implements hook_update_index(). * Implements hook_cron().
*/ */
function comment_update_index() { function comment_cron() {
// Store the maximum possible comments per thread (used for ranking by reply count) // Store the maximum possible comments per thread (used for node search
// ranking by reply count).
\Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1, db_query('SELECT MAX(comment_count) FROM {comment_entity_statistics}')->fetchField())); \Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1, db_query('SELECT MAX(comment_count) FROM {comment_entity_statistics}')->fetchField()));
} }
@ -1557,8 +1558,8 @@ function comment_ranking() {
'on' => "ces.entity_id = i.sid AND ces.entity_type = 'node' AND ces.field_id = 'node__comment'", 'on' => "ces.entity_id = i.sid AND ces.entity_type = 'node' AND ces.field_id = 'node__comment'",
), ),
// Inverse law that maps the highest reply count on the site to 1 and 0 to 0. // Inverse law that maps the highest reply count on the site to 1 and 0 to 0.
'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * CAST(:scale AS DECIMAL))', 'score' => '2.0 - 2.0 / (1.0 + ces.comment_count * :scale)',
'arguments' => array(':scale' => \Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0), 'arguments' => array(':scale' => (float) \Drupal::state()->get('comment.node_comment_statistics_scale') ?: 0),
), ),
); );
} }

View File

@ -83,10 +83,6 @@ class SearchRankingTest extends SearchTestBase {
} }
} }
// Update the search index.
$this->nodeSearch->getPlugin()->updateIndex();
search_update_totals();
// Add a comment to one of the nodes. // Add a comment to one of the nodes.
$edit = array(); $edit = array();
$edit['subject'] = 'my comment title'; $edit['subject'] = 'my comment title';
@ -98,22 +94,16 @@ class SearchRankingTest extends SearchTestBase {
// Enable counting of statistics. // Enable counting of statistics.
\Drupal::config('statistics.settings')->set('count_content_views', 1)->save(); \Drupal::config('statistics.settings')->set('count_content_views', 1)->save();
// Then View one of the nodes a bunch of times. // Simulating content views is kind of difficult in the test. Leave that
// Manually calling statistics.php, simulating ajax behavior. // to the Statistics module. So instead go ahead and manually update the
$client = \Drupal::httpClient(); // counter for this node.
$client->setConfig(array('curl.options' => array(CURLOPT_TIMEOUT => 10)));
$nid = $nodes['views'][1]->id(); $nid = $nodes['views'][1]->id();
global $base_url; db_insert('node_counter')
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php'; ->fields(array('totalcount' => 5, 'daycount' => 5, 'timestamp' => REQUEST_TIME, 'nid' => $nid))
for ($i = 0; $i < 5; $i ++) { ->execute();
$client->post($stats_path, array(), array('nid' => $nid))->send();
}
// @todo - comments and views are removed from the array since they are // Run cron to update the search index and comment/statistics totals.
// broken in core. Those modules expected hook_update_index() to be called $this->cronRun();
// even though it was only called on modules that implemented a search type.
array_pop($node_ranks);
array_pop($node_ranks);
// Test that the settings form displays the context ranking section. // Test that the settings form displays the context ranking section.
$this->drupalGet('admin/config/search/settings/manage/node_search'); $this->drupalGet('admin/config/search/settings/manage/node_search');

View File

@ -118,6 +118,9 @@ function statistics_cron() {
->execute(); ->execute();
\Drupal::state()->set('statistics.day_timestamp', REQUEST_TIME); \Drupal::state()->set('statistics.day_timestamp', REQUEST_TIME);
} }
// Calculate the maximum of node views, for node search ranking.
\Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1, db_query('SELECT MAX(totalcount) FROM {node_counter}')->fetchField()));
} }
/** /**
@ -241,20 +244,13 @@ function statistics_ranking() {
'on' => 'node_counter.nid = i.sid', 'on' => 'node_counter.nid = i.sid',
), ),
// Inverse law that maps the highest view count on the site to 1 and 0 to 0. // 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 * CAST(:scale AS DECIMAL))', 'score' => '2.0 - 2.0 / (1.0 + node_counter.totalcount * :scale)',
'arguments' => array(':scale' => \Drupal::state()->get('statistics.node_counter_scale') ?: 0), 'arguments' => array(':scale' => (float) \Drupal::state()->get('statistics.node_counter_scale') ?: 0),
), ),
); );
} }
} }
/**
* Implements hook_update_index().
*/
function statistics_update_index() {
\Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1, db_query('SELECT MAX(totalcount) FROM {node_counter}')->fetchField()));
}
/** /**
* Implements hook_preprocess_HOOK() for block templates. * Implements hook_preprocess_HOOK() for block templates.
*/ */