Manually Indexing an Entity with NHibernate Search
Updated: thanks, Ayende!
NHibernate Search, which you can get in source format with SVN from the NHContrib trunk here, is an API that integrates NHibernate with the popular indexer Lucene.NET. Out of the box, it indexes the properties from your entities, the way you want it to (at the moment, only by using attributes), at insert/update/delete time, through the use of listeners. But what if you want to index an entity that already comes from the DB? Let's see how this can be done.
First, out entity:
public class SomeClass
public virtual Int32 Id
[Field(Index.Tokenized, Store = Store.Yes)]
public virtual String SomeProperty
Then, use this code:
Configuration cfg = ...;
ISession session = ...;
SomeClass a = ...;
using (IFullTextSession searchSession = Search.CreateFullTextSession(session))
And, finally, query it using this:
IList<SomeClass> items = searchSession.CreateFullTextQuery<SomeClass>("SomeProperty:SomeValue").List<SomeClass>();