17 Mar
Posted by Ganesh H S , Bangalore, India as zend framework
Once the index is created, we are ready use zend lucene search to search the website. In the following example, php is the search keyword used to fetch the relevant search results in the already indexed data.
<?php
require_once ‘Zend/Search/Lucene.php’;$query = "php";
$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);
?>
To retrieve the index data, first thing we need to do is to open the indexed path.
$index = Zend_Search_Lucene::open("/var/www/lucene-data/blog-index");
Suppose if user search input is -
$query = "php";
We have to use the find method of zend search lucene -
$results = $index->find($query);
To retrieve the total records resulted in the search result, we have to use count method of zend lucene search -
echo "Index contains ".$index->count()." documents.\n\n";
To limit the search result count we have to use setResultSetLimit of zend lucene search -
$index->setResultSetLimit(10);
Related articles:
Zend Lucene Search - part1 - creating index
Zend Lucene Search - part2 - Real time indexing
Zend Lucene Search - part4 - Search Results Highlighting
Home Page
8 Responses
G
September 15th, 2008 at 4:26 am
1Did you try to create index directly in Java? Would be intresting to show how to do that.
Ganesh
September 16th, 2008 at 2:14 am
2This is done using php zend framework, i haven’t used apache lucene in java.
chen_kevin
January 20th, 2009 at 3:55 am
3how to use pagination for zend search lucene!
Ganesh H S , Bangalore, India
February 22nd, 2009 at 2:14 pm
4Hello chen-kevin,
1. Easier way is to get the data from lucene find for a keyword, once you get the array you extract the data based on the page number and number items per page you want to show.
2. Never got a chance to look if zend lucene search supports the range search support? if it supports it would be best approach for pagination.
Will let you know soon on #2. Let me know if you are still blocked?
- Ganesh
Mike
May 11th, 2009 at 12:53 am
5Hi.
Have you had chance to practice a bit with Zend_Paginator?
I haven`t fount other method than save all search results in array and then pass it to the Zend_Paginator. So on each page I still selecting all resultset. It`s a bit ugly solution
Thanks for your opinion
yugenekr
October 1st, 2009 at 3:33 am
6Hello!
Thanks for posts about Lucene. This is my first attempt to use this search engine and ur posts are helpful.
Don’t u need to use parser for the query string?
$query = Zend_Search_Lucene_Search_QueryParser::parse($queryStr, ‘utf-8′);
in this topic?
Other way I got
Call to a member function highlightMatches() on a non-object
error
And the second problem is (may be u could help) if I use parser and highlightMatches() function
then I got page with wrongly encoded characters on string that was highlighted.
Lang Nguyen
December 19th, 2009 at 10:32 am
7Yes i think you change code to
highlightMatches($str, ‘utf-8′)
good luck to you
Lang
March 10th, 2010 at 1:54 am
8How to order by
$results = $index->find($query, ‘data_id’ int, desc); performent very slow
hox to fix it
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