首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NHibernate.Search与S#arp体系结构

NHibernate.Search与S#arp体系结构
EN

Stack Overflow用户
提问于 2009-12-29 12:20:04
回答 3查看 1.2K关注 0票数 2

有人设法让nhibernate.search (Lucene)使用S#arp体系结构吗?我认为我所有的连接都是正确的,除了卢克在运行我的索引方法时没有显示任何记录或索引。创建了实体的索引文件(segments.gen & segments_1),但两者的大小都是1kb,这就解释了路克为什么没有显示数据。

我没有执行其他特定的代码让搜索工作,我是不是遗漏了一些初始化调用?我假设nhibernate会自动捕获侦听器。

在我的Web项目中,我有:

NHibernate.config

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<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>

我的实体装饰如下:

代码语言:javascript
复制
[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; }
    ...
}

和我运行以下命令来创建索引

代码语言:javascript
复制
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);
}
EN

回答 3

Stack Overflow用户

发布于 2010-01-30 12:49:10

找到了我的问题的答案,所以这里是为了防止其他人出现这个问题。

web.config中的NHS配置包含这样的行:

代码语言:javascript
复制
<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应具有搜索索引目录的所有权限。

票数 1
EN

Stack Overflow用户

发布于 2009-12-29 16:27:03

乔丹,你在使用NHContrib的最新位元作为NHibernate.Search吗?我最近更新了我的信息,我遇到了和你一样的情况。对我来说,从七月份开始,这对我来说是可行的。但我也不能让我的索引创建。你的配置看起来是对的,和我的一样。你的索引方法看起来也不错。

票数 0
EN

Stack Overflow用户

发布于 2010-03-05 23:35:28

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

http://fnhsearch.codeplex.com/

代码语言:javascript
复制
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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1974366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档