Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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 commit().

Listing 18 listing-18.php
    // where to save our index
    $indexPath = '/var/www/';
    // 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)
    // re-add the document
    $index->addDocument(new PhpRiotIndexedDocument($document));
    // write the index to disk

In This Article

Article History

Apr 27, 2006
Initial article version
Dec 17, 2007
Updated to use Zend Framework 1.0.3