---
title: "Zend Search Lucene实现全文搜索收藏"
author: "ythzjk"
date: "2009-02-17T10:24:03Z"
tags: []
slug: "zend-search-lucene"
draft: false
lastmod: "2009-02-17T10:24:03Z"
id: 333958
url: "https://ythzjk.iteye.com/blog/333958"
---

[Zend Search Lucene实现全文搜索](http://blog.csdn.net/ideal_1983/archive/2008/01/18/2051635.aspx)[收藏]()

### [新一篇: MySQL的优化](http://blog.csdn.net/ideal_1983/archive/2008/01/18/2051668.aspx)  | [旧一篇: 缓存策略的改动](http://blog.csdn.net/ideal_1983/archive/2008/01/18/2051623.aspx)

&lt;noscript&gt;&lt;/noscript&gt;

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

- 具有排名功能的搜索——最符合要求的结果出现在最前面
- 许多强大的查询类型：短语查询、通配符查询、近似查询、范围查询等
- 搜索特定的字段，如标题、作者、内容，等等

Zend\_Search\_Lucene 来源于 Apache Lucene project。要了解关于 Lucene 的更多详情，请访问 [http://lucene.apache.org/java/docs/](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](http://www.phpeye.com/zf/zend.search.html#zend.search.overview)

好，下面请看我的例子。

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

**createindex.php**

&lt;?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-&gt;query($sql);  
while($row = $result-&gt;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-&gt;addField(Zend\_Search\_Lucene\_Field::UnIndexed(’url’, $url));  
  $doc-&gt;addField(Zend\_Search\_Lucene\_Field::Text(’title’, $title));  
  $doc-&gt;addField(Zend\_Search\_Lucene\_Field::Text(’contents’, $description));  
  $index-&gt;addDocument($doc); //将这个文档加到索引中  
}  
// Write changes to the index.  
$index-&gt;commit();//提交，保存索引资料  
?&gt;

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

**search.php**

&lt;?php  
require\_once ‘../includes/application\_top.php’;  
require\_once DIR\_FS\_CATALOG . ‘includes/Zend/Search/Lucene.php’;  
$index = new Zend\_Search\_Lucene(’index’);   
$str = &lt;&lt;&lt; EOT  
&lt;form method=get action=”"&gt;  
&lt;input type=”text” name=”keywords”&gt;&lt;input type=”submit”&gt;  
&lt;/form&gt;  
EOT;

echo $str;  
$keywords = strtolower($\_GET\[’keywords’]);  
if(! empty($keywords)) {  
 $hits = $index-&gt;find($keywords);  
    echo ‘&lt;br&gt;Search result:&lt;br&gt;’;  
    foreach ($hits as $hit) {  
        echo ‘&lt;a href=”‘ . $hit-&gt;url . ‘”&gt;&lt;strong&gt;’ . $hit-&gt;title . ‘&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;’;  
        echo $hit-&gt;contents . ‘&lt;hr&gt;’;  
    }  
}  
?&gt;

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