Dave Knipper Was Here

Generating a Lucene.NET Index

NHibernate.Search is an awesome search API, but it's poorly documented. Besides a few blog posts, I have not had much luck getting answers to my NHibernate.Search questions. I hope this article can help you avoid some headaches.

This article assumes you have a working knowledge of NHibernate Search and Lucene.NET. If you’re looking for basics on NHibernate Search and Lucene.NET, just Google "NHibernate.Search" and/or "Lucene.NET" and you'll find what you're looking for.

To work with this code, you will need to find some the NHibernate.Search and Lucene.Net DLLs. I had a hell of time finding the NHibernate.Search source code, so it's your lucky day! I added some support files here. They probably aren’t the last versions, but they work.

NHibernate Search: (2.6MB)

The following console application generates a full lucene index based off an indexable NHibernate data abstraction layer. I wrote this utility to regenerate a index for cases when my index and database get out of sync. This utility can be extremely helpful during initial development time when a lot of backdoor editing is happening to the database, and the application layer is not indexing the data when the database is being modified.

Here’s an example of a class from an NHibernate data abstraction layer which represents a glossary term. It’s important to note that this class is decorated a “Indexed,” this attribute indicates that this type of entity is to be indexed into the lucene index. Here’s the code for the console application. First off, the program looks for an old index and deletes the directory if it exists. The program than loads the assembly housing the data abstraction layer, and creates an NHibernate Search Session.

Once everything is initialized, the application loops through all the Types in the assembly’s exported types collection. If the current Type is decorated as "Indexed", all of the entities for that particular data entity class are added to the lucene index. This utility is great for design time and initial development. You know your project is moving along when you end up using this application less and less…