Lucene - Zend
I’m not sure if this is the norm, I stumbled across the answer while banging my head against a wall trying to integrate a Lucene search through the ZF framework (zend). So, as an example I wanted to run the following code block. It simply adds a document and some fields to the the document and some unstored content.. pretty basic.
Notice the last line though…. “index->commit();”
You will have to look hard to find the use or reason for using this, but if you do not and your script ends pre-maturely, there is a chance you will not get the results you expect. The examples I have seen do not mention that if you do not do this and you run and exit or throw an exception prior to the front controller dispatching, it goes to shit.
Just as an fyi many people after the commit will toss in the index->optimize,.. it’s a good practice, I just did not do it for this example.
Example code:
$index = Zend_Search_Lucene::create(SEARCH_DIR.'articles/'.$data['article_index']);
$doc = new Zend_Search_Lucene_Document();
$doc->addField(Zend_Search_Lucene_Field::Text(’title’, $data['article_title']));
$doc->addField(Zend_Search_Lucene_Field::Text(’description’, $data['article_short']));
$doc->addField(Zend_Search_Lucene_Field::Text(’link’, $data['article_url']));
$doc->addField(Zend_Search_Lucene_Field::UnStored(’contents’, $data['index_content']));
$index->addDocument($doc);
$index->commit();
27 Apr 2009 vMonkey 0 comments