2

can someone tell me how to use nhibernate serach and lucene with fluent nhibernate. I have my application writen with fluent nhibernate but now i need full text serach but do not know how to implmenet nhibernate search with lucene to fluent nhibernate.

i found this but it is not much and do not know how to use it: Fluent NHibernate + Lucene Search (NHibernate.Search)

thx in advanced

4

1 回答 1

2

Lucene.net 是一个独立的搜索和目录实用程序。据我所知,它不能仅通过映射与 nhibernate 集成。您应该自己实现将数据添加到 lucene 索引。Lucene 允许将自定义字段添加到索引,以便您可以将记录的数据库 ID 与索引文本一起添加。

例如,如果您想将带有 id 的文本对象添加到 lucene 索引,您可以这样做:

public void AddRecordToIndex(string text, int id)
{
    IndexWriter writer = new IndexWriter("c:\\index\\my", new StandardAnalyzer(), true);
    Document doc = new Document();
    doc.add(Field.Text("contents", text));
    doc.add(Field.Keyword("id", id.ToStrirng()));
    writer.addDocument(doc);
}

维护索引的策略取决于您的应用程序。您可以在每次将数据提交到数据库时将数据添加到索引中,或者您可以增量地进行 - 每天一次(您必须将有关记录何时被索引或不在数据库表中的信息存储)。

如果创建了索引,您可以使用 IndexSearcher 对象搜索它,然后使用 id 将搜索结果与您的 NHibernate 对象结合起来。

于 2010-02-06T11:06:28.237 回答