17 Mar
Posted by Ganesh H S , Bangalore, India as zend framework
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);
Related articles:
Zend Lucene Search - part1 - creating index
Zend Lucene Search - part2 - Real time indexing
Zend Lucene Search - part4 - Search Results Highlighting
Home Page
2 Responses
G
September 15th, 2008 at 4:26 am
1Did you try to create index directly in Java? Would be intresting to show how to do that.
Ganesh
September 16th, 2008 at 2:14 am
2This is done using php zend framework, i haven’t used apache lucene in java.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Blogroll
Recent Comments