网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 论文素材 设计下载 最新论文 下载排行 论文上传 在线投稿 联系我们
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > PHP > 正文
ZendFramework中使用Lucene全文检索
来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/12/07
下载{$ArticleTitle}原创论文样式

  代码参考自ZF自带的demo。

  如下图所示建立工程:

ZendFramework中使用Lucene全文检索

  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";
      }
    }
  }
}

(责任编辑:admin)

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号