One problem is that Zend_Search_Lucene doesn??™t handle updating a document. If you want to update a
document, then you need to delete it and then re-add it. We don??™t want to ever end up with the same document
in the index twice, and so we will create Places_Search_Lucene as an extension of Zend_Search_Lucene and
override addDocument() to do a delete first if required. The code for Places_Search_Lucene is shown in
Listing 8.8.
Listing 8.8: Deleting a document before adding it
class Places_Search_Lucene extends Zend_Search_Lucene
{
public function addDocument(Zend_Search_Lucene_Document $document)
{
$docRef = $document->docRef; A
$term = new Zend_Search_Lucene_Index_Term(
$docRef, 'docRef');
$query = new Zend_Search_Lucene_Search_Query_Term($term);
$results = $this->find($query); B
if(count($results) > 0) { C
foreach($results as $result) C
{ C
$this->delete($result->id); C
} C
} C
return parent::addDocument($document); D
}
A docRef is unique in the index
B Find the document
C Delete current documents from index
D Add new document to index
This function works very well and does exactly what we need, however, it doesn??™t get called until we
change the static function as Zend_Search_Lucene::open() as this creates an instance of Zend_Search_Lucene,
not Places_Search_Lucene.
Pages:
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235