星期一, 7月 11, 2005

[.Net]dotLucene(2)

其實,依照 dotLucene 網站所提供的 Tutorial,就已經蠻足夠的了.不過我還是同時參考了源碼裡所附的 Demo.

這邊我就只寫出跟 Tutorial / Demo 不同的地方...

1.在 Document 部分,我參考了 Demo 的寫法, 另外寫了一個 FileDocument, 來產生 IndexWriter 所需要的 Document 類別.
public class FileDocument
{
public static Document Document( string title, string content)
{
// make a new, empty document
Document doc = new Document();

// Add necessary fields
// 這邊要 tokenized, 為了中文...
doc.Add( new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));

// Add the contents of the file to a field named "contents".
// 原來是用 stream reader 來把整個檔案內容讀進來
// 這邊改成只把欄位資料放進去.
doc.Add( new Field("contents", content, Field.Store.YES, Field.Index.TOKENIZED));

// return the document
return doc;
}

private FileDocument()
{
}


2. 我把原來的 QueryParser 改成 MultiFieldQueryParser, 因為我想同時搜索主旨與內容.
string[] fields = { "title", "contents" };
Query query = MultiFieldQueryParser.Parse( txtQueryString.Text, fields, analyzer );
Hits hits = searcher.Search(query);


大致上就這樣子...
我之後想搞一個進度顯示,讓使用者不會覺得太慢,不過怎麼樣都不行.
這應該就要應用所謂的 UI/worker thread 的技術了吧...
真的是對 Windows form.Net 太不熟了...
這再慢慢摸索吧...

沒有留言: