пятница, 1 февраля 2008 г.

Drupal Search without temporaty tables

Many drupal lovers getting trouble at godaddy for search module.
when user try to search they will get error message as not able to create temporary tables.
Here is the code snippets for search hack:
File: search.module
Function name: do_search
Replace the function with the following code. Please backup your copy before applying the code.
-----------------------------------------
function do_search($keywords, $type, $join1 = '', $where1 = '1', $arguments1 = array(), $select2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') {
$query = search_parse_query($keywords);

if ($query[2] == '') {
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))));
}
if ($query === NULL || $query[0] == '' || $query[2] == '') {
return array();
}

// First pass: select all possible matching sids, doing a simple index-based OR matching on the keywords.
// 'matches' is used to reject those items that cannot possibly match the query.
$conditions = $where1 .' AND ('. $query[2] .") AND i.type = '%s'";
$arguments = array_merge($arguments1, $query[3], array($type, $query[4]));

//----G // Temporary table not allowed in godaddy hosting.

//$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');

$result = db_query("CREATE TABLE IF NOT EXISTS temp_search_sids 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);

// Calculate maximum relevance, to normalize it
$normalize = db_result(db_query('SELECT MAX(relevance) FROM temp_search_sids'));
if (!$normalize) {
return array();
}
$select2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * i.relevance)', $select2);

// Second pass: only keep items that match the complicated keywords conditions (phrase search, negative keywords, ...)
$conditions = '('. $query[0] .')';
$arguments = array_merge($arguments2, $query[1]);
//----G // Temporary table not allowed in godaddy hosting.
//$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');

$result = db_query("CREATE TABLE IF NOT EXISTS temp_search_results 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);

if (($count = db_result(db_query('SELECT COUNT(*) FROM temp_search_results'))) == 0) {
return array();
}
$count_query = "SELECT $count";

// Do actual search query
$result = pager_query("SELECT * FROM temp_search_results", 10, 0, $count_query);
$results = array();
while ($item = db_fetch_object($result)) {
$results[] = $item;
}
//----G // Temporary table not allowed in godaddy hosting.
db_query('DELETE LOW_PRIORITY FROM temp_search_sids');
db_query('DELETE LOW_PRIORITY FROM temp_search_results');
return $results;
}
-----------------------------------

Search Table Structure:

CREATE TABLE `temp_search_results` (
`type` varchar(16) default NULL,
`sid` int(10) unsigned NOT NULL default '0',
`score` double default NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `temp_search_sids` (
`type` varchar(16) default NULL,
`sid` int(10) unsigned NOT NULL default '0',
`relevance` double default NULL,
`matches` bigint(21) NOT NULL default '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


-----------------------------------
It is better to create the search tables in advace before hacking the code.

source

Комментариев нет: