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:

 

[Indexed]
public class SomeClass
{
	[DocumentId]
	public virtual Int32 Id
	{
		get;
		private set;
	}

	[Field(Index.Tokenized, Store = Store.Yes)]
	public virtual String SomeProperty
	{
		get;
		set;
	}
}

Then, use this code:

Configuration cfg = ...;
ISession session = ...;
SomeClass a = ...;

using (IFullTextSession searchSession = Search.CreateFullTextSession(session))
{
    searchSession.Index(a);
}

And, finally, query it using this:

IList<SomeClass> items = searchSession.CreateFullTextQuery<SomeClass>("SomeProperty:SomeValue").List<SomeClass>();
Bookmark and Share

                             

4 Comments

Comments have been disabled for this content.