【发布时间】:2020-04-15 13:53:18
【问题描述】:
我编写了一个程序,它使用 Lucene.net 来索引一个 3GB 的文本文件。在构建索引时,进程的 CPU 消耗高达 80,内存使用量上升到 ~1GB。 有什么方法可以限制 CPU 和内存的使用? 下面是我用来构建索引的程序-
public void BuildIndex(string item)
{
System.Diagnostics.EventLog.WriteEntry("LuceneSearch", "Indexing Started for " + item);
string indexPath = string.Format(BaseIndexPath, "20200414", item);
if (System.IO.Directory.Exists(indexPath))
{
System.IO.Directory.Delete(indexPath, true);
}
LuceneIndexDirectory = FSDirectory.Open(indexPath);
Writer = new IndexWriter(LuceneIndexDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
Writer.SetRAMBufferSizeMB(500);
string file = "c:\LogFile.txt";
string line=string.Empty;
int count = 0;
StreamReader fileReader = new StreamReader(file);
while ((line = fileReader.ReadLine()) != null)
{
count++;
Document doc = new Document();
try
{
doc.Add(new Field("LineNumber", count.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("LogTime", line.Substring(6, 12), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("LineText", line.Substring(18, line.Length -18 ), Field.Store.YES, Field.Index.NOT_ANALYZED));
Writer.AddDocument(doc);
}
catch (Exception)
{
System.Diagnostics.EventLog.WriteEntry("LuceneSearch", "Exception ocurred while entring a line in the index");
}
}
System.Diagnostics.EventLog.WriteEntry("LuceneSearch", "Indexing finished for " + item + ". Starting Optimization now.");
Writer.Optimize();
Writer.Commit();
Writer.Close();
LuceneIndexDirectory.Dispose();
System.Diagnostics.EventLog.WriteEntry("LuceneSearch", "Optimization finished for " + item );
}
【问题讨论】:
标签: c# lucene lucene.net