Fluent nhibernate + nhibernate.serach + lucene.net

寵の児 提交于 2019-12-03 09:05:35

Lucene.net is a self-contained search and directory utility. As far as I know it cannot be integrated with nhibernate by mappings only. You should implement adding data to lucene index by yourself. Lucene allows to add custom fields to index so you can add your database id's of the records together with indexed texts.

For example, if you want to add text object with id to lucene index you can do it like that:

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

The strategy of maintaining the index depends of your application. You can add data to index each time they are commited to database or you can do it incrementally - once a day (you have to store the information about whenever the record is indexed or not in your database table then).

If the index is created you can search it with IndexSearcher object, and then combine the search results with you NHibernate objects using the id's.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!