17 Mar
Posted by Ganesh H S , Bangalore, India as zend framework
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.
<?phprequire_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
Related articles:
Zend Lucene Search - part1 - creating index
Zend Lucene Search - part2 - Real time indexing
Zend Lucene Search - part3 - retrieving the indexed data
Zend Lucene Search - part4 - Search Results Highlighting
3 Responses
zaw
March 25th, 2009 at 6:13 am
1Hello
$searchResultsContent .= sprintf(”%s”, substr($data[$count][”article_description”], 0, $snapshotTextLength));
If I set snapshottextlength to 100, the above code will show the first 100 characters of article_description.
The search keyword may or may not exists within first 100 characters.
I want to show only 100 characters of article description around the search keyword. It is like google search result.
How can I achieve this ?
mi@ge (french user)
April 9th, 2009 at 5:57 pm
2Good job, thank you
vks
April 14th, 2009 at 12:35 am
3You can use regular expressions (preg_match etc…) to find the first occurrence of the required search text.
HTH
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Blogroll
Recent Comments