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
Home Page
20 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?
suhail
September 19th, 2008 at 12:18 am
4Hi,
informative article, one thing which is missing in this article is the paging. you have tell about the limiting records, but how can i implement paging in zend search lucene.
Daniel Bandel
October 30th, 2008 at 9:50 am
5Thanks, excellent article! I’m brasilien and use charset=ISO-8859-1. It is not powerfull. How can I implement highlightMatches() to run it? Thanks again!
Ryan
January 4th, 2009 at 4:22 am
6cool.
Also can do it like this:
$term = new Zend_Search_Lucene_Index_Term($quserStr, $field);
$query = new Zend_Search_Lucene_Search_Query_Term($term);
$hits = $index->find($query);
foreach ($hits as &$hit) {
$hit->$field = $query->highlightMatches($hit->$field);
}
Ryan
January 4th, 2009 at 5:19 am
7by the way ,how to highlight a string in the page that is opened in the search list.
zaw
March 13th, 2009 at 10:29 am
8In the display result list view page, when searching html file body content the result may be long text.
How can I display only the synopsis of the long text?
I want to show the parts of text in result that found the search terms.
e.g. in google result when I find the search term | zend framework sample |
Akra?s DevNotes ? Tutorial: Getting Started with Zend Framework
The original tutorial for Zend Framework 1.0 is still available, …… Rob, Did you have a sample of using Zend call linux command ? how to do that? …
In above e.g., Google show two parts of text. How can I do like this?
Ganesh H S , Bangalore, India
March 16th, 2009 at 2:36 am
9Hi Zaw,
I have written a program that meets your requirement as suggested in your comment,
Check this post,
http://ganeshhs.com/zend-framework/zend-lucene-search-part5-search-engine-results-page-formating
Let me know if you need any help.
Ganesh H S , Bangalore, India
March 16th, 2009 at 2:38 am
10Hello Ryan,
I didn’t get your question exactly “by the way ,how to highlight a string in the page that is opened in the search list.”? What are you trying to achieve, can you explain?
Ganesh H S , Bangalore, India
March 22nd, 2009 at 9:56 pm
11Zaw,
The article you were looking for is -
http://ganeshhs.com/zend-framework/zend-lucene-search-part5-search-engine-results-page-formatting
fei
June 21st, 2009 at 8:58 pm
12Hi, i’m doing research about Zend_Search_Lucene. i want to more understand about this search engine. i have few questions here. May i know what is the meaning of stored, indexed, tokenized and binary at the table above? How to different between each other?
fei
June 21st, 2009 at 9:02 pm
13sorry, i also want to know what is the meaning of hightlightMatchs()?
is it search deeper? is it split into few keywords then search again? The hightlightMatchs() is search by each character or whole keyword? thank you
Ganesh H S , Bangalore, India
August 4th, 2009 at 3:46 pm
14hightlightMatchs is used for highlighting the text with some color, for the matches found in the content.
sandeep
August 11th, 2009 at 2:13 am
15hi ganesh it was an excellent article for my requirement it exactly suits and thanks a lot, and i am getting an error hightlightMatchs() with the error is Warning: domdocument::domdocument() expects at least 1 parameter, 0 given
Ganesh H S , Bangalore, India
August 18th, 2009 at 2:59 pm
16Hi Sandeep,
Its nothing to deal with zend lucene search.
Probably you are missing some library packages related to dom, try installing it.
- Ganesh
SNairs
September 14th, 2009 at 4:05 pm
17Is there anythings simlar to Limit N, 25 part of Mysql query in Lucene search?
I would like to display a paginated result with links to pages that are found
1 2 3 4…….39 40 last
yugenekr
October 12th, 2009 at 4:44 am
18Hi!
It’d be greate to have an example on fuzzy search also.
Thanks for articles!
Michael
October 29th, 2009 at 3:20 pm
19As far as I see Zend Lucene provides a method to limit the the search results (setResultSetLimit) to avoid bursting the memory of the server but there is no method for a offset to continue the search.
M. Gogol
April 26th, 2010 at 11:04 pm
20Thanks, this was a great tutorial, it certainly sheds some light on searching text effectively.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Blogroll
Recent Comments
Ganesh H S is proudly powered by WordPress - BloggingPro theme by: Design Disease