我无法获得nHibernate.Search来创建索引。

如果我使用nHibernate.dll的1.2.1.4& nHibernate.Search.dll然后正确创建索引,我可以用Luke(一个Lucene实用程序)检查它。 创建段文件以及片段文件等

然而,当我使用vH的nHibernate.dll& nHibernate.Search.dll然后索引未正确创建。在Index目录中只创建了1k段文件,Luke无法检查它。

我在v1中使用的代码如下:

_configuration = new Configuration();
_configuration.Configure();
_configuration.AddAssembly(typeof (Contact).Assembly);
_sessionFactory = _configuration.BuildSessionFactory();
SearchFactory.Initialize(_configuration, _sessionFactory);

我在配置文件中有以下内容

<property name="hibernate.search.default.directory_provider">NHibernate.Search.Storage.FSDirectoryProvider, NHibernate.Search</property>
<property name="hibernate.search.default.indexBase">~/Index</property>

在版本2中没有SearchFactory。我能找到的唯一类似的东西是

SearchFactoryImpl.GetSearchFactory(_configuration);

所以我按如下方式设置了配置

_configuration = new Configuration();
_configuration.Configure();
_configuration.AddAssembly(typeof (Contact).Assembly);
_sessionFactory = _configuration.BuildSessionFactory();
_configuration.SetProperty("hibernate.search.default.directory_provider",
                                       "NHibernate.Search.Store.FSDirectoryProvider, NHibernate.Search");

_configuration.SetProperty("hibernate.search.default.indexBase", "Index");
_configuration.SetProperty("hibernate.search.analyzer",
                                        "Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net");


_configuration.SetListener(ListenerType.PostUpdate, new FullTextIndexEventListener());
_configuration.SetListener(ListenerType.PostInsert, new FullTextIndexEventListener());
_configuration.SetListener(ListenerType.PostDelete, new FullTextIndexEventListener());

SearchFactoryImpl.GetSearchFactory(_configuration);

这会创建一个索引的骨头,但是Luke无法查看 - 这告诉我它已损坏

我还使用以下代码手动尝试创建索引,但它再次只创建了段文件,没有别的

public void CreateIndex<T>(string rootIndexDirectory)
{
    Type type = typeof (T);

    var info = new DirectoryInfo(Path.Combine(rootIndexDirectory, type.Name));

    // Recursively delete the index and files in there
    if (info.Exists) info.Delete(true);

    // Now recreate the index
    FSDirectory dir = FSDirectory.GetDirectory(Path.Combine(rootIndexDirectory, type.Name), true);
    //Ioc.UrlProvider.MapPath(Path.Combine(rootIndexDirectory, type.Name)), true);

    try
    {
        var writer = new IndexWriter(dir, new StandardAnalyzer(), true);
        writer.Close();
    }
    finally
    {
        if (dir != null) 
            dir.Close();
    }

    using (ISession session = _sessionFactory.OpenSession())
    {
        using (IFullTextSession fullTextSession = Search.CreateFullTextSession(session)) 
        {
            foreach (var contact in _contacts)
            {
                //session.Save(contact);
                fullTextSession.Index(contact);
            }
        }
    }
}

所以我的问题是 - 如果我想使用nHibernate.Search,我是否必须使用nHibernate的v1.1.4? 或者我可以使用v2吗?在哪种情况下,我做错了什么?

网上关于此事的人很少。

任何?

scroll top