public virtual void  TestFieldSelector()
        {
            RAMDirectory  ramDirectory1, ramDirectory2;
            IndexSearcher indexSearcher1, indexSearcher2;

            ramDirectory1 = new RAMDirectory();
            ramDirectory2 = new RAMDirectory();
            Query query = new TermQuery(new Term("contents", "doc0"));

            // Now put the documents in a different index
            InitIndex(ramDirectory1, 10, true, null); // documents with a single token "doc0", "doc1", etc...
            InitIndex(ramDirectory2, 10, true, "x");  // documents with two tokens "doc0" and "x", "doc1" and x, etc...

            indexSearcher1 = new IndexSearcher(ramDirectory1, true);
            indexSearcher2 = new IndexSearcher(ramDirectory2, true);

            MultiSearcher searcher = GetMultiSearcherInstance(new Searcher[] { indexSearcher1, indexSearcher2 });

            Assert.IsTrue(searcher != null, "searcher is null and it shouldn't be");
            ScoreDoc[] hits = searcher.Search(query, null, 1000).ScoreDocs;
            Assert.IsTrue(hits != null, "hits is null and it shouldn't be");
            Assert.IsTrue(hits.Length == 2, hits.Length + " does not equal: " + 2);
            Document document = searcher.Doc(hits[0].Doc);

            Assert.IsTrue(document != null, "document is null and it shouldn't be");
            Assert.IsTrue(document.GetFields().Count == 2, "document.getFields() Size: " + document.GetFields().Count + " is not: " + 2);
            //Should be one document from each directory
            //they both have two fields, contents and other
            ISet <string> ftl = Support.Compatibility.SetFactory.CreateHashSet <string>();

            ftl.Add("other");
            SetBasedFieldSelector fs = new SetBasedFieldSelector(ftl, Support.Compatibility.SetFactory.CreateHashSet <string>());

            document = searcher.Doc(hits[0].Doc, fs);
            Assert.IsTrue(document != null, "document is null and it shouldn't be");
            Assert.IsTrue(document.GetFields().Count == 1, "document.getFields() Size: " + document.GetFields().Count + " is not: " + 1);
            System.String value_Renamed = document.Get("contents");
            Assert.IsTrue(value_Renamed == null, "value is not null and it should be");
            value_Renamed = document.Get("other");
            Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be");
            ftl.Clear();
            ftl.Add("contents");
            fs            = new SetBasedFieldSelector(ftl, Support.Compatibility.SetFactory.CreateHashSet <string>());
            document      = searcher.Doc(hits[1].Doc, fs);
            value_Renamed = document.Get("contents");
            Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be");
            value_Renamed = document.Get("other");
            Assert.IsTrue(value_Renamed == null, "value is not null and it should be");
        }
Example #2
0
		public virtual void  TestFieldSelector()
		{
			RAMDirectory ramDirectory1, ramDirectory2;
			IndexSearcher indexSearcher1, indexSearcher2;
			
			ramDirectory1 = new RAMDirectory();
			ramDirectory2 = new RAMDirectory();
			Query query = new TermQuery(new Term("contents", "doc0"));
			
			// Now put the documents in a different index
			InitIndex(ramDirectory1, 10, true, null); // documents with a single token "doc0", "doc1", etc...
			InitIndex(ramDirectory2, 10, true, "x"); // documents with two tokens "doc0" and "x", "doc1" and x, etc...
			
			indexSearcher1 = new IndexSearcher(ramDirectory1);
			indexSearcher2 = new IndexSearcher(ramDirectory2);
			
			MultiSearcher searcher = GetMultiSearcherInstance(new Searcher[]{indexSearcher1, indexSearcher2});
			Assert.IsTrue(searcher != null, "searcher is null and it shouldn't be");
			ScoreDoc[] hits = searcher.Search(query, null, 1000).scoreDocs;
			Assert.IsTrue(hits != null, "hits is null and it shouldn't be");
			Assert.IsTrue(hits.Length == 2, hits.Length + " does not equal: " + 2);
			Document document = searcher.Doc(hits[0].doc);
			Assert.IsTrue(document != null, "document is null and it shouldn't be");
			Assert.IsTrue(document.GetFields().Count == 2, "document.getFields() Size: " + document.GetFields().Count + " is not: " + 2);
			//Should be one document from each directory
			//they both have two fields, contents and other
			System.Collections.Hashtable ftl = new System.Collections.Hashtable();
			SupportClass.CollectionsHelper.AddIfNotContains(ftl, "other");
			SetBasedFieldSelector fs = new SetBasedFieldSelector(ftl, (System.Collections.Hashtable) new System.Collections.Hashtable());
			document = searcher.Doc(hits[0].doc, fs);
			Assert.IsTrue(document != null, "document is null and it shouldn't be");
			Assert.IsTrue(document.GetFields().Count == 1, "document.getFields() Size: " + document.GetFields().Count + " is not: " + 1);
			System.String value_Renamed = document.Get("contents");
			Assert.IsTrue(value_Renamed == null, "value is not null and it should be");
			value_Renamed = document.Get("other");
			Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be");
			ftl.Clear();
			SupportClass.CollectionsHelper.AddIfNotContains(ftl, "contents");
			fs = new SetBasedFieldSelector(ftl, (System.Collections.Hashtable) new System.Collections.Hashtable());
			document = searcher.Doc(hits[1].doc, fs);
			value_Renamed = document.Get("contents");
			Assert.IsTrue(value_Renamed != null, "value is null and it shouldn't be");
			value_Renamed = document.Get("other");
			Assert.IsTrue(value_Renamed == null, "value is not null and it should be");
		}
Example #3
0
		public virtual void  TestBinaryFields()
		{
			Directory dir = new RAMDirectory();
			byte[] bin = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
			
			IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
			
			for (int i = 0; i < 10; i++)
			{
				AddDoc(writer, "document number " + (i + 1));
				AddDocumentWithFields(writer);
				AddDocumentWithDifferentFields(writer);
				AddDocumentWithTermVectorFields(writer);
			}
			writer.Close();
			writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.LIMITED);
			Document doc = new Document();
			doc.Add(new Field("bin1", bin, Field.Store.YES));
			doc.Add(new Field("bin2", bin, Field.Store.COMPRESS));
			doc.Add(new Field("junk", "junk text", Field.Store.NO, Field.Index.ANALYZED));
			writer.AddDocument(doc);
			writer.Close();
			IndexReader reader = IndexReader.Open(dir);
			doc = reader.Document(reader.MaxDoc() - 1);
			Field[] fields = doc.GetFields("bin1");
			Assert.IsNotNull(fields);
			Assert.AreEqual(1, fields.Length);
			Field b1 = fields[0];
			Assert.IsTrue(b1.IsBinary());
			byte[] data1 = b1.GetBinaryValue();
			Assert.AreEqual(bin.Length, b1.GetBinaryLength());
			for (int i = 0; i < bin.Length; i++)
			{
				Assert.AreEqual(bin[i], data1[i + b1.GetBinaryOffset()]);
			}
			fields = doc.GetFields("bin2");
			Assert.IsNotNull(fields);
			Assert.AreEqual(1, fields.Length);
			b1 = fields[0];
			Assert.IsTrue(b1.IsBinary());
			data1 = b1.GetBinaryValue();
			Assert.AreEqual(bin.Length, b1.GetBinaryLength());
			for (int i = 0; i < bin.Length; i++)
			{
				Assert.AreEqual(bin[i], data1[i + b1.GetBinaryOffset()]);
			}
			System.Collections.Hashtable lazyFields = new System.Collections.Hashtable();
			SupportClass.CollectionsHelper.AddIfNotContains(lazyFields, "bin1");
			FieldSelector sel = new SetBasedFieldSelector(new System.Collections.Hashtable(), lazyFields);
			doc = reader.Document(reader.MaxDoc() - 1, sel);
			Fieldable[] fieldables = doc.GetFieldables("bin1");
			Assert.IsNotNull(fieldables);
			Assert.AreEqual(1, fieldables.Length);
			Fieldable fb1 = fieldables[0];
			Assert.IsTrue(fb1.IsBinary());
			Assert.AreEqual(bin.Length, fb1.GetBinaryLength());
			data1 = fb1.GetBinaryValue();
			Assert.AreEqual(bin.Length, fb1.GetBinaryLength());
			for (int i = 0; i < bin.Length; i++)
			{
				Assert.AreEqual(bin[i], data1[i + fb1.GetBinaryOffset()]);
			}
			reader.Close();
			// force optimize
			
			
			writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.LIMITED);
			writer.Optimize();
			writer.Close();
			reader = IndexReader.Open(dir);
			doc = reader.Document(reader.MaxDoc() - 1);
			fields = doc.GetFields("bin1");
			Assert.IsNotNull(fields);
			Assert.AreEqual(1, fields.Length);
			b1 = fields[0];
			Assert.IsTrue(b1.IsBinary());
			data1 = b1.GetBinaryValue();
			Assert.AreEqual(bin.Length, b1.GetBinaryLength());
			for (int i = 0; i < bin.Length; i++)
			{
				Assert.AreEqual(bin[i], data1[i + b1.GetBinaryOffset()]);
			}
			fields = doc.GetFields("bin2");
			Assert.IsNotNull(fields);
			Assert.AreEqual(1, fields.Length);
			b1 = fields[0];
			Assert.IsTrue(b1.IsBinary());
			data1 = b1.GetBinaryValue();
			Assert.AreEqual(bin.Length, b1.GetBinaryLength());
			for (int i = 0; i < bin.Length; i++)
			{
				Assert.AreEqual(bin[i], data1[i + b1.GetBinaryOffset()]);
			}
			reader.Close();
		}