Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
Subscribe to my feed
MainelyDesign.com Blog
Avoiding Errors with Zend Lucene Search Results
Posted on 01/14/2013 at 02:34 pm by Kevin Wentworth
Viewed 16,314 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>’;
}
}
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!