NHibernate 搜索 Lucene.NET SearchFactory 为 null

发布于 2024-10-10 07:33:12 字数 3056 浏览 7 评论 0原文

我已经为以下问题苦苦挣扎了几个小时。我用不同的 NHibernate/NHibernate.Search 程序集(3.0.0.4 / 2.1.2)尝试过此操作,所有这些都会导致相同的错误。使用的Lucene版本是2.9.2.2

都是从源码编译的。 NHibernate 设置为使用 NHibernate Search,配置通过 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());
                }
            );

到目前为止一切顺利,在 bin 文件夹的 Index 目录中创建了一个索引(segments.gen 和segments_1 文件)。

创建配置后,我获取 NHibernate Session 并尝试索引某些内容:

        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();
        }

一切正常,直到命中 fts.PurgeAll 或 fts.Index,这会出现此错误:

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:                {

此错误是从 NHibernate.Search.dll 抛出的,它看起来 SearchFactory 尚未初始化。应该创建 SearchFactory 的代码返回 null:

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

遇到了几种可能的解决方案,我需要使用 SearchFactory.Initialize 初始化 SearchFactory,但我的(2.0 / 3.0)NHibernate.Search 程序集中不存在这样的方法。

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

还抛出一个“空异常”(当然),上面正在调用:

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

其中 searchFactory == null

有一个 SearchFactoryImpl

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

它返回一个 SearchFactoryImpl 实例,但不知道如何处理它......

也许我错过了一些东西?非常感谢任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡寂寞咖啡 2024-10-17 07:33:12

嗯,看来 Ninject 与此有关。不知道为什么/如何。我有一个 NH 3.0.1.4000 + Search + Lucene 2.9.2.2 的工作解决方案,如果有兴趣,请给我发电子邮件。

http://ceesplug.nl/LuceneNHibernateTest.rar

完整的解决方案,适用于带有或不带有 FluentNHibernate 的 NHibernate。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文