开发者

Each time Lucene indexing is running it's getting slower and slower

This code tests indexing with 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();

For each time the indexing is running, the indexing gets slower and slower. If I skip the search after the indexing, it doesn't get slower. This only occur when I start it with debugging. Not if I start it without debugging.

What may cause this?


I'm wondering if it's the first chance IOExceptions that occur when Lucene tries to auto-clear the index directory. These would occur since your readers/searchers are still open, and locks the segment files for deletion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜