Finding things using Zend Search Lucene in symfony

[tags]Zend, Zend Search Lucene, Search, Lucene, php, symfony, zsl[/tags]

This is part of an on going series about the Zend Search Lucene libraries and symfony. We’ll pretty everything up when we’re done =)

We now know how to manipulate the index via our model classes. But let’s actually do something useful with our search engine… let’s search!

[tags]Zend, Zend Search Lucene, Search, Lucene, php, symfony, zsl[/tags]

At the time of this writing we’re dealing with Propel which uses Peer classes which are meant for dealing with multiple objects1. This is the perfect place for a ::search() method. In otherwords, UserPeer::search('dave'); should query Lucene for users matching “dave”. Let’s make that happen:

What we’re doing is retrieving our Lucene index. Somewhere between tutorials we wrote this Peer function to handle that:

If our index is missing we’ll conveniently create it on the fly. We then use the Zend Search Lucene API to retrieve the matching hits in this index and then use some Propel trickery to retrieve by an array of primary keys.

It’s now simple to use ::search() functions in the same manner as you use ::doSelect().

At this point you should be able to create a basic symfony app that can utilize a Lucene index.

  1. The examples refer to using Propel, but it's trivial to adapt this to sfDoctrine