2012-05-24 40 views
0

我想创建一个搜索框,而不是使用查询中的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; 

     } 

    } 
+2

做一个'全文搜索'的网络搜索 - 有几个这样的系统可用于PHP或MySQL。你不希望搜索“Google的工作原理” - 这是[相当复杂](https://en.wikipedia.org/wiki/Google_search_technology)! – halfer

回答

1

如果你正在寻找一个PHP的搜索引擎教程,采取看看这个YouTube video - 它可能有帮助。

+0

我会看看:) – user1394925

+0

这个优秀的指南是非常有帮助的,帮助我alote。 – Blanktext