[翻译]利用Lucene.Net进行文档递归查询

如何在项目中分析建立索引

1.添加引用lucene.net dll和名字空间

using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using ClassLibrary1.Icons;
using ClassLibrary1.Parsing;

2.在page load事件中声明保存索引文件的路径.程序会在页面生存周期内记住文件的路径.然后调用IndexBuilt和Search函数来实现查询.

this.pathIndex = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "test");
IndexBuilt();
search();

3.以下是IndexBuilt函数的内容.他将递归的添加文件夹.然后索引目录信息
private void IndexBuilt()
{
// 以下的代码在磁盘上创建一个新的索引.
indexWriter = new IndexWriter(this.pathIndex, new StandardAnalyzer(), true);
bytesTotal 
= 0;
countTotal 
= 0;
countSkipped 
= 0;

DirectoryInfo di 
= new DirectoryInfo(@"C:\Inetpub\wwwroot\test\Resumes");
DateTime start 
= DateTime.Now;
addFolder(di);

string summary = String.Format("Done. Indexed {0} files ({1} bytes). Skipped {2} files.", countTotal, bytesTotal, countSkipped);
summary 
+= String.Format(" Took {0}", (DateTime.Now - start));

indexWriter.Optimize();
indexWriter.Close();
}

4.search函数

private void search()
{
DateTime start 
= DateTime.Now;

try
{
searcher 
= new IndexSearcher(this.pathIndex);
}
catch (IOException ex)
{
MessageBox.Show(
"The index doesn't exist or is damaged. Please rebuild the index.\r\n\r\nDetails:\r\n" + ex.Message);
return;
}
Query query 
= QueryParser.Parse("test""text"new StandardAnalyzer());

Hits hits 
= searcher.Search(query);
for (int i = 0; i < hits.Length(); i++)
{
// 从索引获取文档
Document doc = hits.Doc(i);

// 在结果集里创建新的行
string filename = doc.Get("title");
string path = doc.Get("path");
string folder = Path.GetDirectoryName(path);
DirectoryInfo di 
= new DirectoryInfo(folder);
str 
+= filename + ","

}
searcher.Close();
Response.Write(str);
string searchReport = String.Format("Search took {0}. Found {1} items.", (DateTime.Now - start), hits.Length());
}

http://www.dotlucene.net/
posted @ 2007-04-15 14:16 jecray 阅读(302) 评论(2) 编辑 收藏