Question

I've been struggling with the following issue for hours now. I tried this with different NHibernate/NHibernate.Search assemblies (3.0.0.4 / 2.1.2), all of them result in the same error. Used Lucene version is 2.9.2.2

All of them compiled from source. NHibernate is setup to use NHibernate Search, configuration goes through Fluent NHibernate.

FluentConfiguration fc = Fluently.Configure()
. (mappings, db config, etc.)
.ExposeConfiguration
            (
                cfg =>
                {
                    cfg.SetProperty("hibernate.search.default.directory_provider", typeof(FSDirectoryProvider).AssemblyQualifiedName);
                    cfg.SetProperty("hibernate.search.default.indexBase", "~/Index");
                    cfg.SetProperty("hibernate.search.default.indexBase.create", "true");

                    cfg.SetListener(NHibernate.Event.ListenerType.PostUpdate, new FullTextIndexEventListener());
                    cfg.SetListener(NHibernate.Event.ListenerType.PostInsert, new FullTextIndexEventListener());
                    cfg.SetListener(NHibernate.Event.ListenerType.PostDelete, new FullTextIndexEventListener());
                }
            );

So far so good, an index is created in the Index directory in the bin folder (segments.gen & segments_1 files).

After the configuration has been created I fetch the NHibernate Session and try to index something:

        var _session = _factory.OpenSession();
        using (ITransaction tx = _session.BeginTransaction())
        {
            var fts = Search.CreateFullTextSession(_session);
            fts.PurgeAll(typeof(User));

            var coll = fts.CreateCriteria<User>().List<User>();
            foreach (var item in coll)
            {
                fts.Index(item);
            }

            tx.Commit();
        }

This goes fine until the fts.PurgeAll or fts.Index is hit, which gives this error:

Object reference not set to an instance of an object.
Line 602:                // TODO: Cache that at the FTSession level
Line 603:                // not strictly necesary but a small optmization
Line 604:                DocumentBuilder builder = searchFactoryImplementor.DocumentBuilders[clazz];
Line 605:                if (builder != null)
Line 606:                {

This error is thrown from the NHiberate.Search.dll, it looks like the SearchFactory was not initialized. The code that should create the SearchFactory returns null:

            if (searchFactory == null)
            {
                searchFactory = ContextHelper.GetSearchFactory(session);
            }

Came across several possible solutions where I need to initialize the SearchFactory with SearchFactory.Initialize, but no such method exists in my (2.0 / 3.0) NHibernate.Search assemblies.

NHibernate.Search.Search.CreateFullTextSession(_session)
            .CreateFullTextQuery<User>("Firstname:Cees").List<User>();

Also throws a 'null exception' (of course), above is calling:

IDictionary<System.Type, DocumentBuilder> builders = searchFactory.DocumentBuilders;

Where searchFactory == null

There is a SearchFactoryImpl

SearchFactoryImpl searchFactory = NHibernate.Search.Impl.SearchFactoryImpl.GetSearchFactory(config);

Which return a SearchFactoryImpl instance, no idea what to do with it though...

Maybe I'm missing something? Any help is much appreciated.

Was it helpful?

Solution

Hmm, seems like Ninject had something to do with it. Not sure why/how. I've got a working solution with NH 3.0.1.4000 + Search + Lucene 2.9.2.2, if interested, send me an email.

http://ceesplug.nl/LuceneNHibernateTest.rar

Complete solution, working for NHibernate with and without FluentNHibernate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top