#native_company# #native_desc#

Using Zend_Search_Lucene, the PHP Lucene Implementation Page 2

By W. Jason Gilmore
on July 6, 2011

PHP + Lucene: Searching Documents

With several resumes indexed, let’s turn our attention to the search interface. Starting with a simple scenario, suppose an employer wanted to search for all job seekers who mentioned Dell somewhere within their resumes:
$index = Zend_Search_Lucene::open('/var/www/dev.example.com/lucene-index'); $query = 'Dell'; $results = $index->find($query); foreach($results AS $result) { printf("%f
", $result->score); printf("<a href="../../people/id/%d">%s</a>
", $result->dbid, $result->name); }
Any returned search results are iterated over and output to the browser with a URL generated, which presumably points to a controller action that will use the provided primary key to retrieve more information about the job seeker. Additionally, a score is included which indicates the quality of the match. You can optionally limit the number of results returned using the setResultSetLimit() method (see the documentation for more information).
You can narrow the search to a specific indexed field by modifying the search query to look something like this:
$query = 'name: "Gilmore"';
It’s also possible to define queries which search a specific indexed field:
$term = new Zend_Search_Lucene_Index_Term('Ohio', 'education'); $query = new Zend_Search_Lucene_Search_Query_Term($term); $results = $index->find($query);
You can even combine terms to produce more complex queries. For instance, the following example will return only job seekers who mention the term Ohio in their education with the qualifier that Michigan is not also referenced in the same field:
$query = new Zend_Search_Lucene_Search_Query_MultiTerm(); $query->addTerm(new Zend_Search_Lucene_Index_Term('Ohio', 'education'), true); $query->addTerm(new Zend_Search_Lucene_Index_Term('Michigan', 'education'), false); $results = $index->find($query);


The Zend_Search_Lucene component isn’t without its limitations; for instance, only indexes of up to 2GB are currently supported on 32-bit operating systems. However, if you’re not intent on building the next Monster.com, chances are it’s going to suit your needs quite nicely!

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books “Easy PHP Websites with the Zend Framework”, “Easy PayPal with PHP”, and “Beginning PHP and MySQL, Fourth Edition”. Follow him on Twitter at @wjgilmore.