Fluent nhibernate + nhibernate.serach + lucene.net

前端 未结 1 1810
情歌与酒
情歌与酒 2021-02-11 04:02

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 no

相关标签:
1条回答
  • 2021-02-11 04:49

    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.

    0 讨论(0)
提交回复
热议问题