Zend Lucene Search – part4 – Search Results Highlighting


From ganeshhs.com

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.

  1. <?phprequire_once ‘Zend/Search/Lucene.php’;$queryStr= “php”;
  2. $query = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
  3. $index = Zend_Search_Lucene::open(“/var/www/lucene-data/blog-index”);
  4. $results = $index->find($query);
  5. echo “Index contains “.$index->count().” documents.\n\n”;
  6. if($index->count())
  7. {
  8. $count = 0;
  9. foreach ($results as $result)
  10. {
  11. $data[$count][“article_url”]         = $result->url;
  12. $data[$count][“article_title”]        = $query->highlightMatches($result->title);
  13. $data[$count][“article_description”]        = $query->highlightMatches($result->contents);
  14. $data[$count][“article_created_date_time”]    = $result->postedDateTime;
  15. $data[$count][“article_id”]             = $result->articleId;
  16. $count++;
  17. }
  18. }
  19. print_R($data);
  20. ?>

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s