ZendFramework中使用Lucene全文检索
时间:2009-06-24
来源:互联网
代码参考自ZF自带的demo。
如下图所示建立工程:
=700) window.open('http://images.phpfans.net/attach/images/20090624_81a521d62e141035cbccrZxjSa0nuPpw.jpg');" onload="if(this.offsetWidth>'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" >
library下建立lucene的模块。
FileDocument.php:
<?php
require_once'Zend/Search/Lucene.php';
classFileDocumentextendsZend_Search_Lucene_Document
{
publicfunction__construct($fileName,$storeContent=false)
{
if(!file_exists($fileName)){
thrownewZend_Search_Lucene_Exception(\"Filedoesn'texists.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';
classIndexControllerextendsZend_Controller_Action
{
publicfunctioninit()
{
$index=newZend_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;
}
//CreatenewDocumentfromafile
$doc=newFileDocument($indexSourceDir.'/'.$file,true);
//Adddocumenttotheindex
$index->addDocument($doc);
echo$file.\"...n\";
flush();
}
closedir($dir);
}
publicfunctionindexAction()
{
$index=newZend_Search_Lucene('index');
echo\"Indexcontains{$index->count()}documents.n\";
$search='explanations';
$hits =$index->find(strtolower($search));
echo\"Searchfor\"$search\"returned\".count($hits).\"hits.nn\";
foreach($hitsas$hit){
echostr_repeat('-',80).\"n\";
echo'ID: '.$hit->id .\"n\";
echo'Score:'.sprintf('%.2f',$hit->score).\"nn\";
foreach($hit->getDocument()->getFieldNames()as$field){
echo\"$field:n\";
echo' '.trim(substr($hit->$field,0,76)).\"n\";
}
}
}
}
如下图所示建立工程:
=700) window.open('http://images.phpfans.net/attach/images/20090624_81a521d62e141035cbccrZxjSa0nuPpw.jpg');" onload="if(this.offsetWidth>'700')this.width='700';if(this.offsetHeight>'700')this.height='700';" >
library下建立lucene的模块。
FileDocument.php:
<?php
require_once'Zend/Search/Lucene.php';
classFileDocumentextendsZend_Search_Lucene_Document
{
publicfunction__construct($fileName,$storeContent=false)
{
if(!file_exists($fileName)){
thrownewZend_Search_Lucene_Exception(\"Filedoesn'texists.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';
classIndexControllerextendsZend_Controller_Action
{
publicfunctioninit()
{
$index=newZend_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;
}
//CreatenewDocumentfromafile
$doc=newFileDocument($indexSourceDir.'/'.$file,true);
//Adddocumenttotheindex
$index->addDocument($doc);
echo$file.\"...n\";
flush();
}
closedir($dir);
}
publicfunctionindexAction()
{
$index=newZend_Search_Lucene('index');
echo\"Indexcontains{$index->count()}documents.n\";
$search='explanations';
$hits =$index->find(strtolower($search));
echo\"Searchfor\"$search\"returned\".count($hits).\"hits.nn\";
foreach($hitsas$hit){
echostr_repeat('-',80).\"n\";
echo'ID: '.$hit->id .\"n\";
echo'Score:'.sprintf('%.2f',$hit->score).\"nn\";
foreach($hit->getDocument()->getFieldNames()as$field){
echo\"$field:n\";
echo' '.trim(substr($hit->$field,0,76)).\"n\";
}
}
}
}
作者: 淡水 发布时间: 2009-06-24
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28