我的任务是创建一个通过数据库查找内容的查询,即使它在搜索时被拼错,所以我使用strcmp来比较查询中的soundex。我当前的代码有问题。如果我搜索AZUS,我的意思是华硕,并且有一个华硕的标题,它不会出现。那是因为在post_entry
ASUS中<a href="">ASUS </a>
所以我必须在我的代码中soundex("%'.$arraytagsd.'")
取出%
来执行此操作,但每个选项%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);
?>
答案 0 :(得分:0)
你应该考虑使用solr,但如果你想在mysql中使用它,这就是我要做的。