An IndexReader which reads multiple, parallel indexes. Each index added must have the same number of documents, but typically each contains different fields. Each document contains the union of the fields of all documents with the same document number. When searching, matches for a query term are from the first index added that has the field.

This is useful, e.g., with collections that have large fields which change rarely and small fields that change more frequently. The smaller fields may be re-indexed in a new index and both indexes may be searched together.

Warning: It is up to you to make sure all indexes are created and modified the same way. For example, if you add documents to one index, you need to add the same documents in the same order to the other indexes. Failure to do so will result in undefined behavior.

Inheritance: IndexReader
Beispiel #1
0
        public virtual void  TestIsCurrent()
        {
            Directory      dir1 = GetDir1();
            Directory      dir2 = GetDir2();
            ParallelReader pr   = new ParallelReader();

            pr.Add(IndexReader.Open(dir1));
            pr.Add(IndexReader.Open(dir2));

            Assert.IsTrue(pr.IsCurrent());
            IndexReader modifier = IndexReader.Open(dir1);

            modifier.SetNorm(0, "f1", 100);
            modifier.Close();

            // one of the two IndexReaders which ParallelReader is using
            // is not current anymore
            Assert.IsFalse(pr.IsCurrent());

            modifier = IndexReader.Open(dir2);
            modifier.SetNorm(0, "f3", 100);
            modifier.Close();

            // now both are not current anymore
            Assert.IsFalse(pr.IsCurrent());
        }
Beispiel #2
0
        public virtual void  TestIncompatibleIndexes()
        {
            // two documents:
            Directory dir1 = GetDir1();

            // one document only:
            Directory   dir2 = new MockRAMDirectory();
            IndexWriter w2   = new IndexWriter(dir2, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
            Document    d3   = new Document();

            d3.Add(new Field("f3", "v1", Field.Store.YES, Field.Index.ANALYZED));
            w2.AddDocument(d3);
            w2.Close();

            ParallelReader pr = new ParallelReader();

            pr.Add(IndexReader.Open(dir1));
            try
            {
                pr.Add(IndexReader.Open(dir2));
                Assert.Fail("didn't get exptected exception: indexes don't have same number of documents");
            }
            catch (System.ArgumentException e)
            {
                // expected exception
            }
        }
 public ParallelTermEnum(ParallelReader enclosingInstance)
 {
     InitBlock(enclosingInstance);
     field = ((System.String)Enclosing_Instance.fieldToReader.GetKey(0));
     if (field != null)
     {
         termEnum = ((IndexReader)Enclosing_Instance.fieldToReader[field]).Terms();
     }
 }
Beispiel #4
0
        // Fields 1 & 2 in one index, 3 & 4 in other, with ParallelReader:
        private Searcher Parallel()
        {
            Directory      dir1 = GetDir1();
            Directory      dir2 = GetDir2();
            ParallelReader pr   = new ParallelReader();

            pr.Add(IndexReader.Open(dir1));
            pr.Add(IndexReader.Open(dir2));
            return(new IndexSearcher(pr));
        }
            public ParallelTermEnum(ParallelReader enclosingInstance, Term term)
            {
                InitBlock(enclosingInstance);
                field = term.Field();
                IndexReader reader = ((IndexReader)Enclosing_Instance.fieldToReader[field]);

                if (reader != null)
                {
                    termEnum = reader.Terms(term);
                }
            }
Beispiel #6
0
            public ParallelTermEnum(ParallelReader enclosingInstance, Term term, IState state)
            {
                InitBlock(enclosingInstance);
                field = term.Field;
                IndexReader reader = Enclosing_Instance.fieldToReader[field];

                if (reader != null)
                {
                    termEnum = reader.Terms(term, state);
                }
            }
 public ParallelTermDocs(ParallelReader enclosingInstance, Term term)
 {
     InitBlock(enclosingInstance);
     if (term == null)
     {
         termDocs = (Enclosing_Instance.readers.Count == 0)?null:((IndexReader)Enclosing_Instance.readers[0]).TermDocs(null);
     }
     else
     {
         Seek(term);
     }
 }
Beispiel #8
0
		public virtual void  TestFieldNames()
		{
			Directory dir1 = GetDir1();
			Directory dir2 = GetDir2();
			ParallelReader pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			pr.Add(IndexReader.Open(dir2));
            System.Collections.Generic.ICollection<string> fieldNames = pr.GetFieldNames(IndexReader.FieldOption.ALL);
			Assert.AreEqual(4, fieldNames.Count);
			Assert.IsTrue(SupportClass.CollectionsHelper.Contains(fieldNames, "f1"));
			Assert.IsTrue(SupportClass.CollectionsHelper.Contains(fieldNames, "f2"));
			Assert.IsTrue(SupportClass.CollectionsHelper.Contains(fieldNames, "f3"));
			Assert.IsTrue(SupportClass.CollectionsHelper.Contains(fieldNames, "f4"));
		}
Beispiel #9
0
 public ParallelTermDocs(ParallelReader enclosingInstance, Term term, IState state)
 {
     InitBlock(enclosingInstance);
     if (term == null)
     {
         termDocs = (Enclosing_Instance.readers.Count == 0)
                        ? null
                        : Enclosing_Instance.readers[0].TermDocs(null, state);
     }
     else
     {
         Seek(term, state);
     }
 }
Beispiel #10
0
        public virtual void  TestIsOptimized()
        {
            Directory dir1 = GetDir1();
            Directory dir2 = GetDir2();

            // add another document to ensure that the indexes are not optimized
            IndexWriter modifier = new IndexWriter(dir1, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
            Document    d        = new Document();

            d.Add(new Field("f1", "v1", Field.Store.YES, Field.Index.ANALYZED));
            modifier.AddDocument(d);
            modifier.Close();

            modifier = new IndexWriter(dir2, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
            d        = new Document();
            d.Add(new Field("f2", "v2", Field.Store.YES, Field.Index.ANALYZED));
            modifier.AddDocument(d);
            modifier.Close();


            ParallelReader pr = new ParallelReader();

            pr.Add(IndexReader.Open(dir1));
            pr.Add(IndexReader.Open(dir2));
            Assert.IsFalse(pr.IsOptimized());
            pr.Close();

            modifier = new IndexWriter(dir1, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
            modifier.Optimize();
            modifier.Close();

            pr = new ParallelReader();
            pr.Add(IndexReader.Open(dir1));
            pr.Add(IndexReader.Open(dir2));
            // just one of the two indexes are optimized
            Assert.IsFalse(pr.IsOptimized());
            pr.Close();


            modifier = new IndexWriter(dir2, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
            modifier.Optimize();
            modifier.Close();

            pr = new ParallelReader();
            pr.Add(IndexReader.Open(dir1));
            pr.Add(IndexReader.Open(dir2));
            // now both indexes are optimized
            Assert.IsTrue(pr.IsOptimized());
            pr.Close();
        }
            public ParallelTermEnum(ParallelReader enclosingInstance)
            {
                InitBlock(enclosingInstance);
                IEnumerator <string> e = Enclosing_Instance.fieldToReader.Keys.GetEnumerator();

                if (e.MoveNext())
                {
                    field = e.Current;
                    if (field != null)
                    {
                        termEnum = Enclosing_Instance.fieldToReader[field].Terms();
                    }
                }
            }
Beispiel #12
0
        public virtual void  TestFieldNames()
        {
            Directory      dir1 = GetDir1();
            Directory      dir2 = GetDir2();
            ParallelReader pr   = new ParallelReader();

            pr.Add(IndexReader.Open(dir1));
            pr.Add(IndexReader.Open(dir2));
            System.Collections.Generic.ICollection <string> fieldNames = pr.GetFieldNames(IndexReader.FieldOption.ALL);
            Assert.AreEqual(4, fieldNames.Count);
            Assert.IsTrue(SupportClass.CollectionsHelper.Contains(fieldNames, "f1"));
            Assert.IsTrue(SupportClass.CollectionsHelper.Contains(fieldNames, "f2"));
            Assert.IsTrue(SupportClass.CollectionsHelper.Contains(fieldNames, "f3"));
            Assert.IsTrue(SupportClass.CollectionsHelper.Contains(fieldNames, "f4"));
        }
Beispiel #13
0
 public ParallelTermEnum(ParallelReader enclosingInstance, IState state)
 {
     InitBlock(enclosingInstance);
     try
     {
         field = Enclosing_Instance.fieldToReader.Keys.First();
     }
     catch (ArgumentOutOfRangeException)
     {
         // No fields, so keep field == null, termEnum == null
         return;
     }
     if (field != null)
     {
         termEnum = Enclosing_Instance.fieldToReader[field].Terms(state);
     }
 }
Beispiel #14
0
 public ParallelTermEnum(ParallelReader enclosingInstance)
 {
     InitBlock(enclosingInstance);
     try
     {
         field = ((System.String)Enclosing_Instance.fieldToReader.GetKey(0));
     }
     catch (ArgumentOutOfRangeException e)
     {
         // No fields, so keep field == null, termEnum == null
         return;
     }
     if (field != null)
     {
         termEnum = ((IndexReader)Enclosing_Instance.fieldToReader[field]).Terms();
     }
 }
Beispiel #15
0
        public virtual void  TestParallelReader()
        {
            Directory dir1 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir1, true);
            Directory dir2 = new MockRAMDirectory();

            TestIndexReaderReopen.CreateIndex(dir2, true);
            IndexReader r1 = IndexReader.Open(dir1, false);
            IndexReader r2 = IndexReader.Open(dir2, false);

            ParallelReader pr1 = new ParallelReader();

            pr1.Add(r1);
            pr1.Add(r2);

            PerformDefaultTests(pr1);
            pr1.Close();
            dir1.Close();
            dir2.Close();
        }
        public virtual void  TestIncompatibleIndexes()
        {
            // two documents:
            Directory dir1 = GetDir1();

            // one document only:
            Directory   dir2 = new MockRAMDirectory();
            IndexWriter w2   = new IndexWriter(dir2, new StandardAnalyzer(Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
            Document    d3   = new Document();

            d3.Add(new Field("f3", "v1", Field.Store.YES, Field.Index.ANALYZED));
            w2.AddDocument(d3);
            w2.Close();

            ParallelReader pr = new ParallelReader();

            pr.Add(IndexReader.Open(dir1, false));

            Assert.Throws <ArgumentException>(() => pr.Add(IndexReader.Open(dir2, false)),
                                              "didn't get exptected exception: indexes don't have same number of documents");
        }
Beispiel #17
0
        public virtual void  TestDocument()
        {
            Directory      dir1 = GetDir1();
            Directory      dir2 = GetDir2();
            ParallelReader pr   = new ParallelReader();

            pr.Add(IndexReader.Open(dir1));
            pr.Add(IndexReader.Open(dir2));

            Document doc11  = pr.Document(0, new MapFieldSelector(new System.String[] { "f1" }));
            Document doc24  = pr.Document(1, new MapFieldSelector(new System.Collections.ArrayList(new System.String[] { "f4" })));
            Document doc223 = pr.Document(1, new MapFieldSelector(new System.String[] { "f2", "f3" }));

            Assert.AreEqual(1, doc11.GetFields().Count);
            Assert.AreEqual(1, doc24.GetFields().Count);
            Assert.AreEqual(2, doc223.GetFields().Count);

            Assert.AreEqual("v1", doc11.Get("f1"));
            Assert.AreEqual("v2", doc24.Get("f4"));
            Assert.AreEqual("v2", doc223.Get("f2"));
            Assert.AreEqual("v2", doc223.Get("f3"));
        }
Beispiel #18
0
        public virtual void  TestAllTermDocs()
        {
            Directory      dir1 = GetDir1();
            Directory      dir2 = GetDir2();
            ParallelReader pr   = new ParallelReader();

            pr.Add(IndexReader.Open(dir1));
            pr.Add(IndexReader.Open(dir2));
            int      NUM_DOCS = 2;
            TermDocs td       = pr.TermDocs(null);

            for (int i = 0; i < NUM_DOCS; i++)
            {
                Assert.IsTrue(td.Next());
                Assert.AreEqual(i, td.Doc());
                Assert.AreEqual(1, td.Freq());
            }
            td.Close();
            pr.Close();
            dir1.Close();
            dir2.Close();
        }
Beispiel #19
0
        public virtual void  TestIncompatibleIndexes()
        {
            // two documents:
            Directory dir1 = GetDir1();
            
            // one document only:
            Directory dir2 = new MockRAMDirectory();
            IndexWriter w2 = new IndexWriter(dir2, new StandardAnalyzer(Util.Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
            Document d3 = new Document();
            d3.Add(new Field("f3", "v1", Field.Store.YES, Field.Index.ANALYZED));
            w2.AddDocument(d3);
            w2.Close();
            
            ParallelReader pr = new ParallelReader();
            pr.Add(IndexReader.Open(dir1, false));

            Assert.Throws<ArgumentException>(() => pr.Add(IndexReader.Open(dir2, false)),
                                                "didn't get exptected exception: indexes don't have same number of documents");
        }
			protected internal override IndexReader OpenReader()
			{
				ParallelReader pr = new ParallelReader();
				pr.Add(IndexReader.Open(dir3));
				pr.Add(IndexReader.Open(dir4));
				pr.Add(new FilterIndexReader(IndexReader.Open(dir3)));
				return pr;
			}
Beispiel #21
0
 public ParallelTermDocs(ParallelReader enclosingInstance)
 {
     InitBlock(enclosingInstance);
 }
			public ParallelTermEnum(ParallelReader enclosingInstance, Term term)
			{
				InitBlock(enclosingInstance);
				field = term.Field();
				termEnum = ((IndexReader) Enclosing_Instance.fieldToReader[field]).Terms(term);
			}
        public virtual void  Test1()
        {
            ParallelReader pr = new ParallelReader();

            pr.Add(ir1);
            pr.Add(ir2);

            TermDocs td = pr.TermDocs();

            TermEnum te = pr.Terms();

            Assert.IsTrue(te.Next());
            Assert.AreEqual("field1:brown", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field1:fox", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field1:jumps", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field1:quick", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field1:the", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field2:brown", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field2:fox", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field2:jumps", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field2:quick", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field2:the", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field3:dog", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field3:fox", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field3:jumps", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field3:lazy", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field3:over", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsTrue(te.Next());
            Assert.AreEqual("field3:the", te.Term().ToString());
            td.Seek(te.Term());
            Assert.IsTrue(td.Next());
            Assert.AreEqual(0, td.Doc());
            Assert.IsFalse(td.Next());
            Assert.IsFalse(te.Next());
        }
			protected internal override IndexReader OpenReader()
			{
				ParallelReader pr = new ParallelReader();
				pr.Add(IndexReader.Open(dir1));
				pr.Add(IndexReader.Open(dir2));
				MultiReader mr = new MultiReader(new IndexReader[]{IndexReader.Open(dir3), IndexReader.Open(dir4)});
				return new MultiReader(new IndexReader[]{pr, mr, IndexReader.Open(dir5)});
			}
			protected internal override IndexReader OpenReader()
			{
				ParallelReader pr = new ParallelReader();
				pr.Add(IndexReader.Open(dir3));
				pr.Add(IndexReader.Open(dir4));
				// Does not implement reopen, so
				// hits exception:
				pr.Add(new FilterIndexReader(IndexReader.Open(dir3)));
				return pr;
			}
Beispiel #26
0
			private void  InitBlock(ParallelReader enclosingInstance)
			{
				this.enclosingInstance = enclosingInstance;
			}
 public ParallelTermDocs(ParallelReader enclosingInstance, Term term)
 {
     InitBlock(enclosingInstance);
     Seek(term);
 }
			public ParallelTermDocs(ParallelReader enclosingInstance, Term term)
			{
				InitBlock(enclosingInstance);
				Seek(term);
			}
Beispiel #29
0
		public virtual void  TestIsCurrent()
		{
			Directory dir1 = GetDir1();
			Directory dir2 = GetDir2();
			ParallelReader pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			pr.Add(IndexReader.Open(dir2));
			
			Assert.IsTrue(pr.IsCurrent());
			IndexReader modifier = IndexReader.Open(dir1);
			modifier.SetNorm(0, "f1", 100);
			modifier.Close();
			
			// one of the two IndexReaders which ParallelReader is using
			// is not current anymore
			Assert.IsFalse(pr.IsCurrent());
			
			modifier = IndexReader.Open(dir2);
			modifier.SetNorm(0, "f3", 100);
			modifier.Close();
			
			// now both are not current anymore
			Assert.IsFalse(pr.IsCurrent());
		}
Beispiel #30
0
		public virtual void  TestIncompatibleIndexes()
		{
			// two documents:
			Directory dir1 = GetDir1();
			
			// one document only:
			Directory dir2 = new MockRAMDirectory();
			IndexWriter w2 = new IndexWriter(dir2, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
			Document d3 = new Document();
			d3.Add(new Field("f3", "v1", Field.Store.YES, Field.Index.ANALYZED));
			w2.AddDocument(d3);
			w2.Close();
			
			ParallelReader pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			try
			{
				pr.Add(IndexReader.Open(dir2));
				Assert.Fail("didn't get exptected exception: indexes don't have same number of documents");
			}
			catch (System.ArgumentException e)
			{
				// expected exception
			}
		}
		public virtual void  TestReferenceCountingParallelReader()
		{
			for (int mode = 0; mode <= 1; mode++)
			{
				Directory dir1 = new MockRAMDirectory();
				CreateIndex(dir1, false);
				Directory dir2 = new MockRAMDirectory();
				CreateIndex(dir2, true);
				
				IndexReader reader1 = IndexReader.Open(dir1);
				AssertRefCountEquals(1, reader1);
				
				ParallelReader parallelReader1 = new ParallelReader(mode == 0);
				parallelReader1.Add(reader1);
				IndexReader initReader2 = IndexReader.Open(dir2);
				parallelReader1.Add(initReader2);
				ModifyIndex(1, dir2);
				AssertRefCountEquals(1 + mode, reader1);
				
				IndexReader parallelReader2 = parallelReader1.Reopen();
				// index1 hasn't changed, so parallelReader2 should share reader1 now with multiReader1
				AssertRefCountEquals(2 + mode, reader1);
				
				ModifyIndex(0, dir1);
				ModifyIndex(0, dir2);
				IndexReader reader2 = reader1.Reopen();
				AssertRefCountEquals(2 + mode, reader1);
				
				if (mode == 1)
				{
					initReader2.Close();
				}
				
				ModifyIndex(4, dir1);
				IndexReader reader3 = reader2.Reopen();
				AssertRefCountEquals(2 + mode, reader1);
				AssertRefCountEquals(1, reader2);
				
				parallelReader1.Close();
				AssertRefCountEquals(1 + mode, reader1);
				
				parallelReader1.Close();
				AssertRefCountEquals(1 + mode, reader1);
				
				if (mode == 1)
				{
					initReader2.Close();
				}
				
				reader1.Close();
				AssertRefCountEquals(1, reader1);
				
				parallelReader2.Close();
				AssertRefCountEquals(0, reader1);
				
				parallelReader2.Close();
				AssertRefCountEquals(0, reader1);
				
				reader3.Close();
				AssertRefCountEquals(0, reader1);
				AssertReaderClosed(reader1, true, false);
				
				reader2.Close();
				AssertRefCountEquals(0, reader1);
				AssertReaderClosed(reader1, true, false);
				
				reader2.Close();
				AssertRefCountEquals(0, reader1);
				
				reader3.Close();
				AssertRefCountEquals(0, reader1);
				AssertReaderClosed(reader1, true, true);
				
				dir1.Close();
				dir2.Close();
			}
		}
Beispiel #32
0
			public ParallelTermEnum(ParallelReader enclosingInstance)
			{
				InitBlock(enclosingInstance);
				try
				{
					field = ((System.String) Enclosing_Instance.fieldToReader.GetKey(0));
				}
				catch (ArgumentOutOfRangeException e)
				{
					// No fields, so keep field == null, termEnum == null
					return;
				}
				if (field != null)
					termEnum = ((IndexReader) Enclosing_Instance.fieldToReader[field]).Terms();
			}
Beispiel #33
0
			public ParallelTermDocs(ParallelReader enclosingInstance)
			{
				InitBlock(enclosingInstance);
			}
Beispiel #34
0
		public virtual void  TestIsOptimized()
		{
			Directory dir1 = GetDir1();
			Directory dir2 = GetDir2();
			
			// add another document to ensure that the indexes are not optimized
			IndexWriter modifier = new IndexWriter(dir1, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
			Document d = new Document();
			d.Add(new Field("f1", "v1", Field.Store.YES, Field.Index.ANALYZED));
			modifier.AddDocument(d);
			modifier.Close();
			
			modifier = new IndexWriter(dir2, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
			d = new Document();
			d.Add(new Field("f2", "v2", Field.Store.YES, Field.Index.ANALYZED));
			modifier.AddDocument(d);
			modifier.Close();
			
			
			ParallelReader pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			pr.Add(IndexReader.Open(dir2));
			Assert.IsFalse(pr.IsOptimized());
			pr.Close();
			
			modifier = new IndexWriter(dir1, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
			modifier.Optimize();
			modifier.Close();
			
			pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			pr.Add(IndexReader.Open(dir2));
			// just one of the two indexes are optimized
			Assert.IsFalse(pr.IsOptimized());
			pr.Close();
			
			
			modifier = new IndexWriter(dir2, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
			modifier.Optimize();
			modifier.Close();
			
			pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			pr.Add(IndexReader.Open(dir2));
			// now both indexes are optimized
			Assert.IsTrue(pr.IsOptimized());
			pr.Close();
		}
			protected internal override IndexReader OpenReader()
			{
				ParallelReader pr = new ParallelReader();
				pr.Add(IndexReader.Open(dir1));
				pr.Add(IndexReader.Open(dir2));
				return pr;
			}
Beispiel #36
0
		protected internal virtual IndexReader DoReopen(bool doClone)
		{
			EnsureOpen();
			
			bool reopened = false;
			System.Collections.IList newReaders = new System.Collections.ArrayList();
			
			bool success = false;
			
			try
			{
				for (int i = 0; i < readers.Count; i++)
				{
					IndexReader oldReader = (IndexReader) readers[i];
					IndexReader newReader = null;
					if (doClone)
					{
						newReader = (IndexReader) oldReader.Clone();
					}
					else
					{
						newReader = oldReader.Reopen();
					}
					newReaders.Add(newReader);
					// if at least one of the subreaders was updated we remember that
					// and return a new ParallelReader
					if (newReader != oldReader)
					{
						reopened = true;
					}
				}
				success = true;
			}
			finally
			{
				if (!success && reopened)
				{
					for (int i = 0; i < newReaders.Count; i++)
					{
						IndexReader r = (IndexReader) newReaders[i];
						if (r != readers[i])
						{
							try
							{
								r.Close();
							}
							catch (System.IO.IOException ignore)
							{
								// keep going - we want to clean up as much as possible
							}
						}
					}
				}
			}
			
			if (reopened)
			{
				System.Collections.IList newDecrefOnClose = new System.Collections.ArrayList();
				ParallelReader pr = new ParallelReader();
				for (int i = 0; i < readers.Count; i++)
				{
					IndexReader oldReader = (IndexReader) readers[i];
					IndexReader newReader = (IndexReader) newReaders[i];
					if (newReader == oldReader)
					{
						newDecrefOnClose.Add(true);
						newReader.IncRef();
					}
					else
					{
						// this is a new subreader instance, so on close() we don't
						// decRef but close it 
						newDecrefOnClose.Add(false);
					}
					pr.Add(newReader, !storedFieldReaders.Contains(oldReader));
				}
				pr.decrefOnClose = newDecrefOnClose;
				pr.incRefReaders = incRefReaders;
				return pr;
			}
			else
			{
				// No subreader was refreshed
				return this;
			}
		}
Beispiel #37
0
		public virtual void  TestAllTermDocs()
		{
			Directory dir1 = GetDir1();
			Directory dir2 = GetDir2();
			ParallelReader pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			pr.Add(IndexReader.Open(dir2));
			int NUM_DOCS = 2;
			TermDocs td = pr.TermDocs(null);
			for (int i = 0; i < NUM_DOCS; i++)
			{
				Assert.IsTrue(td.Next());
				Assert.AreEqual(i, td.Doc());
				Assert.AreEqual(1, td.Freq());
			}
			td.Close();
			pr.Close();
			dir1.Close();
			dir2.Close();
		}
Beispiel #38
0
			public ParallelTermEnum(ParallelReader enclosingInstance, Term term)
			{
				InitBlock(enclosingInstance);
				field = term.Field();
				IndexReader reader = ((IndexReader) Enclosing_Instance.fieldToReader[field]);
				if (reader != null)
					termEnum = reader.Terms(term);
			}
Beispiel #39
0
		// Fields 1 & 2 in one index, 3 & 4 in other, with ParallelReader:
		private Searcher Parallel()
		{
			Directory dir1 = GetDir1();
			Directory dir2 = GetDir2();
			ParallelReader pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			pr.Add(IndexReader.Open(dir2));
			return new IndexSearcher(pr);
		}
Beispiel #40
0
			public ParallelTermDocs(ParallelReader enclosingInstance, Term term)
			{
				InitBlock(enclosingInstance);
				if (term == null)
					termDocs = (Enclosing_Instance.readers.Count == 0)?null:((IndexReader) Enclosing_Instance.readers[0]).TermDocs(null);
				else
					Seek(term);
			}
Beispiel #41
0
 private void  InitBlock(ParallelReader enclosingInstance)
 {
     this.enclosingInstance = enclosingInstance;
 }
Beispiel #42
0
			public ParallelTermPositions(ParallelReader enclosingInstance, Term term):base(enclosingInstance)
			{
				InitBlock(enclosingInstance);
				Seek(term);
			}
			public ParallelTermEnum(ParallelReader enclosingInstance)
			{
				InitBlock(enclosingInstance);
				try
				{
					field = Enclosing_Instance.fieldToReader.Keys.First();
				}
				catch (ArgumentOutOfRangeException)
				{
					// No fields, so keep field == null, termEnum == null
					return;
				}
				if (field != null)
					termEnum = Enclosing_Instance.fieldToReader[field].Terms();
			}
Beispiel #44
0
        protected internal virtual IndexReader DoReopen(bool doClone)
        {
            EnsureOpen();

            bool reopened = false;

            System.Collections.IList newReaders = new System.Collections.ArrayList();

            bool success = false;

            try
            {
                for (int i = 0; i < readers.Count; i++)
                {
                    IndexReader oldReader = (IndexReader)readers[i];
                    IndexReader newReader = null;
                    if (doClone)
                    {
                        newReader = (IndexReader)oldReader.Clone();
                    }
                    else
                    {
                        newReader = oldReader.Reopen();
                    }
                    newReaders.Add(newReader);
                    // if at least one of the subreaders was updated we remember that
                    // and return a new ParallelReader
                    if (newReader != oldReader)
                    {
                        reopened = true;
                    }
                }
                success = true;
            }
            finally
            {
                if (!success && reopened)
                {
                    for (int i = 0; i < newReaders.Count; i++)
                    {
                        IndexReader r = (IndexReader)newReaders[i];
                        if (r != readers[i])
                        {
                            try
                            {
                                r.Close();
                            }
                            catch (System.IO.IOException ignore)
                            {
                                // keep going - we want to clean up as much as possible
                            }
                        }
                    }
                }
            }

            if (reopened)
            {
                System.Collections.IList newDecrefOnClose = new System.Collections.ArrayList();
                ParallelReader           pr = new ParallelReader();
                for (int i = 0; i < readers.Count; i++)
                {
                    IndexReader oldReader = (IndexReader)readers[i];
                    IndexReader newReader = (IndexReader)newReaders[i];
                    if (newReader == oldReader)
                    {
                        newDecrefOnClose.Add(true);
                        newReader.IncRef();
                    }
                    else
                    {
                        // this is a new subreader instance, so on close() we don't
                        // decRef but close it
                        newDecrefOnClose.Add(false);
                    }
                    pr.Add(newReader, !storedFieldReaders.Contains(oldReader));
                }
                pr.decrefOnClose = newDecrefOnClose;
                pr.incRefReaders = incRefReaders;
                return(pr);
            }
            else
            {
                // No subreader was refreshed
                return(this);
            }
        }
		public virtual void  TestParallelReader()
		{
			Directory dir1 = new MockRAMDirectory();
			TestIndexReaderReopen.CreateIndex(dir1, true);
			Directory dir2 = new MockRAMDirectory();
			TestIndexReaderReopen.CreateIndex(dir2, true);
			IndexReader r1 = IndexReader.Open(dir1, false);
            IndexReader r2 = IndexReader.Open(dir2, false);
			
			ParallelReader pr1 = new ParallelReader();
			pr1.Add(r1);
			pr1.Add(r2);
			
			PerformDefaultTests(pr1);
			pr1.Close();
			dir1.Close();
			dir2.Close();
		}
 public ParallelTermEnum(ParallelReader enclosingInstance, Term term)
 {
     InitBlock(enclosingInstance);
     field    = term.Field();
     termEnum = ((IndexReader)Enclosing_Instance.fieldToReader[field]).Terms(term);
 }
		public virtual void  Test1()
		{
			ParallelReader pr = new ParallelReader();
			pr.Add(ir1);
			pr.Add(ir2);
			
			TermDocs td = pr.TermDocs();
			
			TermEnum te = pr.Terms();
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field1:brown", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field1:fox", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field1:jumps", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field1:quick", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field1:the", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field2:brown", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field2:fox", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field2:jumps", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field2:quick", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field2:the", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field3:dog", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field3:fox", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field3:jumps", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field3:lazy", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field3:over", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsTrue(te.Next());
			Assert.AreEqual("field3:the", te.Term.ToString());
			td.Seek(te.Term);
			Assert.IsTrue(td.Next());
			Assert.AreEqual(0, td.Doc);
			Assert.IsFalse(td.Next());
			Assert.IsFalse(te.Next());
		}
Beispiel #48
0
		public virtual void  TestDocument()
		{
			Directory dir1 = GetDir1();
			Directory dir2 = GetDir2();
			ParallelReader pr = new ParallelReader();
			pr.Add(IndexReader.Open(dir1));
			pr.Add(IndexReader.Open(dir2));
			
			Document doc11 = pr.Document(0, new MapFieldSelector(new System.String[]{"f1"}));
			Document doc24 = pr.Document(1, new MapFieldSelector(new System.Collections.ArrayList(new System.String[]{"f4"})));
			Document doc223 = pr.Document(1, new MapFieldSelector(new System.String[]{"f2", "f3"}));
			
			Assert.AreEqual(1, doc11.GetFields().Count);
			Assert.AreEqual(1, doc24.GetFields().Count);
			Assert.AreEqual(2, doc223.GetFields().Count);
			
			Assert.AreEqual("v1", doc11.Get("f1"));
			Assert.AreEqual("v2", doc24.Get("f4"));
			Assert.AreEqual("v2", doc223.Get("f2"));
			Assert.AreEqual("v2", doc223.Get("f3"));
		}
			public ParallelTermEnum(ParallelReader enclosingInstance)
			{
				InitBlock(enclosingInstance);
				field = ((System.String) Enclosing_Instance.fieldToReader.GetKey(0));
				if (field != null)
					termEnum = ((IndexReader) Enclosing_Instance.fieldToReader[field]).Terms();
			}
Beispiel #50
0
 public ParallelTermPositions(ParallelReader enclosingInstance, Term term) : base(enclosingInstance)
 {
     InitBlock(enclosingInstance);
     Seek(term);
 }
 public ParallelTermEnum(ParallelReader enclosingInstance)
 {
     InitBlock(enclosingInstance);
     IEnumerator<string> e = Enclosing_Instance.fieldToReader.Keys.GetEnumerator();
     if (e.MoveNext())
     {
         field = e.Current;
         if (field != null)
             termEnum = Enclosing_Instance.fieldToReader[field].Terms();
     }
 }