Lucene.Net.Index.IndexWriter.Dispose C# (CSharp) Method

Dispose() public method

Commits all changes to an index and closes all associated files. Note that this may be a costly operation, so, try to re-use a single writer instead of closing and opening a new one. See Commit() for caveats about write caching done by some IO devices.

If an Exception is hit during close, eg due to disk full or some other reason, then both the on-disk index and the internal state of the IndexWriter instance will be consistent. However, the close will not be complete even though part of it (flushing buffered documents) may have succeeded, so the write lock will still be held.

If you can correct the underlying cause (eg free up some disk space) then you can call close() again. Failing that, if you want to force the write lock to be released (dangerous, because you may then lose buffered docs in the IndexWriter instance) then you can do something like this:

try { writer.close(); } finally { if (IndexWriter.isLocked(directory)) { IndexWriter.unlock(directory); } } after which, you must be certain not to use the writer instance anymore.

NOTE: if this method hits an OutOfMemoryError you should immediately close the writer, again. See above for details.

public Dispose ( ) : void
return void
        public virtual void Dispose()
        {
            Dispose(true);
        }

Same methods

IndexWriter::Dispose ( bool waitForMerges ) : void
IndexWriter::Dispose ( bool disposing, bool waitForMerges ) : void

Usage Example

        public static void CreateIndexCreative(Creative entity, string IndexPath)
        {
            var document = new Document();
            document.Add(new Field("CreativeId", entity.Creativeid.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
            document.Add(new Field("Title", entity.Title, Field.Store.YES, Field.Index.ANALYZED));
            if (!string.IsNullOrEmpty(entity.About))
            {
                document.Add(new Field("About", entity.About, Field.Store.YES, Field.Index.ANALYZED));
            }

            Directory directory = FSDirectory.Open(new DirectoryInfo(IndexPath));
              Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);

               var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);

              try
              {

            writer.AddDocument(document);

            writer.Optimize();

            writer.Dispose();
              }
             finally
              {

              writer.Dispose();
              }
        }
All Usage Examples Of Lucene.Net.Index.IndexWriter::Dispose
IndexWriter