NHibernate.Search与S#arp架构

时间:2009-12-29 12:20:05

标签: asp.net-mvc nhibernate lucene.net s#arp-architecture

有没有人设法让nhibernate.search(Lucene)与S#arp架构合作?我认为我已经正确连接,除了Luke在运行索引方法时没有显示记录或索引。创建了实体的索引文件(segments.gen& segments_1),但两者的大小均为1kb,这解释了为什么Luke没有显示数据。

我没有执行其他特定于搜索工作的代码,我是否缺少一些初始化调用?我假设听众被nhibernate自动拾取。

在我的Web项目中,我有:

NHibernate.config

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.connection_string">Data Source=.\SQLEXPRESS;Database=MyDatabase;Integrated Security=True;</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="show_sql">true</property>
    <property name="generate_statistics">true</property>
    <property name="connection.release_mode">auto</property>
    <property name="adonet.batch_size">500</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

    <listener class='NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search' type='post-insert'/>
    <listener class='NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search' type='post-update'/>
    <listener class='NHibernate.Search.Event.FullTextIndexEventListener, NHibernate.Search' type='post-delete'/>
  </session-factory>
</hibernate-configuration>

的Web.Config

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

<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">~\Lucene</property>
  </search-factory>
</nhs-configuration>

我的实体装饰如下:

[Indexed(Index = "Posting")] 
public class Posting : Entity
{
    [DocumentId]
    public new virtual int Id
    {
        get { return base.Id; }
        protected set { base.Id = value; }
    }

    [Field(Index.Tokenized, Store = Store.Yes)]
    [Analyzer(typeof(StandardAnalyzer))]
    public virtual string Title { get; set; }

    [Field(Index.Tokenized, Store = Store.Yes)]
    [Analyzer(typeof(StandardAnalyzer))]
    public virtual string Description { get; set; }

    public virtual DateTime CreatedDate { get; set; }
    ...
}

我运行以下内容来创建索引

public void BuildSearchIndex()
{
    FSDirectory directory = null;
    IndexWriter writer = null;

    var type = typeof(Posting);

    var info = new DirectoryInfo(GetIndexDirectory());

    if (info.Exists)
    {
        info.Delete(true);
    }

    try
    {
        directory = FSDirectory.GetDirectory(Path.Combine(info.FullName, type.Name), true);
        writer = new IndexWriter(directory, new StandardAnalyzer(), true);
    }
    finally
    {
        if (directory != null)
        {
            directory.Close();
        }

        if (writer != null)
        {
            writer.Close();
        }
    }

    var fullTextSession = Search.CreateFullTextSession(this.Session);

    // select all Posting objects from NHibernate and add them to the Lucene index
    foreach (var instance in Session.CreateCriteria(typeof(Posting)).List<Posting>())
    {
        fullTextSession.Index(instance);
    }
}

private static string GetIndexDirectory()
{
    var nhsConfigCollection = CfgHelper.LoadConfiguration();
    var property = nhsConfigCollection.DefaultConfiguration.Properties["hibernate.search.default.indexBase"];
    var fi = new FileInfo(property);
    return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fi.Name);
}

3 个答案:

答案 0 :(得分:1)

找到我的问题的答案,所以这是因为其他人遇到这个问题。

web.config中的NHS配置包含以下行:

<property name="hibernate.search.default.directory_provider">NHibernate.Search.Store.FSDirectoryProvider, NHibernate.Search</property>
<property name="hibernate.search.default.indexBase">~\SearchIndex</property>

应删除第一行,因为在这种情况下,NHS会将其视为索引共享。众所周知,NHibernateSearch问题。

如果站点是从IIS运行的,则Network Service应具有搜索索引目录的所有权限。

答案 1 :(得分:0)

乔丹,您是否正在使用来自NHContrib的NHibernate.Search的最新部分?我刚刚更新了我的位,我遇到了与你相同的情况。从7月左右开始,它适用于较旧的钻头。但我无法让我的索引创建。您的配置看起来正确,与我的相同。你的索引方法看起来也不错。

答案 2 :(得分:0)

乔丹,现在有一种替代基于属性的Lucene.NET映射,名为FluentNHibernate.Search,这个项目托管在codeplex上。

http://fnhsearch.codeplex.com/

public class BookSearchMap : DocumentMap<Book>
{
    public BookSearchMap()
    {
        Id(p => p.BookId).Field("BookId").Bridge().Guid();
        Name("Book");
        Boost(500);
        Analyzer<StandardAnalyzer>();

        Map(x => x.Title)
            .Analyzer<StandardAnalyzer>()
            .Boost(500);

        Map(x => x.Description)
            .Boost(500)
            .Name("Description")
            .Store().Yes()
            .Index().Tokenized();
    }
}