Creating A Fulltext Search Engine In PHP 5 With The Zend Framework's Zend Search Lucene
Keeping The Index Up-To-Date
The other thing we haven’t yet dealt with is if any of our documents are updated. There are several ways to handle this:
- Update just the entry for the updated document straight away
- Rebuild the entire index when a document is updated straight away
- Rebuild the entire index at a certain time each day (or several times per day)
The ideal method really depends on the kind of data you have, how often it is updated, and how important it is for it the search index to be completely up-to-date.
Probably the most common way of dealing with this will be to update the document in the index when the document changes. The process for doing this is to delete it from the index then readd it.
To delete the document from the index, you must first find its internal ID. This can be done using the
termDocs() method. Once you know the ID you pass it to the delete() method of the index. You can then re-add the document (as well saw earlier) and then call
require_once('Zend/Search/Lucene.php'); require_once('PhpRiotIndexedDocument.php'); // where to save our index $indexPath = '/var/www/phpriot.com/data/docindex'; // some internal method to retrieve the document from the database $document = getDocument(); // create our index $index = Zend_Search_Lucene::open($indexPath); // find the document based on the indexed document_id field $term = new Zend_Search_Lucene_Index_Term($document->id, 'document_id'); foreach ($index->termDocs($term) as $id) $index->delete($id); // re-add the document $index->addDocument(new PhpRiotIndexedDocument($document)); // write the index to disk $index->commit();