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

Optimize() public method

Just like Optimize(int), except you can specify whether the call should block until the optimize completes. This is only meaningful with a MergeScheduler that is able to run merges in background threads.

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

public Optimize ( int maxNumSegments, bool doWait ) : void
maxNumSegments int
doWait bool
return void
		public virtual void  Optimize(int maxNumSegments, bool doWait)
		{
			EnsureOpen();
			
			if (maxNumSegments < 1)
				throw new System.ArgumentException("maxNumSegments must be >= 1; got " + maxNumSegments);
			
			if (infoStream != null)
				Message("optimize: index now " + SegString());
			
			Flush(true, false, true);
			
			lock (this)
			{
				ResetMergeExceptions();
                segmentsToOptimize = Lucene.Net.Support.Compatibility.SetFactory.GetSet<SegmentInfo>();
                optimizeMaxNumSegments = maxNumSegments;
				int numSegments = segmentInfos.Count;
				for (int i = 0; i < numSegments; i++)
                    segmentsToOptimize.Add(segmentInfos.Info(i));
				
				// Now mark all pending & running merges as optimize
				// merge:
				foreach(MergePolicy.OneMerge merge in pendingMerges)
				{
					merge.optimize = true;
					merge.maxNumSegmentsOptimize = maxNumSegments;
				}
				
				foreach(MergePolicy.OneMerge merge in runningMerges)
				{
					merge.optimize = true;
					merge.maxNumSegmentsOptimize = maxNumSegments;
				}
			}
			
			MaybeMerge(maxNumSegments, true);
			
			if (doWait)
			{
				lock (this)
				{
					while (true)
					{
						
						if (hitOOM)
						{
							throw new System.SystemException("this writer hit an OutOfMemoryError; cannot complete optimize");
						}
						
						if (mergeExceptions.Count > 0)
						{
							// Forward any exceptions in background merge
							// threads to the current thread:
							int size = mergeExceptions.Count;
							for (int i = 0; i < size; i++)
							{
								MergePolicy.OneMerge merge = mergeExceptions[i];
								if (merge.optimize)
								{
                                    System.IO.IOException err;
									System.Exception t = merge.GetException();
                                    if (t != null)
									    err = new System.IO.IOException("background merge hit exception: " + merge.SegString(directory), t);
                                    else
                                        err = new System.IO.IOException("background merge hit exception: " + merge.SegString(directory));
									throw err;
								}
							}
						}
						
						if (OptimizeMergesPending())
							DoWait();
						else
							break;
					}
				}
				
				// If close is called while we are still
				// running, throw an exception so the calling
				// thread will know the optimize did not
				// complete
				EnsureOpen();
			}
			
			// NOTE: in the ConcurrentMergeScheduler case, when
			// doWait is false, we can return immediately while
			// background threads accomplish the optimization
		}
		

Same methods

IndexWriter::Optimize ( ) : void
IndexWriter::Optimize ( bool doWait ) : void
IndexWriter::Optimize ( int maxNumSegments ) : void

Usage Example

Ejemplo n.º 1
1
        public void SetUp()
        {

            var writer = new IndexWriter(store, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);

            var doc = new Document();
            doc.Add(new Field("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
            writer.AddDocument(doc);

            doc = new Document();
            doc.Add(new Field("aaa", "foo", Field.Store.YES, Field.Index.ANALYZED));
            writer.AddDocument(doc);

            doc = new Document();
            doc.Add(new Field("contents", "Tom", Field.Store.YES, Field.Index.ANALYZED));
            writer.AddDocument(doc);

            doc = new Document();
            doc.Add(new Field("contents", "Jerry", Field.Store.YES, Field.Index.ANALYZED));
            writer.AddDocument(doc);

            doc = new Document();
            doc.Add(new Field("zzz", "bar", Field.Store.YES, Field.Index.ANALYZED));
            writer.AddDocument(doc);

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