title: Updating Lucene index on the fly
body: <p>I am an absolute newbie in Lucene and got a problem updating the index. </p>
  
  <p>Currently I can rebuild the entire index daily but the index is only updated to the time of built, but how can I update the index like appending it so it is up to date? Currently there are code trying to update the index but it only updates the segment files and not other files.  </p>
  
  <p>Every time when an entry is added from my website, it will run RefreshFromDatabase method and try to add the latest index, however in the search index folder, it will update the two files segments.gen and segments_t, however all other files (.fdt .fdx .fnm .frq .nrm .prx .tii .tis .del .cfs) are not updated. 
  Here is the screenshot: <a href="http://i.stack.imgur.com/0olOE.jpg" rel="nofollow">folder screenshot</a></p>
  
  <p>code:</p>
  
  <pre><code>using (ISiteScope scope = _scopeFactory.GetSiteScope(site)){
          scope.Get&lt;ISearchIndexUpdater&gt;().RefreshFromDatabase(primaryId, secondaryId);
          scope.Commit();
  }
  
  public void RefreshFromDatabase(long primaryId, int? secondaryId){
      Process process = _processRepo.GetById(primaryId);
      IList&lt;Decision&gt; allDecisions = _decisionRepo.GetByProcess(process);
      IList&lt;Link&gt; allLinks = _linkRepo.GetActiveByProcess(process);
      Decision current = allDecisions.OrderByDescending(x =&gt; x.DTG).FirstOrDefault();
      _luceneRepository.Add(process, allDecisions, allLinks);
  }
  
  public void Add(Process process, IList&lt;Decision&gt; decisions, IList&lt;Link&gt; links){
      if (null == decisions)
      decisions = new List&lt;Decision&gt;();
  
      using (LuceneWriter writer = BeginWriter(false)) {
          Add(writer.Writer,
              new SearchIndexProcess {
                  // properties
              },
              decisions.Select(x =&gt; new SearchIndexDecision {
                  // params
              }).ToArray(),
              (links ?? new List&lt;Link&gt;()).Select(x =&gt; new SearchIndexLink {
                  // properties
              }).ToArray()
          );
          writer.Commit();
      }
  }
  </code></pre>
  
  <p>and LuceneWriter class: </p>
  
  <pre><code>public class LuceneWriter : IDisposable
         {
                Directory _directory;
                Analyzer _analyzer;
                IndexWriter _indexWriter;
  
                bool _commit;
                bool _optimise;
  
                /// &lt;summary&gt;
                /// Constructor for LuceneWriter.
                /// &lt;/summary&gt;
                /// &lt;param name="fileSystem"&gt;An IFileSystem.&lt;/param&gt;
                /// &lt;param name="luceneDir"&gt;The directory that contains the Lucene index. Need not exist.&lt;/param&gt;
                public LuceneWriter(IFileSystem fileSystem, string luceneDir)
                       : this(fileSystem, luceneDir, false)
                {
                }
  
                /// &lt;summary&gt;
                /// Constructor for LuceneWriter.
                /// &lt;/summary&gt;
                /// &lt;param name="fileSystem"&gt;An IFileSystem.&lt;/param&gt;
                /// &lt;param name="luceneDir"&gt;The directory that contains the Lucene index. Need not exist.&lt;/param&gt;
                /// &lt;param name="optimiseWhenDone"&gt;Optimse the index on Dispose(). This is an expensive operation.&lt;/param&gt;
                public LuceneWriter(IFileSystem fileSystem, string luceneDir, bool optimiseWhenDone)
                {
                       Init(fileSystem, luceneDir, optimiseWhenDone);
                }
  
                //init has its own single use method for mocking reasons.
                /// &lt;summary&gt;
                /// Initialise the LuceneWriter.
                /// &lt;/summary&gt;
                /// &lt;param name="fileSystem"&gt;An IFileSystem.&lt;/param&gt;
                /// &lt;param name="luceneDir"&gt;The directory containing the Lucene index.&lt;/param&gt;
                /// &lt;param name="optimiseWhenDone"&gt;Whether or not to optimise the Lucene index upon Dispose().&lt;/param&gt;
                protected virtual void Init(IFileSystem fileSystem, string luceneDir, bool optimiseWhenDone)
                {
                       _optimise = optimiseWhenDone;
  
                       bool exists = true;
                       if (!fileSystem.DirectoryExists(luceneDir)) {
                             fileSystem.CreateDirectory(luceneDir);
                             exists = false;
                       }
  
                       _directory = FSDirectory.Open(new DirectoryInfo(luceneDir));
                       _analyzer = new StandardAnalyzer(Version.LUCENE_30);
                       _indexWriter = new IndexWriter(_directory, _analyzer, !exists, IndexWriter.MaxFieldLength.UNLIMITED);
                }
  
                /// &lt;summary&gt;
                /// Flags writer to commit and optimise. Does not commit until Dispose() is called.
                /// &lt;/summary&gt;
                public void Commit()
                {
                       _commit = true;
                }
  
                /// &lt;summary&gt;
                /// The IndexWriter.
                /// &lt;/summary&gt;
                public IndexWriter Writer { get { return _indexWriter; } }
  
                /// &lt;summary&gt;
                /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
                /// &lt;/summary&gt;
                /// &lt;filterpriority&gt;2&lt;/filterpriority&gt;
                public void Dispose()
                {
                       if ((null != _indexWriter) &amp;&amp; (_commit)) {
                             if (_optimise)
                                    _indexWriter.Optimize(true);
                             _indexWriter.Commit();
                             _indexWriter.Close(true);
                       }
  
                       if (null != _indexWriter)
                             _indexWriter.Dispose();
                       if (null != _analyzer)
                             _analyzer.Dispose();
                       if (null != _directory) {
                             _directory.Close();
                             _directory.Dispose();
                       }
                }
         }
  </code></pre>