English 中文(简体)
Lucene.net intermitantly indexed documents not appearing in the
原标题:

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?

问题回答

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?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签