我的任务是进行查询,即使在搜索时拼写错误,也可以通过数据库查找内容,因此我使用 strcmp 来比较查询中的 soundex。我当前的代码有问题。如果我搜索 AZUS,我的意思是 ASUS,并且有一个带有 ASUS 的标题,它不会出现。那是因为在post_entry
华硕中,<a href="">ASUS </a>
所以我必须soundex("%'.$arraytagsd.'")
在下面的代码中取出 a%
来执行此操作,但是对于每个选项 %s% 或 %s 或 s% 或 s 都会使我的脚本变慢。
无论如何,仍然使用 soundex 可以更快地做到这一点吗?
我创建了这个函数来获取搜索输入的每个单词并将其与数据库进行比较
$colname_getPosts = $_GET['post_title'];
mysql_select_db($database_Main);
function test($string) {
$output = '';
$arrays = explode(' ', $string);
foreach ($arrays as $arraytagsd) {
$output .= ' OR strcmp(soundex(post_title), soundex("%'.$arraytagsd.'%")) = 0';
$output .= ' OR strcmp(soundex(post_entry), soundex("%'.$arraytagsd.'%")) = 0';
$output .= ' OR strcmp(soundex(tags), soundex("%'.$arraytagsd.'%")) = 0';
$output .= ' OR strcmp(soundex(post_author), soundex("%'.$arraytagsd.'%")) = 0';
}
return $output;
}
我在按相关性排序的查询中使用它
$query_getPosts = "
SELECT post_id, post_date, post_title, post_author, post_entry, tags, sum(relevance)
FROM (
SELECT post_id, post_date, post_title, post_author, post_entry, tags, 10 AS relevance FROM blog_posts WHERE MATCH(post_title) AGAINST('%$colname_getPosts%' IN BOOLEAN MODE)
UNION
SELECT post_id, post_date, post_title, post_author, post_entry, tags, 2 AS relevance FROM blog_posts WHERE MATCH(post_author) AGAINST('%$colname_getPosts%' IN BOOLEAN MODE)".test($colname_getPosts)."
UNION
SELECT post_id, post_date, post_title, post_author, post_entry, tags, 5 AS relevance FROM blog_posts WHERE MATCH(post_entry) AGAINST('%$colname_getPosts%' IN BOOLEAN MODE)
UNION
SELECT post_id, post_date, post_title, post_author, post_entry, tags, 7 AS relevance FROM blog_posts WHERE MATCH(tags) AGAINST('%$colname_getPosts%' IN BOOLEAN MODE))results
GROUP BY post_id, post_date, post_title, post_author, post_entry, tags ORDER BY relevance DESC";
$getPosts = mysql_query($query_getPosts) or die(mysql_error());
$row_getPosts = mysql_fetch_assoc($getPosts);
$totalRows_getPosts = mysql_num_rows($getPosts);
?>