这是我搜索 Lucene 索引的代码,
String DocPath=@"c:\Test1.txt";
if (File.Exists(DocPath))
{
StreamReader Reader = new StreamReader(DocPath);
StringBuilder Content = new StringBuilder();
Content.Append(Reader.ReadToEnd());
if (Content.ToString().Trim() != "")
{
FSDirectory Direc = FSDirectory.Open(new DirectoryInfo(IndexDir));
IndexReader Reader = IndexReader.Open(Direc, true);
IndexSearcher searcher = new IndexSearcher(Reader);
QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Content", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29, new FileInfo(Application.StartupPath + Path.DirectorySeparatorChar + "noise.dat")));
BooleanQuery.MaxClauseCount = Convert.ToInt32(Content.ToString().Length);
Query query = parser.Parse(QueryParser.Escape(Content.ToString().ToLower()));
TopDocs docs = searcher.Search(query, Reader.maxDoc);
}
}
在这段代码中,我打开一个 15MB 的文本文件并将其提供给索引搜索器。搜索需要很长时间,并且显然会抛出OutOfMemoryException
. 解析查询甚至需要时间。索引大小约为 16K 文档。