views:

389

answers:

1

Hi,

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: http://stackoverflow.com/questions/551101/fluent-nhibernate-lucene-search-nhibernate-search

thx in advanced

A: 

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.

PanJanek
hmm do you have any tutorial or any link where can i see this?Regards
senzacionale
http://today.java.net/pub/a/today/2003/07/30/LuceneIntro.htmlhttp://www.lucenetutorial.com/lucene-in-5-minutes.htmlhttp://www.bitworm.com/search/2007/simple-lucene-example/these examples are meant for java, but lucene.net keeps exact the same API, so it should work in c# as well.
PanJanek
thx for your answer. So if i understand you correctly i do not need nhibernate and nhibernate serach if i want to use lucene with fluent nhibernate?
senzacionale
Yes. If you build your index with all the searchable data, then you don't have to access the database during search.
PanJanek
On the other hand - nhibernate.search can help a lot.
Arnis L.