Index: modules/node/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.module,v retrieving revision 1.963 diff -u -F^function -r1.963 node.module --- modules/node/node.module 10 May 2008 13:19:50 -0000 1.963 +++ modules/node/node.module 10 May 2008 21:58:09 -0000 @@ -1193,7 +1193,8 @@ function node_search($op = 'search', $ke case 'search': // Build matching conditions - list($join1, $where1) = _db_rewrite_sql(); + $join1 = ''; + $where1 = ''; $arguments1 = array(); $conditions1 = 'n.status = 1'; @@ -1285,7 +1286,8 @@ function node_search($op = 'search', $ke } // Do search. - $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid ' . $join1 . ' INNER JOIN {users} u ON n.uid = u.uid', $conditions1 . (empty($where1) ? '' : ' AND ' . $where1), $arguments1, $select2, $join2, $arguments2); + $search = search_build_query($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid ' . $join1 . ' INNER JOIN {users} u ON n.uid = u.uid', $conditions1 . (empty($where1) ? '' : ' AND ' . $where1), $arguments1, $select2, $join2, $arguments2); + $find = do_search(db_rewrite_sql($search['query']), db_rewrite_sql($search['count query']), $search['arguments']); // Load results. $results = array(); Index: modules/search/search.module =================================================================== RCS file: /cvs/drupal/drupal/modules/search/search.module,v retrieving revision 1.256 diff -u -F^function -r1.256 search.module --- modules/search/search.module 6 May 2008 12:18:50 -0000 1.256 +++ modules/search/search.module 10 May 2008 21:58:14 -0000 @@ -857,7 +857,7 @@ function _search_parse_query(&$word, &$s } /** - * Do a query on the full-text search index for a word or words. + * Build a SQL query for searching for a word or words in the full-text index. * * This function is normally only called by each module that support the * indexed search (and thus, implements hook_update_index()). @@ -906,11 +906,16 @@ function _search_parse_query(&$word, &$s * Default: 'ORDER BY score DESC' * * @return - * An array of SIDs for the search results. + * An array with the following elements: + * - 'query': The actual SQL search query string. + * - 'count query': A SQL query for counting the number of search + * results. This is useful mainly for paging. + * - 'arguments': An array containing the arguments needed by the query and + * the count query. * * @ingroup search */ -function do_search($keywords, $type, $join1 = '', $where1 = '1', $arguments1 = array(), $columns2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') { +function search_build_query($keywords, $type, $join1 = '', $where1 = '1', $arguments1 = array(), $columns2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') { $query = search_parse_query($keywords); if ($query[2] == '') { @@ -939,18 +944,39 @@ function do_search($keywords, $type, $jo $select = "SELECT SUM(i.score * t.count) AS score FROM {search_index} i $join WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d ORDER BY score DESC"; $arguments = array_merge($arguments1, array($query[4])); $normalize = db_result(db_query_range($select, $arguments, 0, 1)); - if (!$normalize) { - return array(); + if ($normalize) { + $columns2 = ', ' . str_replace('i.relevance', '(' . (1.0 / $normalize) . ' * SUM(i.score * t.count))', $columns2); + } + else { + $columns2 = ''; } - $columns2 = str_replace('i.relevance', '(' . (1.0 / $normalize) . ' * SUM(i.score * t.count))', $columns2); // Build query to retrieve results. - $select = "SELECT i.type, i.sid, $columns2 FROM {search_index} i $join $join2 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d"; - $count_select = "SELECT COUNT(*) FROM ($select) n1"; - $arguments = array_merge($arguments2, $arguments1, array($query[4])); + $select = "SELECT i.type, i.sid $columns2 FROM {search_index} i $join $join2 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d"; + return array( + 'query' => "$select $sort_parameters", + 'count query' => "SELECT COUNT(*) FROM ($select) n1", + 'arguments' => array_merge($arguments2, $arguments1, array($query[4])), + ); +} - // Do actual search query - $result = pager_query("$select $sort_parameters", 10, 0, $count_select, $arguments); +/** + * Do a query with paging. + * + * @param $query + * The SQL query string. + * @param $count_query + * A SQL query for counting the number of results. + * @param $arguments + * An array containing the arguments needed by the query and the count query. + * @return + * An array of SIDs for the results. + * + * @see search_build_query() + * @ingroup search + */ +function do_search($query, $count_query, $arguments) { + $result = pager_query($query, 10, 0, $count_query, $arguments); $results = array(); while ($item = db_fetch_object($result)) { $results[] = $item; Index: modules/search/search.test =================================================================== RCS file: /cvs/drupal/drupal/modules/search/search.test,v retrieving revision 1.1 diff -u -F^function -r1.1 search.test --- modules/search/search.test 20 Apr 2008 18:23:29 -0000 1.1 +++ modules/search/search.test 10 May 2008 21:58:19 -0000 @@ -115,7 +115,7 @@ 'xx "minim am veniam es" OR dolore' => array() ); foreach ($queries as $query => $results) { - $set = do_search($query, SEARCH_TYPE); + $set = call_user_func_array('do_search', search_build_query($query, SEARCH_TYPE)); $this->_testQueryMatching($query, $set, $results); $this->_testQueryScores($query, $set, $results); }