views:

45

answers:

1

I've got an issue which shows up intermitantly in my unit tests and I can't work out why.

The unit test itself is adding multiple documents to an index, then trying to query the index to get the documents back out again.

So 95% of the time it works without any problems. Then the other 5% of the time it cannot retrieve the documents back out of the index.

My unit test code is as follows:

[Test]
    public void InsertMultipleDocuments()
    {
        string indexPath = null;
        using (LuceneHelper target = GetLuceneHelper(ref indexPath))
        {
            target.InsertOrUpdate(
                    target.MakeDocument(GetDefaultSearchDocument()), 
                    target.MakeDocument(GetSecondSearchDocument()));

            var doc = target.GetDocument(_documentID.ToString()).FirstOrDefault();
            Assert.IsNotNull(doc);
            Assert.AreEqual(doc.DocumentID, _documentID.ToString());

            doc = target.GetDocument(_document2ID.ToString()).FirstOrDefault();
            Assert.IsNotNull(doc);
            Assert.AreEqual(doc.DocumentID, _document2ID.ToString());
        }

        TidyUpTempFolder(indexPath);
    }

I won't post the full code from my LuceneHelper, but the basic idea of it is that it holds an IndexSearcher in reference which is closed every time an item is written to the index (so it can be re-opened again with all the of the latest documents).

The actual unit test will often fail when gathering the second document. I assumed it was to do with the searcher not being closed and seeing cached data, however this isn't the case.

Does Lucene have any delay in adding documents to the index? I assumed that once it had added the document to the index it was available immediately as long as you closed any old search indexers and opened a new one.

Any ideas?

A: 

How do you close the IndexWriter you use for updating the index? The close method has an overload that takes a single boolean parameter specifying whether or not you want to wait for merges to complete. The default merge scheduler runs the merges in a separate thread and that might cause your problems.

Try closing the writer like this:

indexWriter.Close(true);

More information can be found at Lucene.NET documentation.

Btw, which version of Lucene.NET are you using?

HakonB
This sounded like it would fix it, however I am still getting the same issue intermitently.I actually don't mind it doing this in the background however I would like to be notified once it is done. Is this possible at all?
John_
I don't think it is possible to find out when the merging is complete without calling the blocking Close(true) method.
HakonB