ZendFramework中使用Lucene全文检索
来源:互联网 发布:手机文件查看软件 编辑:程序博客网 时间:2024/05/21 10:49
代码参考自ZF自带的demo。
如下图所示建立工程:
library下建立lucene的模块。
FileDocument.php:
- <?php
- require_once 'Zend/Search/Lucene.php';
- class FileDocument extends Zend_Search_Lucene_Document
- {
- public function __construct($fileName, $storeContent = false)
- {
- if (!file_exists($fileName)) {
- throw new Zend_Search_Lucene_Exception("File doesn't exists. Filename: '$fileName'");
- }
- $this->addField(Zend_Search_Lucene_Field::Text('path', $fileName));
- $this->addField(Zend_Search_Lucene_Field::Keyword( 'modified', filemtime($fileName) ));
- $f = fopen($fileName,'rb');
- $byteCount = filesize($fileName);
- $data = '';
- while ( $byteCount > 0 && ($nextBlock = fread($f, $byteCount)) != false ) {
- $data .= $nextBlock;
- $byteCount -= strlen($nextBlock);
- }
- fclose($f);
- if ($storeContent) {
- $this->addField(Zend_Search_Lucene_Field::Text('contents', $data, 'ISO8859-1'));
- } else {
- $this->addField(Zend_Search_Lucene_Field::UnStored('contents', $data, 'ISO8859-1'));
- }
- }
- }
- ?>
测试代码如下:
- <?php
- require_once 'Zend/Controller/Action.php';
- require_once 'Lucene/FileDocument.php';
- class IndexController extends Zend_Controller_Action
- {
- public function init()
- {
- $index = new Zend_Search_Lucene('index', true);
- setlocale(LC_CTYPE, 'en_US');
- $indexSourceDir = 'C:/IndexSource';
- $dir = opendir($indexSourceDir);
- while (($file = readdir($dir)) !== false) {
- if (is_dir($indexSourceDir . '/' . $file)) {
- continue;
- }
- if (strcasecmp(substr($file, strlen($file)-5), '.html') != 0) {
- continue;
- }
- // Create new Document from a file
- $doc = new FileDocument($indexSourceDir . '/' . $file, true);
- // Add document to the index
- $index->addDocument($doc);
- echo $file . ".../n";
- flush();
- }
- closedir($dir);
- }
- public function indexAction()
- {
- $index = new Zend_Search_Lucene('index');
- echo "Index contains {$index->count()} documents./n";
- $search = 'explanations';
- $hits = $index->find(strtolower($search));
- echo "Search for /"$search/" returned " .count($hits). " hits./n/n";
- foreach ($hits as $hit) {
- echo str_repeat('-', 80) . "/n";
- echo 'ID: ' . $hit->id ."/n";
- echo 'Score: ' . sprintf('%.2f', $hit->score) ."/n/n";
- foreach ($hit->getDocument()->getFieldNames() as $field) {
- echo "$field: /n";
- echo ' ' . trim(substr($hit->$field,0,76)) . "/n";
- }
- }
- }
- }
更多资料:http://www.jcan.19dog.com/blog/article/item/7be5528f1f49b050.html
- ZendFramework中使用Lucene全文检索
- 全文检索Lucene的使用
- 使用lucene实现全文检索
- 全文检索Lucene使用与优化
- 全文检索技术与Lucene的使用
- 使用Lucene.Net实现全文检索
- 使用Lucene.Net实现全文检索
- 使用Lucene.Net实现全文检索
- 使用Lucene.Net实现全文检索
- 使用lucene实现简单的全文检索
- Lucene 全文检索实践
- lucene 全文检索简介
- lucene 全文检索简介
- Lucene 全文检索
- 全文检索Lucene说明书
- 全文检索引擎lucene
- 全文检索LUCENE
- Lucene全文检索1
- .Net部署二三事之一——如何为VS安装文件MSI制作更新补丁MSP
- 关于ajax toolkit中的下拉菜单CascadingDropDown 的method error 500问题
- 魔方图最终版本奇数偶数均已实现
- 根据前序遍历-中序遍历结果 来还原一颗二叉树
- java计算器源代码
- ZendFramework中使用Lucene全文检索
- B/S和C/S模式比较
- SAP Query Reporting
- Prioritizing Web Usability
- Solving the PowerPoint Predicament: Using Digital Media for Effective Communication
- neogeo模拟街机游戏
- Security Threat Mitigation and Response: Understanding Cisco Security MARS
- 关于 去掉重复的面试题 涉及rowid 和id 的性能比较 外表字段和子查询字段的连接
- Digital Photography All-in-One Desk Reference For Dummies