Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > 网页制作 > Zend Search Lucene实现全文搜索
【标  题】:Zend Search Lucene实现全文搜索
【关键字】:Zend,Search,Lucene
【来  源】:http://blog.csdn.net/jxyuhua/archive/2006/09/18/1238413.aspx

Zend Search Lucene实现全文搜索

Your Ad Here  简介:

Zend_Search_Lucene 是一个完全由 PHP 5 编写的通用文本搜索引擎。由于其将索引保存在文件系统中而不需要数据库支持,因此它几乎可以为任何由 PHP 驱动的网站增加搜索能力。Zend_Search_Lucene 支持下列特性:

  • 具有排名功能的搜索——最符合要求的结果出现在最前面

  • 许多强大的查询类型:短语查询、通配符查询、近似查询、范围查询等 

  • 搜索特定的字段,如标题、作者、内容,等等

Zend_Search_Lucene 来源于 Apache Lucene project。要了解关于 Lucene 的更多详情,请访问 http://lucene.apache.org/java/docs/

看了N久,查了许多的文章和例子之后,终于成功运行了,特记录下来,与大家共享。

首先需下载Zend Framework,下载地址:http://framework.zend.com
我这里用的是Preview 0.1.5版.
具体的使用方法请看官方文档,中文文档在这里:http://www.phpeye.com/zf/zend.search.html#zend.search.overview

好,下面请看我的例子。

1、建立索引
你可以对静态页面文件(如新闻网站等)进行索引,也可以对数据库的内容进行索引,总之,一切的数据都
索引。我这里以mysql数据库为例。

createindex.php

<?php
require_once '../includes/application_top.php';
require_once DIR_FS_CATALOG . 'includes/Zend/Search/Lucene.php';
if (function_exists("set_time_limit") && ! get_cfg_var('safe_mode')) {
  set_time_limit(0);
}
$index = new Zend_Search_Lucene('index', true);//建立索引对象,TRUE表示一个新的索引
$sql = "SELECT c.categories_name, m.models_id, m.models_name, m.models_series, " .
         "m.models_brand, p.products_id, p.products_title..."; //查询数据库产品资料
$result = $class_db->query($sql);
while($row = $result->fetchRow()) {
  $url = 'http://www.sellcamera.net/detail.php/' . $row['products_id']; //产品链接
  $title = $row['products_title'];//产品标题
  $description = $models_brand . ' ' . $models_name . ' ' . $categories_name; //产品的描述,自己组合它的内容
 // Store document URL to identify it in search result.
  $doc = new Zend_Search_Lucene_Document();//建立一个索引文档
  $doc->addField(Zend_Search_Lucene_Field::UnIndexed('url', $url));
  $doc->addField(Zend_Search_Lucene_Field::Text('title', $title));
  $doc->addField(Zend_Search_Lucene_Field::Text('contents', $description));
  $index->addDocument($doc); //将这个文档加到索引中
}
// Write changes to the index.
$index->commit();//提交,保存索引资料
?>

好的,再运行它,就将网站的所有产品资料的索引保存到指定的目录中了。接下来,我们要做的就是将它们
查找出来。

search.php

<?php
require_once '../includes/application_top.php';
require_once DIR_FS_CATALOG . 'includes/Zend/Search/Lucene.php';
$index = new Zend_Search_Lucene('index'); 
$str = <<< EOT
<form method=get action="">
<input type="text" name="keywords"><input type="submit">
</form>
EOT;

echo $str;
$keywords = strtolower($_GET['keywords']);
if(! empty($keywords)) {
 $hits = $index->find($keywords);
    echo '<br>Search result:<br>';
    foreach ($hits as $hit) {
        echo '<a href="' . $hit->url . '"><strong>' . $hit->title . '</strong></a><br>';
        echo $hit->contents . '<hr>';
    }
}
?>

OK,大功告成,赶快试试吧。

asp.net运行时,动态添加Button(或其它控件),并处理相应的事件:【上一篇】
多框架 跨页面调用jsp过程,实现功能导航树的隐藏:【下一篇】
【相关文章】
  • Lucene开源检索架构预览
  • 在 Web 应用程序中集成 Lucene
  • Lucene深入
  • Lucene的学习
  • 用 Lucene 加速 Web 搜索应用程序的开发
  • 关于lucene2.0的创建、检索和删除功能的完整实现
  • Lucene 一个比较准确和高效的分词算法
  • Linux AS4 Zend Optimizer 无法运行的原因。。。。
  • 实战部署基于 Linux 平台的 WEB 服务器(MySQL+Apache+GD+PHP+Zend+
  • ShootSearch 中文分词组件(c#开源) 1.0 bate 060830
  • 【随机文章】
  • 用Shutdown实现定时关机
  • 漫谈IBM pSeries的逻辑分区和动态逻辑分区(二)
  • XML Schema指南(一)之概述
  • 加速家用DSL终端标准化
  • 三谈关于%5c暴库
  • opensll
  • 用Socket发邮件的代码(可以群发)
  • 《葵花宝典》第一式——分离并包装变化的部分(Seperating & Encapsulating)
  • 病毒名称 达比(Worm.Darby.o)
  • Win XP系统中网桥的配置方法
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.