Once the index is created, we are ready use zend lucene search to search the website. In the following example, php is the search keyword used to fetch the relevant search results in the already indexed data.
<?php
require_once ‘Zend/Search/Lucene.php’;$query = "php";
$index = Zend_Search_Lucene::open("/var/www/lucene-data/blog-index");
$results = $index->find($query);
echo "Index contains ".$index->count()." documents.\n\n";
if($index->count())
{
$count = 0;
foreach ($results as $result)
{
$data[$count]["article_url"] = $result->url;
$data[$count]["article_title"] = $query->highlightMatches($result->title);
$data[$count]["article_description"] = $query->highlightMatches($result->contents);
$data[$count]["article_created_date_time"] = $result->postedDateTime;
$data[$count]["article_id"] = $result->articleId;
$count++;
}
}
print_R($data);
?>
To retrieve the index data, first thing we need to do is to open the indexed path.
$index = Zend_Search_Lucene::open("/var/www/lucene-data/blog-index");
Suppose if user search input is -
$query = "php";
We have to use the find method of zend search lucene -
$results = $index->find($query);
To retrieve the total records resulted in the search result, we have to use count method of zend lucene search -
echo "Index contains ".$index->count()." documents.\n\n";
To limit the search result count we have to use setResultSetLimit of zend lucene search -
$index->setResultSetLimit(10);
RSS feed for comments on this post · TrackBack URI
Leave a reply