此代码使用Lucene.NET测试索引。
for (int i = 0; i < 10; i++)
{
var stopwatch = Stopwatch.StartNew();
string indexPath = Path.Combine("C:\\lucene\\");
var directory = FSDirectory.Open(new DirectoryInfo(indexPath));
var analyzer = new StandardAnalyzer(LuceneConfiguration.Version);
IndexWriter indexWriter = null;
try
{
indexWriter = new IndexWriter(directory, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
indexWriter.DeleteAll();
for (int documentNumber = 0; documentNumber < 100; documentNumber++)
{
var document = new Document();
for (int fieldNumber = 0; fieldNumber < 10; fieldNumber++)
{
document.Add(new Field("Field" + fieldNumber, "asdf qwerty Value" + fieldNumber, Field.Store.YES,
Field.Index.ANALYZED));
}
indexWriter.AddDocument(document);
}
indexWriter.Optimize();
}
finally
{
if (indexWriter != null)
{
indexWriter.Close();
}
}
stopwatch.Stop();
Console.WriteLine("Index time: " + stopwatch.Elapsed.TotalMilliseconds);
var reader = IndexReader.Open(directory, true);
var searcher = new IndexSearcher(reader);
var parser = new QueryParser(LuceneConfiguration.Version, "Field0", analyzer);
var query = parser.Parse("asdf");
var collector = TopScoreDocCollector.create(10, true);
searcher.Search(query, collector);
Console.WriteLine("Hits: " + collector.GetTotalHits());
}
Console.ReadKey();
每次索引运行时,索引都会越来越慢。如果我在索引后跳过搜索,它就不会变慢。这只有在我通过调试启动它时才会发生。如果我没有调试就启动它。
可能导致这种情况的原因是什么?
答案 0 :(得分:1)
我想知道这是否是Lucene尝试自动清除索引目录时发生IOExceptions的第一次机会。这些都会发生,因为您的读者/搜索者仍处于打开状态,并锁定段文件以进行删除。