我想创建一个搜索框,而不是在查询中使用WHERE句来匹配字段中的确切短语,而是希望搜索框能够执行语义搜索,以便当用户输入部分短语时,它会在能够匹配词组含义的地方显示合适的结果。尝试通过Google搜索时Google的工作原理。这有可能实现吗,有人有什么想法吗?
以下是我目前正在使用的代码,它能够使用数据库查看行是否与在搜索框中输入的确切短语匹配:
<?php
//connected to DB
foreach (array('questioncontent') as $varname) {
$questioncontent = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
?>
<p>Search for a previous question by entering in a phrase in the search box below and submitting the phrase</p>
<form action="previousquestions.php" method="post" id="modalform">
<p>Search: <input type="text" name="questioncontent" value="<?php echo $questioncontent; ?>" /></p>
<p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
</form>
<?php
if (isset($_POST['searchQuestion'])) {
$questionquery = "SELECT QuestionContent FROM Question
WHERE(QuestionContent = '".mysql_real_escape_string($questioncontent)."')";
$questionnum = mysql_num_rows($questionresult = mysql_query($questionquery));
if($questionnum !=0){
$output = "";
while ($questionrow = mysql_fetch_array($questionresult)) {
$output .= "
<table>
<tr>
<td>{$questionrow['QuestionContent']}</td>
</tr>";
}
$output .= " </table>";
echo $output;
}
}
最佳答案
如果您正在寻找PHP搜索引擎教程,请查看此YouTube video-它可能会有所帮助。
关于php - 如何创建语义搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10742561/