0

I have noticed that while one of my VMs is writing to my lucene index, a query on my other VM stalls until the write.lock file is removed.

Surely an IndexSearcher object should be able to read from the index while and IndexWriter object is busy writing.

I'm using the IndexSearcher as follows:

        using (IndexSearcher searcher = new IndexSearcher(directory(indexName)))
        {
            int endIndex = skip + take;
            endIndex = endIndex > 0 ? endIndex : 1000;
            StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
            MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_30, searchFields, analyzer);
            Query query = parseQuery(searchQuery, parser);
            TopDocs results = searcher.Search(query, null, endIndex);
            ScoreDoc[] scoreDocs = results.ScoreDocs;
            //code to return the data

        }

I'm using the IndexWriter as follows:

        var analyzer = new StandardAnalyzer(Version.LUCENE_30);
        using (var writer = new IndexWriter(directory(_indexName), analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
        {
            // add data to lucene search index (replaces older entry if any)
            foreach (var item in articles) _addArticleToLuceneIndex(item, writer);

            // close handles
            analyzer.Close();
            writer.Dispose();
        }

The _addArticleToLuceneIndex method deletes the corresponding document and adds it anew:

private static void _addArticleToLuceneIndex(NewsRoomArticle article, IndexWriter writer)
{
        var searchQuery = new TermQuery(new Term("ArticleId", article.ArticleId.ToString()));

        writer.DeleteDocuments(searchQuery);
        doc.Add(new Field("ArticleId", article.ArticleId.IsNotEmpty() ? article.ArticleId.ToString() : "", Field.Store.YES, Field.Index.ANALYZED));
        //other fields
        // other fields
        writer.AddDocument(doc);
}

So the problem creeps in when one process writes a batch of records while another process is simultaneously carrying out a search process. The search is paused while the write is executed and continues when the write is completed.

Is there a way that my search process can read data without being interrupted by the writer?

Adam Hey
  • 1,512
  • 1
  • 20
  • 24

0 Answers0