コード例 #1
0
ファイル: TestDocument.cs プロジェクト: yonder/mono
        public virtual void  TestRemoveForNewDocument()
        {
            Document doc = MakeDocumentWithFields();

            Assert.AreEqual(8, doc.fields.Count);
            doc.RemoveFields("keyword");
            Assert.AreEqual(6, doc.fields.Count);
            doc.RemoveFields("doesnotexists");       // removing non-existing fields is siltenlty ignored
            doc.RemoveFields("keyword");             // removing a Field more than once
            Assert.AreEqual(6, doc.fields.Count);
            doc.RemoveField("text");
            Assert.AreEqual(5, doc.fields.Count);
            doc.RemoveField("text");
            Assert.AreEqual(4, doc.fields.Count);
            doc.RemoveField("text");
            Assert.AreEqual(4, doc.fields.Count);
            doc.RemoveField("doesnotexists");             // removing non-existing fields is siltenlty ignored
            Assert.AreEqual(4, doc.fields.Count);
            doc.RemoveFields("unindexed");
            Assert.AreEqual(2, doc.fields.Count);
            doc.RemoveFields("unstored");
            Assert.AreEqual(0, doc.fields.Count);
            doc.RemoveFields("doesnotexists");             // removing non-existing fields is siltenlty ignored
            Assert.AreEqual(0, doc.fields.Count);
        }
コード例 #2
0
        public virtual void  TestUpdateDocument()
        {
            bool optimize = true;

            Directory   dir1   = new MockRAMDirectory();
            IndexWriter writer = new IndexWriter(dir1, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);

            // create the index
            CreateIndexNoClose(!optimize, "index1", writer);

            // writer.flush(false, true, true);

            // get a reader
            IndexReader r1 = writer.GetReader();

            Assert.IsTrue(r1.IsCurrent());

            System.String id10 = r1.Document(10).GetField("id").StringValue();

            Document newDoc = r1.Document(10);

            newDoc.RemoveField("id");
            newDoc.Add(new Field("id", System.Convert.ToString(8000), Field.Store.YES, Field.Index.NOT_ANALYZED));
            writer.UpdateDocument(new Term("id", id10), newDoc);
            Assert.IsFalse(r1.IsCurrent());

            IndexReader r2 = writer.GetReader();

            Assert.IsTrue(r2.IsCurrent());
            Assert.AreEqual(0, Count(new Term("id", id10), r2));
            Assert.AreEqual(1, Count(new Term("id", System.Convert.ToString(8000)), r2));

            r1.Close();
            writer.Close();
            Assert.IsTrue(r2.IsCurrent());

            IndexReader r3 = IndexReader.Open(dir1);

            Assert.IsTrue(r3.IsCurrent());
            Assert.IsTrue(r2.IsCurrent());
            Assert.AreEqual(0, Count(new Term("id", id10), r3));
            Assert.AreEqual(1, Count(new Term("id", System.Convert.ToString(8000)), r3));

            writer = new IndexWriter(dir1, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
            Document doc = new Document();

            doc.Add(new Field("field", "a b c", Field.Store.NO, Field.Index.ANALYZED));
            writer.AddDocument(doc);
            Assert.IsTrue(r2.IsCurrent());
            Assert.IsTrue(r3.IsCurrent());

            writer.Close();

            Assert.IsFalse(r2.IsCurrent());
            Assert.IsTrue(!r3.IsCurrent());

            r2.Close();
            r3.Close();

            dir1.Close();
        }
コード例 #3
0
		public virtual void  TestBinaryField()
		{
			Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
			Fieldable stringFld = new Field("string", binaryVal, Field.Store.YES, Field.Index.NO);
			Fieldable binaryFld = new Field("binary", (new System.Text.ASCIIEncoding()).GetBytes(binaryVal), Field.Store.YES);
			Fieldable binaryFld2 = new Field("binary", (new System.Text.ASCIIEncoding()).GetBytes(binaryVal2), Field.Store.YES);

			doc.Add(stringFld);
			doc.Add(binaryFld);

			Assert.AreEqual(2, doc.GetFieldsCount());
			
			Assert.IsTrue(binaryFld.IsBinary());
			Assert.IsTrue(binaryFld.IsStored());
			Assert.IsFalse(binaryFld.IsIndexed());
			Assert.IsFalse(binaryFld.IsTokenized());
			
			System.String binaryTest = (new System.Text.ASCIIEncoding()).GetString(doc.GetBinaryValue("binary"));
			Assert.IsTrue(binaryTest.Equals(binaryVal));
			
			System.String stringTest = doc.Get("string");
			Assert.IsTrue(binaryTest.Equals(stringTest));
			
			doc.Add(binaryFld2);
			
			Assert.AreEqual(3, doc.GetFieldsCount());
			
			byte[][] binaryTests = doc.GetBinaryValues("binary");
			
			Assert.AreEqual(2, binaryTests.Length);
			
			binaryTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(binaryTests[0]));
			System.String binaryTest2 = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(binaryTests[1]));
			
			Assert.IsFalse(binaryTest.Equals(binaryTest2));
			
			Assert.IsTrue(binaryTest.Equals(binaryVal));
			Assert.IsTrue(binaryTest2.Equals(binaryVal2));
			
			doc.RemoveField("string");
			Assert.AreEqual(2, doc.GetFieldsCount());
			
			doc.RemoveFields("binary");
			Assert.AreEqual(0, doc.GetFieldsCount());
		}