27 Mar
Posted by Ganesh H S , Bangalore, India as zend framework
Zend_Search_Lucene_Search_Query::highlightMatches() method allows the developer to highlight HTML document terms in the context of a search query.
In the previous article Zend Lucene Search - part3 - retrieving the indexed data i talked about retrieving the search results. When we search, highlighting the searched keyword in the search result is one of the important aspect which most search engines follow, in this article i will be writing about highlighting the search results retrieved using the zend lucene search.
<?phprequire_once ‘Zend/Search/Lucene.php’;$queryStr= "php";
$query = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
$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);
?>
This program is same as in the Zend Lucene Search - part3 - retrieving the indexed data only one thing differs is now i am calling highlightMatches for the search results returned.
Related articles:
Zend Lucene Search - part1 - creating index
Zend Lucene Search - part2 - Real time indexing
Zend Lucene Search - part3 - retrieving the indexed data
3 Responses
Alex
April 21st, 2008 at 11:21 am
1Thanks, excellent article Ganesh! I was getting rather frustrated with MATCH … AGAINST searches and didn’t feel like implementing a Google search in my website. Hopefully Lucene will help me get a proper search functionality for Freshville.nl. Thanks again!
Ganesh H S , Bangalore, India
April 21st, 2008 at 11:35 am
2Alex -
Its my pleasure and i am happy that my articles is a helpful for you. Zend Framework is very powerful, i love the way its developed. Zend lucene search is a wonderful class helps to build our own site search.
Alex
April 21st, 2008 at 4:10 pm
3Hmm… there’s one thing I can’t make sense of. I’ve add the ID of my posts table as a Text field (thus stored and indexed) named “post_id”. Yet when I try to find it using $index->find(’post_id:’ . $post_id), no matches are returned
Any idea what I could be doing wrong?
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Blogroll
Recent Comments