views:

1185

answers:

1

I have just spent all day attempting to get NHibernate.Search working alongside NHibernate 2.0 and am sorry to say that I have still not managed it. I ran into the problem posted here and downloaded the dll linked by that post, however the example uses a search Interceptor rather than EventListeners, which I believe to be the newer way of doing things. There seems to be very little information available and what I can find is difficult to understand and contradicts other pieces of information.

At this point I am pretty frustrated with the whole thing and am seriously considering just writing my own integration of Nhibernate and Lucene (or perhaps another indexing library). At the moment it seems that NHibernate.Search is nowehere near mature enough for me to consider using it, I would be far more comfortable maintaining my own rather more simplified library.

What I would like to know is if there is a definitive way of using NHibernate.Search with NHibernate 2 and whether it is feasible to use this in a production environment.

+6  A: 

In order to setup EventListeners, you need to add this code when initializing NHibernate:

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
//Load configuration

//Add NHibernate.Search listeners
cfg.SetListener(NHibernate.Event.ListenerType.PostUpdate, new FullTextIndexEventListener());
cfg.SetListener(NHibernate.Event.ListenerType.PostInsert, new FullTextIndexEventListener());
cfg.SetListener(NHibernate.Event.ListenerType.PostDelete, new FullTextIndexEventListener());

var factory = cfg.BuildSessionFactory();

Your web.config/app.config file must be changed in order to include the following:

<configuration>

    <configSections>
     <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/>
     <section name="nhs-configuration" type="NHibernate.Search.Cfg.ConfigurationSectionHandler, NHibernate.Search" requirePermission="false"/>
    </configSections>

    <!-- NHibernate.Search -->
    <nhs-configuration xmlns='urn:nhs-configuration-1.0'>
     <search-factory>
      <property name='hibernate.search.default.directory_provider'>NHibernate.Search.Store.FSDirectoryProvider, NHibernate.Search</property>


      <property name='hibernate.search.default.indexBase'>PATH TO LUCENE.NET STORE</property>

      <property name='hibernate.search.indexing_strategy'>event</property>
     </search-factory>
    </nhs-configuration>

    <appSettings>
     <add key="Lucene.Net.lockdir" value="SAME PATH AS ABOVE" />
    </appSettings>

    ...

And finally: when you create an ISession instance, remember to use this code in order to get an IFullTextSession instead.

IFullTextSession session = Search.CreateFullTextSession(factory.OpenSession());

This should work with Lucene 2.0 and NHibernate 2.0.

Lck