Creating, Updating, Deleting documents in a Lucene Index with symfony
24 April 2007
Previously we covered an all-at-once approach to indexing objects in your symfony app. But for some reason, people find the need to allow users to sign up, or change their email addresses and then all of a sudden our wonderful Lucene index is out of date.
Here lies the strength of using Zend Search Lucene in your app, you can now get the flexibility of interacting with a Lucene index, no matter how it was created and add, update and delete documents to it.
The last thing you want to do is have a cron job in charge of making sure your index is always up to date by reindexing regularly. This is an inelegant and inefficient process.
A smarter method would be to trigger an update of the index each time you update your database. Luckily the ORM layer allows us to do this using objects (in our case Propel objects).
If we look at our user example from before, we did set ourselves up to easily do this using our
User::generateZSLDocument() function, which did most of the heavy lifting.
We can make a few small changes to the
We have an attribute called
$reindex. When it is false we don’t need to worry about the index. When something significant changes, like an update to your name or email address, then we set
true. Then when we save with an overridden save method:
Now we’ve got the exact same data that we created during our original indexing. This handled creating and updating object, but we miss updating the index when deleting objects.
Luckily we already made a function
Read full post
User::removeFromIndex() to remove any related documents from the index, so our delete function can be pretty simple: