? morelikethis-869730.patch
Index: contrib/morelikethis_taxonomy/morelikethis_taxonomy.class.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/morelikethis/contrib/morelikethis_taxonomy/Attic/morelikethis_taxonomy.class.inc,v
retrieving revision 1.1.2.3
diff -u -p -r1.1.2.3 morelikethis_taxonomy.class.inc
--- contrib/morelikethis_taxonomy/morelikethis_taxonomy.class.inc	7 Jan 2009 21:09:33 -0000	1.1.2.3
+++ contrib/morelikethis_taxonomy/morelikethis_taxonomy.class.inc	30 Jul 2010 16:39:18 -0000
@@ -40,30 +40,56 @@ class MoreLikeThisTaxonomy extends MoreL
     $count = empty($count) ? 10 : intval($count);
     $threshold = floatval(variable_get("morelikethis_taxonomy_threshold_{$key}", 0));
 
-    $sql = "SELECT n.nid, n.type, n.title, count(*) as hits, count(*)/$term_count as relevance";
+    $sql = "SELECT n.nid, n.type, n.title, count(*) as hits";
     $sql .= " FROM {node} n";
-    $sql .= " JOIN {term_node} tn ON n.nid = tn.nid";
+    $sql .= " JOIN {term_node} tn ON n.nid = tn.nid";    
     $sql .= " JOIN {term_data} td ON tn.tid = td.tid";
     $sql .= " WHERE n.nid <> %d";
+    $sql .= " AND n.status <> 0";        
     $sql .= " AND n.type IN (" . db_placeholders($types, 'varchar') . ")";
     $sql .= " AND td.name IN (" . db_placeholders($this->terms, 'varchar') . ")";
-    $sql .= " AND n.status = 1";
     $sql .= " GROUP BY n.nid";
-    $sql .= " HAVING relevance >= %f";
-    $sql .= " ORDER BY hits DESC, n.nid DESC";
-    $args = array_merge(array($this->node->nid), $types, $this->terms, array($threshold));
-
-    $result = db_query_range($sql, $args, 0, $count);
+    $sql .= " HAVING hits >= %f";
+    
+    $args = array_merge(array($this->node->nid), $types, $this->terms, array($threshold * $term_count));
 
+    $result = db_query($sql, $args);
+    
     $likeness = array();
-    while($likenode = db_fetch_object($result)) {
+    /*
+     * Use the maximum number of results as configured on the settings screen
+     * to limit the amount of data we'll be sorting
+     */ 
+    $i = 0;
+    $max_results = variable_get("morelikethis_taxonomy_max_results", 1000);
+    
+    while($i < $max_results && $likenode = db_fetch_object($result)) {
       $likenode->id = $likenode->nid;
       $likenode->url = "node/$likenode->nid";
       $likenode->mlt_type = 'taxonomy';
       $likeness[] = $likenode;
+      $i++;
     }
-		
+    
+    /*
+     * Sorting done in PHP for performance reasons
+     * using "ORDER BY hits DESC" triggers the creation of temporary MySQL tables
+     * and the use of filesort because "hits" is an aggregate value that is not
+     * indexed 
+     */
+    usort($likeness, "_morelikethis_taxonomy_compare_relevance");
+    $likeness = array_slice($likeness, 0, $count);
+    
     return $likeness;
   }  
 }
 
+/**
+ * Helper function for usort above.
+ * Used to order results by relevance.
+ */
+function _morelikethis_taxonomy_compare_relevance($a, $b) {
+  if ($a->hits === $b->hits) return 0;
+  return ($a->hits > $b->hits) ? -1 : 1;
+}
+
Index: contrib/morelikethis_taxonomy/morelikethis_taxonomy.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/morelikethis/contrib/morelikethis_taxonomy/Attic/morelikethis_taxonomy.module,v
retrieving revision 1.1.2.4
diff -u -p -r1.1.2.4 morelikethis_taxonomy.module
--- contrib/morelikethis_taxonomy/morelikethis_taxonomy.module	6 Jan 2009 21:30:33 -0000	1.1.2.4
+++ contrib/morelikethis_taxonomy/morelikethis_taxonomy.module	30 Jul 2010 16:39:18 -0000
@@ -123,7 +123,17 @@ function morelikethis_taxonomy_settings(
       '#description' => t('Set a minimum relevancy score that must be met in order for a match to be created.  Uses a 0.00-1.00 scale based on percentage of matching terms, 0.00 being least relevant (i.e. large number of loosely-related matches returned).'),
     );
   }
-    
+  
+  $form['morelikethis_taxonomy_max_results'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Maximum number of total results'),
+    '#size' => 6,
+    '#maxlength' => 6,
+    '#required' => TRUE,
+    '#default_value' => variable_get("morelikethis_taxonomy_max_results", 1000),
+    '#description' => t('Define the maximum number of total results that should be sorted by relevance by More Like This. The default value is 1000.'),
+  );
+  
   return system_settings_form($form);
 }
 
