In the previous article Zend Lucene Search – part4 – Search Results Highlighting i talked about highlighting the keywords in search results.

In this article i will be writing about highlighting the keywords in search results and formating the output display format much similar to most search engines result page using the zend lucene search.

<?php
require_once ‘Zend/Search/Lucene.php’;

$queryStr= “php”;

$snapshotTextLength = 155;

$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;

displayResults($results, $snapshotTextLength);

}

// Format and display the search results

function displaySearchResults(&$results, $snapshotTextLength)

{

if(is_array($results) && count($results))

{

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++;

// title of each article with URL as link

$searchResultsContent .= sprintf(“%“, $data[$count][”article_url”], $data[$count][”article_title”]);

// snapshot of the description

$searchResultsContent .= sprintf(”%s”, substr($data[$count][”article_description”], 0, $snapshotTextLength));

// url

$searchResultsContent .= $data[$count][”article_url”];

// leave 2 lines after each search results

$searchResultsContent .= “<br> <br> <br>”;

}

}

else

{

$searchResultsContent = “No results found, try using different keywords”;

}

return $searchResultsContent;

}

?>

This program is similar to Zend Lucene Search – part3 – retrieving the indexed data , the only difference is i am formating the display format, the output of this program displays the output much similar to what you get in the search engine result page of google.com or search.yahoo.com