利用Lucene.Net进行文档递归查询
2007-04-06 16:21
351 查看
如何在项目中分析建立索引
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/
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/
相关文章推荐
- [翻译]利用Lucene.Net进行文档递归查询
- 利用Lucene.Net进行文档递归查询
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- Vb.net下,利用数组进行“组合”查询
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- asp.net 利用多表联合查询进行汇总统计
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- 利用Lucene.net搜索引擎进行多条件搜索的做法 (转)
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- lucene利用sort对查询结果进行排序示例
- 利用Lucene.net搜索引擎进行多条件搜索
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- Vb.net 利用数组进行组合查询
- lucene利用BooleanQuery进行多个Query组合查询
- VS2010下asp.net 对现有的PDF文档进行加密(利用iTextSharp)
- 利用Lucene.net搜索引擎进行多条件搜索的做法
- 利用Lucene.net搜索引擎进行多条件搜索的做法