Monday, January 23, 2006 06:35 AM
I'm using Lucene to find items stored in a database.
The way I implemented this is:
- get the records from the database, and create a Document with each row, with the primary key as a Keyword. Store that document in the Lucene file-based index.
- use Lucene to search Documents
- whenever you need to act on the database, get the primary key id and use it for your operations
- whenever you modify the database, update the index as well
I'm not sure that Lucene is meant to be used to query a database directly. You might want to use it to implement full text searching of your database, and all those cool features (+, - OR, AND, "exact phrase", etc). I did that the way I described above. I managed to get lightning fast search performances, and a painless index update process. The items that I'm searching aren't updated often, and using Lucene I saved many many database connections and boosted the search performance drastically.
There are many tutorials to show the update mechanism usually implemented (delete the Document then add it again). You might as well consider purchasing Lucene in Action from Mannings, which I just bought and is a really good book.