PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

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
<?php
    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();
?>

In This Article


Article History

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