MainelyDesign.com Blog

Avoiding Errors with Zend Lucene Search Results

Posted on 01/14/2013 at 02:34 pm by Kevin Wentworth
Viewed 15,847 times | 0 comments

A common, recurring headache and complaint among Zend Lucene search processing is handling the data fields in search results.

We all like to be flexible and robust in handling the results, we may want our search to return news articles, blogs or webpage content.  

Invariably,  we run into problems, usually in the form of an exception

Fatal error: Uncaught exception 'Zend_Search_Lucene_Exception' with message 'Field name "whatever" not found in document.' 

What is frustrating is that the line throwing this exception is the just as likely the line where you are simply trying to test for the existence of the field.  In php you may commonly use “isset()” or “!empty()”.

The cause of all this grief is that Lucene does not return an array, rather it returns Objects which must be de-referenced. It is these objects that can be frustrating to handle as the individual elements cannot be tested until they’ve been assigned to usable php variables and they cannot be assigned to said variables if they do not exist, so one gets the exceptions, white screens and embarrassment.

What we have found at Saco Design is a simple fix, simple and effective.

First determine what fields are in the results using  function getFieldNames() where $hit is the result file.

     $fields = $hit->getDocument()->getFieldNames();

$fields is an array of the field names in the results this are values that can be tested with simple string search.  We like to use  in_array();

Example: Given that $hits is the results of the search, in our view we will loop through $hits

foreach ($hits as $hit){

$fields = $hit->getDocument()->getFieldNames();

if(in_array(‘whatever’,$fields){

         echo ‘<p>Search Returned  ‘.$hit->whatever.’</p>’;

        }

}

Bookmark and Share

Tags for Avoiding Errors with Zend Lucene Search Results

Cakephp | Web Programming | Tutorial | Php | Usage

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design