Sunday, August 21, 2011

NHibernate Search

Well I had all but given up on being able to use Lucene.net in the project I am currently busy with. The root indexes where being created correctly but when ever I add a child to the root collection it was not attaching the data to the indexed document. This actually provide so frustrating that I left it and continued on other aspects of the project, hoping that something might pop out at me.

Well, by God’s grace, something did. If you take the model below:

   1: [Indexed]



   2: public class Person{



   3:     [DocumentId]



   4:     public string Id{get;set;}



   5:     [Field]



   6:     public string FirstName{get;set;}



   7:     [Field]



   8:     public string LastName{get;set;}



   9:     [IndexedEmbedded(Depth = 1, Prefix = "attr_", TargetElement = typeof(PersonAttribute))]



  10:     public IList Attributes{get;set;}



  11: }



  12:  



  13: public class PersonAttribute{



  14:     public string Id{get;set;}



  15:     [Field]



  16:     public AttributeKey Key{get;set;}



  17:     [Field]



  18:     public string Value{get;set;}



  19: }



  20:  



  21: public enum AttributeKey{



  22:     Height,



  23:     Weight,



  24:     ShoeSize



  25: }




And then of course you add the listeners:





   1: config.SetListener(NHibernate.Event.ListenerType.PostUpdate, new FullTextIndexEventListener());



   2: config.SetListener(NHibernate.Event.ListenerType.PostInsert, new FullTextIndexEventListener());



   3: config.SetListener(NHibernate.Event.ListenerType.PostDelete, new FullTextIndexEventListener());




Everything works great for the nHibernate persistence to the Postgres data store. The root entry (Person) gets added to the data store and added to the document store but when ever I add an attribute the data store gets updated correctly but is not added to the document store. So I fought and I fought and I fought and I fought till eventually I came across a listing that someone else had in their configuration file and added it to the configuration of my application. Namely:





   1: config.SetListener(NHibernate.Event.ListenerType.PostCollectionUpdate, new FullTextIndexCollectionEventListener());



   2: config.SetListener(NHibernate.Event.ListenerType.PostCollectionRemove, new FullTextIndexCollectionEventListener());



   3: config.SetListener(NHibernate.Event.ListenerType.PostCollectionRecreate, new FullTextIndexCollectionEventListener());




And viola! On update of the child attribute list the embedded indexes were added correctly. Now this might seem obvious but the documentation on configuration is hard to find so I posted it incase someone else runs into the issue.



References:



http://stackoverflow.com/questions/1328647/lucene-net-indexes-are-not-updating-when-dealing-with-many-to-many-relationships


Technorati Tags: ,,


Windows Live Tags: NHibernate,Search,Lucene


WordPress Tags: NHibernate,Search,Lucene


No comments:

Post a Comment