Development With A Dot

Blog on development in general, and specifically on .NET

Sponsors

My Friends

My Links

Permanent Posts

Portuguese Communities

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

Comments

Ayende Rahien said:

Huh?

Why not just call IFullTextSession.Index() ?

# October 13, 2009 4:18 PM

Ricardo Peres said:

Much better! Thanks! :-)

If only I had gotten any answer from the NHUsers mailing list, I wouldn't have to crawl NHibernate Search source with Reflector, and come up with this solution.

# October 14, 2009 5:30 AM

Ricardo Peres said:

By the way, most of the information out there about NHibernate Search is outdated... :-(

# October 14, 2009 5:36 AM

Angela said:

And how does that mean? I do not understand anything.

# March 27, 2012 11:26 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)