ZendFramework中使用Lucene全文检索

来源:互联网 发布:手机文件查看软件 编辑:程序博客网 时间:2024/05/21 10:49

代码参考自ZF自带的demo。

如下图所示建立工程:

library下建立lucene的模块。

FileDocument.php:

  1. <?php
  2. require_once 'Zend/Search/Lucene.php';
  3. class FileDocument extends Zend_Search_Lucene_Document
  4. {
  5.     public function __construct($fileName$storeContent = false)
  6.     {
  7.         if (!file_exists($fileName)) {
  8.             throw new Zend_Search_Lucene_Exception("File doesn't exists. Filename: '$fileName'");
  9.         }
  10.         $this->addField(Zend_Search_Lucene_Field::Text('path'$fileName));
  11.         $this->addField(Zend_Search_Lucene_Field::Keyword( 'modified'filemtime($fileName) ));
  12.         $f = fopen($fileName,'rb');
  13.         $byteCount = filesize($fileName);
  14.         $data = '';
  15.         while ( $byteCount > 0 && ($nextBlock = fread($f$byteCount)) != false ) {
  16.             $data .= $nextBlock;
  17.             $byteCount -= strlen($nextBlock);
  18.         }
  19.         fclose($f);
  20.         if ($storeContent) {
  21.             $this->addField(Zend_Search_Lucene_Field::Text('contents'$data'ISO8859-1'));
  22.         } else {
  23.             $this->addField(Zend_Search_Lucene_Field::UnStored('contents'$data'ISO8859-1'));
  24.         }
  25.     }
  26. }
  27. ?>

测试代码如下:

  1. <?php
  2. require_once 'Zend/Controller/Action.php';
  3. require_once 'Lucene/FileDocument.php';
  4. class IndexController extends Zend_Controller_Action 
  5. {
  6.     public function init()
  7.     {
  8.         $index = new Zend_Search_Lucene('index', true);
  9.         setlocale(LC_CTYPE, 'en_US');
  10.         $indexSourceDir = 'C:/IndexSource';
  11.         $dir = opendir($indexSourceDir);
  12.         while (($file = readdir($dir)) !== false) {
  13.             if (is_dir($indexSourceDir . '/' . $file)) {
  14.                 continue;
  15.             }
  16.             if (strcasecmp(substr($filestrlen($file)-5), '.html') != 0) {
  17.                 continue;
  18.             }
  19.         
  20.             // Create new Document from a file
  21.             $doc = new FileDocument($indexSourceDir . '/' . $file, true);
  22.             // Add document to the index
  23.             $index->addDocument($doc);
  24.         
  25.             echo $file . ".../n";
  26.             flush();
  27.         }
  28.         closedir($dir);
  29.     }
  30.     
  31.     public function indexAction() 
  32.     {
  33.         $index = new Zend_Search_Lucene('index');
  34.         echo "Index contains {$index->count()} documents./n";
  35.         
  36.         $search = 'explanations';
  37.         $hits   = $index->find(strtolower($search));
  38.         echo "Search for /"$search/" returned " .count($hits). " hits./n/n";
  39.         
  40.         foreach ($hits as $hit) {
  41.             echo str_repeat('-', 80) . "/n";
  42.             echo 'ID:    ' . $hit->id                     ."/n";
  43.             echo 'Score: ' . sprintf('%.2f'$hit->score) ."/n/n";
  44.         
  45.             foreach ($hit->getDocument()->getFieldNames() as $field) {
  46.                 echo "$field: /n";
  47.                 echo '    ' . trim(substr($hit->$field,0,76)) . "/n";
  48.             }
  49.         }
  50.     }
  51. }

更多资料:http://www.jcan.19dog.com/blog/article/item/7be5528f1f49b050.html

原创粉丝点击