| 1 |
Index: modules/search/search.module
|
| 2 |
===================================================================
|
| 3 |
--- modules/search/search.module (revision 129)
|
| 4 |
+++ modules/search/search.module (working copy)
|
| 5 |
@@ -835,47 +835,61 @@
|
| 6 |
*
|
| 7 |
* @ingroup search
|
| 8 |
*/
|
| 9 |
-function do_search($keywords, $type, $join1 = '', $where1 = '1', $arguments1 = array(), $select2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') {
|
| 10 |
- $query = search_parse_query($keywords);
|
| 11 |
+if (!module_exists('xapian')) {
|
| 12 |
+ if (!function_exists('xapian_available')) {
|
| 13 |
+ function do_search($keywords, $type, $join1 = '', $where1 = '1', $arguments1 = array(), $select2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') {
|
| 14 |
+ $start_time = microtime(TRUE);
|
| 15 |
|
| 16 |
- if ($query[2] == '') {
|
| 17 |
- form_set_error('keys', t('You must include at least one positive keyword with @count characters or more.', array('@count' => variable_get('minimum_word_size', 3))));
|
| 18 |
+ $query = search_parse_query($keywords);
|
| 19 |
+
|
| 20 |
+ if ($query[2] == '') {
|
| 21 |
+ form_set_error('keys', t('You must include at least one positive keyword with @count characters or more.', array('@count' => variable_get('minimum_word_size', 3))));
|
| 22 |
+ }
|
| 23 |
+ if ($query === NULL || $query[0] == '' || $query[2] == '') {
|
| 24 |
+ return array();
|
| 25 |
+ }
|
| 26 |
+
|
| 27 |
+ // First pass: select all possible matching sids, doing a simple index-based OR matching on the keywords.
|
| 28 |
+ // 'matches' is used to reject those items that cannot possibly match the query.
|
| 29 |
+ $conditions = $where1 .' AND ('. $query[2] .") AND i.type = '%s'";
|
| 30 |
+ $arguments = array_merge($arguments1, $query[3], array($type, $query[4]));
|
| 31 |
+ $result = db_query_temporary("SELECT i.type, i.sid, SUM(i.score * t.count) AS relevance, COUNT(*) AS matches FROM {search_index} i INNER JOIN {search_total} t ON i.word = t.word $join1 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d", $arguments, 'temp_search_sids');
|
| 32 |
+
|
| 33 |
+ // Calculate maximum relevance, to normalize it
|
| 34 |
+ $normalize = db_result(db_query('SELECT MAX(relevance) FROM temp_search_sids'));
|
| 35 |
+ if (!$normalize) {
|
| 36 |
+ return array();
|
| 37 |
+ }
|
| 38 |
+ $select2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * i.relevance)', $select2);
|
| 39 |
+
|
| 40 |
+ // Second pass: only keep items that match the complicated keywords conditions (phrase search, negative keywords, ...)
|
| 41 |
+ $conditions = '('. $query[0] .')';
|
| 42 |
+ $arguments = array_merge($arguments2, $query[1]);
|
| 43 |
+ $result = db_query_temporary("SELECT i.type, i.sid, $select2 FROM temp_search_sids i INNER JOIN {search_dataset} d ON i.sid = d.sid AND i.type = d.type $join2 WHERE $conditions $sort_parameters", $arguments, 'temp_search_results');
|
| 44 |
+ if (($count = db_result(db_query('SELECT COUNT(*) FROM temp_search_results'))) == 0) {
|
| 45 |
+ return array();
|
| 46 |
+ }
|
| 47 |
+ $count_query = "SELECT $count";
|
| 48 |
+
|
| 49 |
+ // Do actual search query
|
| 50 |
+ $result = pager_query("SELECT * FROM temp_search_results", 10, 0, $count_query);
|
| 51 |
+ $results = array();
|
| 52 |
+ while ($item = db_fetch_object($result)) {
|
| 53 |
+ $results[] = $item;
|
| 54 |
+ }
|
| 55 |
+
|
| 56 |
+ if (variable_get('xapian_log_queries', FALSE)) {
|
| 57 |
+ $time_taken = (microtime(TRUE) - $start_time) * 1000;
|
| 58 |
+ watchdog('xapian', t('<p>Query: %desc </p><p>Query time: %timems</p>',
|
| 59 |
+ array('%desc' => $keywords, '%time' => $time_taken)));
|
| 60 |
+ }
|
| 61 |
+
|
| 62 |
+ return $results;
|
| 63 |
+ }
|
| 64 |
}
|
| 65 |
- if ($query === NULL || $query[0] == '' || $query[2] == '') {
|
| 66 |
- return array();
|
| 67 |
- }
|
| 68 |
+}
|
| 69 |
|
| 70 |
- // First pass: select all possible matching sids, doing a simple index-based OR matching on the keywords.
|
| 71 |
- // 'matches' is used to reject those items that cannot possibly match the query.
|
| 72 |
- $conditions = $where1 .' AND ('. $query[2] .") AND i.type = '%s'";
|
| 73 |
- $arguments = array_merge($arguments1, $query[3], array($type, $query[4]));
|
| 74 |
- $result = db_query_temporary("SELECT i.type, i.sid, SUM(i.score * t.count) AS relevance, COUNT(*) AS matches FROM {search_index} i INNER JOIN {search_total} t ON i.word = t.word $join1 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d", $arguments, 'temp_search_sids');
|
| 75 |
|
| 76 |
- // Calculate maximum relevance, to normalize it
|
| 77 |
- $normalize = db_result(db_query('SELECT MAX(relevance) FROM temp_search_sids'));
|
| 78 |
- if (!$normalize) {
|
| 79 |
- return array();
|
| 80 |
- }
|
| 81 |
- $select2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * i.relevance)', $select2);
|
| 82 |
-
|
| 83 |
- // Second pass: only keep items that match the complicated keywords conditions (phrase search, negative keywords, ...)
|
| 84 |
- $conditions = '('. $query[0] .')';
|
| 85 |
- $arguments = array_merge($arguments2, $query[1]);
|
| 86 |
- $result = db_query_temporary("SELECT i.type, i.sid, $select2 FROM temp_search_sids i INNER JOIN {search_dataset} d ON i.sid = d.sid AND i.type = d.type $join2 WHERE $conditions $sort_parameters", $arguments, 'temp_search_results');
|
| 87 |
- if (($count = db_result(db_query('SELECT COUNT(*) FROM temp_search_results'))) == 0) {
|
| 88 |
- return array();
|
| 89 |
- }
|
| 90 |
- $count_query = "SELECT $count";
|
| 91 |
-
|
| 92 |
- // Do actual search query
|
| 93 |
- $result = pager_query("SELECT * FROM temp_search_results", 10, 0, $count_query);
|
| 94 |
- $results = array();
|
| 95 |
- while ($item = db_fetch_object($result)) {
|
| 96 |
- $results[] = $item;
|
| 97 |
- }
|
| 98 |
- return $results;
|
| 99 |
-}
|
| 100 |
-
|
| 101 |
/**
|
| 102 |
* Helper function for grabbing search keys.
|
| 103 |
*/
|