title: Singleton pattern for IndexWriter and IndexSearcher Lucene.net
body: <p>I'm using <strong>Lucene.net 3.0.3.0</strong>(the latest version right now). I would like to know if using a single instance of <strong>IndexWriter</strong> and <strong>IndexSearcher</strong> is a good choice in production environment(considering thread safety). I read in the documentation that creating new objects of these instances is an expensive operation and internally lucene handles concurrent requests pretty well. </p>
  
  <p>This is a question to the people who have used lucene.net in production environment; please let me know if this will work out!</p>
  
  <pre><code>    using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using Lucene.Net.Search;
      using Lucene.Net.Store;
      using System.IO;
  
      using Directory = Lucene.Net.Store.Directory;
      using Version = Lucene.Net.Util.Version;
      using Lucene.Net.Index;
      using Lucene.Net.Analysis.Standard;
      using Lucene.Net.Analysis;
  
      namespace MXLuceneConsoleApp
      {
          /// &lt;summary&gt;
          /// This helper class applies a singleton pattern to create Searcher and Writer objects as it's recommended to create them only once.
          /// Also the searcher gets reinitialized, if any write happens.    
          /// &lt;/summary&gt;
          public class MXLuceneIndexHelper
          {
              private static IndexSearcher _searcher;        
              private static Directory _directory;
              private static Lazy&lt;IndexWriter&gt; _writer = new Lazy&lt;IndexWriter&gt;(() =&gt; new IndexWriter(_directory, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED));
  
              //this private constructor makes it a singleton now.
              private MXLuceneIndexHelper() { }
  
              //Static constructor, opening the directory once for all.
              static MXLuceneIndexHelper()
              {
                  _directory = FSDirectory.Open(new DirectoryInfo(Environment.CurrentDirectory + "\\LuceneIndexDir"));
              }
  
              public static IndexSearcher IndexSearcher
              {
                  get
                  {
                      if (_searcher == null)
                      {
                          InitializeSearcher();
                      }
                      else if (!_searcher.IndexReader.IsCurrent())
                      {                    
                          //_searcher.IndexReader.Reopen(); 
                          //refreshing the underlying Reader above doesn't do the trick, so I'm reinitializing the Searcher.
                          _searcher.Dispose();
                          InitializeSearcher();
                      }
  
                      return _searcher;
                  }
              }
  
              public static IndexWriter IndexWriter
              {
                  get 
                  {                
                      //_writer.SetRAMBufferSizeMB(30.0);
                      return _writer.Value; 
                  }
              }
  
              private static void InitializeSearcher()
              {
                  _searcher = new IndexSearcher(_directory, false);
  
              }
          }//End of class
      }
  </code></pre>