Brief introduction:
Zend_search_lucene is a general-purpose text search engine written entirely by PHP 5. Because it keeps the index in the file system without the need for database support, it can almost add search power to any PHP-driven Web site. Zend_search_lucene supports the following features:
Search with ranking function-the most suitable results appear at the front
Many powerful query types: Phrase query, wildcard query, approximate query, range query, etc.
Search for specific fields, such as title, author, content, etc.
Zend_search_lucene from Apache Lucene project. To learn more about Lucene, please visit http://lucene.apache.org/java/docs/.
Look at the n long, check a lot of articles and examples, finally successfully run, the special record down, and share with you.
First download the Zend Framework, download the address: http://framework.zend.com
I'm using the preview 0.1.5 version here.
The specific use of the method please see the official documents, Chinese documents here: Http://www.phpeye.com/zf/zend.search.html#zend.search.overview
OK, let me take a look at my example below.
1, the establishment of the index
You can index static page files (such as news websites), or you can index the contents of the database, in short, all the data
Index. Here I take the MySQL database as an example.
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);//Set Index object, True to indicate a new index
$sql = "Select C.categories_name, m.models_id, M.models_name, M.models_series,".
"M.models_brand, p.products_id, p.products_title ..."; Query Database product Information
$result = $class _db->query ($sql);
while ($row = $result->fetchrow ()) {
$url = ' http://www.sellcamera.net/detail.php/'. $row [' products_id ']; Product Links
$title = $row [' products_title '];//product title
$description = $models _brand. ' ' . $models _name. ' ' . $categories _name; The description of the product, the content of its own composition
Store document URL to identify it in search result.
$doc = new Zend_search_lucene_document ();//Create an index 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); Add this document to the index
}
Write changes to the index.
$index->commit ()//Submit, save index data
?>
OK, then run it, and save the index of all product data for the site to the specified directory. Next, all we have to do is put them
Find out.
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. ' }
}
?>
OK, it's done, try it quickly.