FlyakiteOSX WINDOWS模擬OSX css-dock-menu 超漂亮的仿MAC的菜單
Oct 16

PHP 中文全文搜尋 FullText Search 連中文分詞 (只需PHP就能使用Lucene)

前兩日發表過一份關於 ZEND FRAMEWORK 既SEARCH ENGINE 既文章
E篇係加強版.

這一次最大的分别是支援了中文分詞。上一個版本只支援英文。而這個版本支援了中文全文搜尋及中文分詞功能已把PROGRAM 簡化 改得容易明白。

按此下載原始檔 (SOURCE CODE)

先說明資料夾內容

│ config.php 資料庫連接設定
│ index_file.php 製作索引的檔案
│ search.php 主程式,搜尋程式。
└─Zend <–主頁的CLASS LIBRARY

Config.php

  1. <?mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
  2.  
  3. mysql_select_db(DB_NAME);
  4.  
  5. //DB_HOSTNAME : 資料庫位置E.G localhost
  6.  
  7. // DB_USERNAME : 資料庫用戶名稱
  8.  
  9. // DB_PASSWORD : 資料庫用戶密碼
  10.  
  11. // DB_NAME : 使用的資料庫名稱
  12.  
  13. ?>

index_file.php

  1. <?phpinclude_once("config.php");
  2.  
  3. error_reporting(E_ALL|E_STRICT);set_include_path(‘.’ . PATH_SEPARATOR . ‘./Zend/’);
  4.  
  5. include "Zend/Loader.php";
  6.  
  7. Zend_Loader::loadClass(‘Zend_Search_Lucene’);
  8.  
  9. Zend_Loader::loadClass(‘Zend_Search_Lucene_Document’);
  10.  
  11. Zend_Loader::loadClass(‘Zend_Search_Lucene_Analysis_Analyzer_Common_Phpbean’);
  12.  
  13. Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Phpbean());
  14.  
  15. if (function_exists("set_time_limit") &amp;&amp; ! get_cfg_var(’safe_mode’)) {
  16.  
  17.  
  18. }
  19.  
  20. $index = new Zend_Search_Lucene(‘index’, true);
  21.  
  22. $sql = "
  23.  
  24. SELECT `id` , `title` , `description`
  25.  
  26. FROM `data_table` "; //拿取需要索引的資料
  27.  
  28. mysql_query("set names ‘utf8′");
  29.  
  30. $result = mysql_query($sql);
  31.  
  32. while($row = mysql_fetch_assoc($result)) {
  33.  
  34. $url = ‘http://www.vincent.idv.hk/’ . $row[‘d’]; //網頁位置
  35.  
  36. $title = $row[‘title’];//主題
  37.  
  38. $description = $row["description"]; //描述
  39.  
  40. //儲存網頁的位置以在搜尋結果中連結.
  41.  
  42. $doc = new Zend_Search_Lucene_Document();//建立新的索引文件
  43.  
  44. $doc->addField(Zend_Search_Lucene_Field::UnIndexed(‘url’, $url));
  45.  
  46. $doc->addField(Zend_Search_Lucene_Field::Text(‘title’, $title,‘utf-8′));
  47.  
  48. $doc->addField(Zend_Search_Lucene_Field::Text(‘contents’, $description,
  49.  
  50. ‘utf-8′));
  51.  
  52. $index->addDocument($doc); //把索引文件加到索引中
  53.  
  54. }
  55.  
  56. $index->commit();//提交,及保存索引

search.php

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2.  
  3. <?php
  4.  
  5. include_once("config.php");
  6.  
  7. error_reporting(E_ALL|E_STRICT);
  8.  
  9. set_include_path(‘.’ . PATH_SEPARATOR . ‘./Zend/’);
  10.  
  11. include "Zend/Loader.php";
  12.  
  13. Zend_Loader::loadClass(‘Zend_Search_Lucene’);
  14.  
  15. Zend_Loader::loadClass(‘Zend_Search_Lucene_Exception’);
  16.  
  17. Zend_Loader::loadClass(‘Zend_Search_Lucene_Document’);
  18.  
  19. Zend_Loader::loadClass(‘Zend_Search_Lucene_Analysis_Analyzer_Common_Phpbean’);
  20.  
  21. Zend_Loader::loadClass(‘Zend_Search_Lucene_Search_QueryParser’);
  22.  
  23. Zend_Search_Lucene_Analysis_Analyzer::setDefault(
  24.  
  25.     new Zend_Search_Lucene_Analysis_Analyzer_Common_Phpbean());$index = new Zend_Search_Lucene(‘index’);
  26.  
  27.   $query = isset($_GET[‘query’]) ? $_GET[‘query’] : ;
  28.  
  29.     $query = trim($query);
  30.  
  31.  if(strlen($query)>0){
  32.  
  33.                try {
  34.  
  35.                               $query2 = Zend_Search_Lucene_Search_QueryParser::parse($query, "utf-8");
  36.  
  37. $hits = $index->find($query2);
  38.  
  39.      }
  40.  
  41.      catch (Zend_Search_Lucene_Exception $ex) {
  42.  
  43.          $hits = array();
  44.  
  45.      }
  46.  
  47.     $numHits = count($hits);
  48.  
  49.  }
  50.  
  51. ?>
  52.  
  53. <form method="get" action="search.php">
  54.  
  55.     <input type="text" name="query" value="<?= htmlSpecialChars($query) ?>" />
  56.  
  57.     <input type="submit" value="Search" />
  58.  
  59. </form>
  60.  
  61. <?php if (strlen($query) > 0) { ?>
  62.  
  63.     <p>
  64.  
  65.         Found <?= $numHits ?> result(s) for query <?= $query ?>.
  66.  
  67.     </p>
  68.  
  69. <?php foreach ($hits as $hit) { ?>
  70.  
  71.         <h3><?= $hit->title ?> <?= $hit->score ?></h3>
  72.  
  73.         <p>
  74.  
  75.             <?= $hit->contents ?><br />
  76.  
  77.             <a href="<?= $hit->url ?>">Read more…</a>
  78.  
  79.         </p>
  80.  
  81.     <?php } ?>
  82.  
  83. <?php } ?>

解壓縮密碼為 : http://www.vincent.idv.hk


Written by 傻仔仔

光波24書網(http://www.24reader.com/) - 免費電子書

  

光波24書網(http://www.24reader.com/) - 新到電子書

  

光波24書網(http://www.24reader.com/) - 快將推出電子書




發表一條評論