1
此代碼使用Lucene.NET測試索引。每次運行Lucene索引時,它都會變得越來越慢
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();
對於每次索引運行時,索引變得越來越慢。如果我在索引後跳過搜索,它不會變慢。這隻發生在我用調試開始時。不,如果我沒有調試就啓動它。
什麼可能導致這種情況?
我沒有看到明確的'.Commit()'你是否自動提交某種?你是在分析相同的構建還是使用一個調試構建和另一個構建版本? – skarmats 2011-03-31 09:11:47
indexWrite.Close()提交它。如果我在發行版或調試版中運行它無關緊要。 – Allrameest 2011-03-31 09:19:20
啊,如果我在調試時運行它會很慢。如果我在沒有調試的情況下啓動它,它總是很快。 – Allrameest 2011-03-31 09:21:23