Esempio n. 1
0
        public virtual void  TestRemoveForNewDocument()
        {
            Document doc = MakeDocumentWithFields();

            Assert.AreEqual(8, doc.fields_ForNUnit.Count);
            doc.RemoveFields("keyword");
            Assert.AreEqual(6, doc.fields_ForNUnit.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_ForNUnit.Count);
            doc.RemoveField("text");
            Assert.AreEqual(5, doc.fields_ForNUnit.Count);
            doc.RemoveField("text");
            Assert.AreEqual(4, doc.fields_ForNUnit.Count);
            doc.RemoveField("text");
            Assert.AreEqual(4, doc.fields_ForNUnit.Count);
            doc.RemoveField("doesnotexists");             // removing non-existing fields is siltenlty ignored
            Assert.AreEqual(4, doc.fields_ForNUnit.Count);
            doc.RemoveFields("unindexed");
            Assert.AreEqual(2, doc.fields_ForNUnit.Count);
            doc.RemoveFields("unstored");
            Assert.AreEqual(0, doc.fields_ForNUnit.Count);
            doc.RemoveFields("doesnotexists");             // removing non-existing fields is siltenlty ignored
            Assert.AreEqual(0, doc.fields_ForNUnit.Count);
        }
Esempio n. 2
0
        public virtual void  TestBinaryField()
        {
            Document  doc        = new Document();
            Fieldable stringFld  = new Field("string", binaryVal, Field.Store.YES, Field.Index.NO);
            Fieldable binaryFld  = new Field("binary", System.Text.UTF8Encoding.UTF8.GetBytes(binaryVal), Field.Store.YES);
            Fieldable binaryFld2 = new Field("binary", System.Text.UTF8Encoding.UTF8.GetBytes(binaryVal2), Field.Store.YES);

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

            Assert.AreEqual(2, doc.fields_ForNUnit.Count);

            Assert.IsTrue(binaryFld.IsBinary());
            Assert.IsTrue(binaryFld.IsStored());
            Assert.IsFalse(binaryFld.IsIndexed());
            Assert.IsFalse(binaryFld.IsTokenized());

            System.String binaryTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(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.fields_ForNUnit.Count);

            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.fields_ForNUnit.Count);

            doc.RemoveFields("binary");
            Assert.AreEqual(0, doc.fields_ForNUnit.Count);
        }
Esempio n. 3
0
		public virtual void  TestBinaryField()
		{
			Document doc = new Document();
			IFieldable stringFld = new Field("string", binaryVal, Field.Store.YES, Field.Index.NO);
			IFieldable binaryFld = new Field("binary", System.Text.UTF8Encoding.UTF8.GetBytes(binaryVal), Field.Store.YES);
			IFieldable binaryFld2 = new Field("binary", System.Text.UTF8Encoding.UTF8.GetBytes(binaryVal2), Field.Store.YES);
			
			doc.Add(stringFld);
			doc.Add(binaryFld);
			
			Assert.AreEqual(2, doc.fields_ForNUnit.Count);
			
			Assert.IsTrue(binaryFld.IsBinary);
			Assert.IsTrue(binaryFld.IsStored);
			Assert.IsFalse(binaryFld.IsIndexed);
			Assert.IsFalse(binaryFld.IsTokenized);
			
			System.String binaryTest = new System.String(System.Text.UTF8Encoding.UTF8.GetChars(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.fields_ForNUnit.Count);
			
			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.fields_ForNUnit.Count);
			
			doc.RemoveFields("binary");
			Assert.AreEqual(0, doc.fields_ForNUnit.Count);
		}
Esempio n. 4
0
        public void Should_Throw_SearchException_When_Field_Is_Missing(string fieldName)
        {
            // Arrange
            LuceneDocument document = new LuceneDocument();

            document.Add(CreateField("id", "123"));
            document.Add(CreateField("title", "the title"));
            document.Add(CreateField("contentsummary", "the summary"));
            document.Add(CreateField("tags", "tag1 tag2"));
            document.Add(CreateField("createdby", "gandhi"));
            document.Add(CreateField("contentlength", "999"));
            document.Add(CreateField("createdon", DateTime.Today.ToString()));

            document.RemoveField(fieldName);

            ScoreDoc scoreDoc = new ScoreDoc(0, 1f);

            // Act + Assert
            SearchResultViewModel model = new SearchResultViewModel(document, scoreDoc);
        }
        public virtual void TestSegmentMerges()
        {
            Directory dir = NewDirectory();
            Random random = Random();
            IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random));
            IndexWriter writer = new IndexWriter(dir, (IndexWriterConfig)conf.Clone());

            int docid = 0;
            int numRounds = AtLeast(10);
            for (int rnd = 0; rnd < numRounds; rnd++)
            {
                Document doc = new Document();
                doc.Add(new StringField("key", "doc", Store.NO));
                doc.Add(new BinaryDocValuesField("bdv", ToBytes(-1)));
                int numDocs = AtLeast(30);
                for (int i = 0; i < numDocs; i++)
                {
                    doc.RemoveField("id");
                    doc.Add(new StringField("id", Convert.ToString(docid++), Store.NO));
                    writer.AddDocument(doc);
                }

                long value = rnd + 1;
                writer.UpdateBinaryDocValue(new Term("key", "doc"), "bdv", ToBytes(value));

                if (random.NextDouble() < 0.2) // randomly delete some docs
                {
                    writer.DeleteDocuments(new Term("id", Convert.ToString(random.Next(docid))));
                }

                // randomly commit or reopen-IW (or nothing), before forceMerge
                if (random.NextDouble() < 0.4)
                {
                    writer.Commit();
                }
                else if (random.NextDouble() < 0.1)
                {
                    writer.Dispose();
                    writer = new IndexWriter(dir, (IndexWriterConfig)conf.Clone());
                }

                // add another document with the current value, to be sure forceMerge has
                // something to merge (for instance, it could be that CMS finished merging
                // all segments down to 1 before the delete was applied, so when
                // forceMerge is called, the index will be with one segment and deletes
                // and some MPs might now merge it, thereby invalidating test's
                // assumption that the reader has no deletes).
                doc = new Document();
                doc.Add(new StringField("id", Convert.ToString(docid++), Store.NO));
                doc.Add(new StringField("key", "doc", Store.NO));
                doc.Add(new BinaryDocValuesField("bdv", ToBytes(value)));
                writer.AddDocument(doc);

                writer.ForceMerge(1, true);
                DirectoryReader reader;
                if (random.NextBoolean())
                {
                    writer.Commit();
                    reader = DirectoryReader.Open(dir);
                }
                else
                {
                    reader = DirectoryReader.Open(writer, true);
                }

                Assert.AreEqual(1, reader.Leaves.Count);
                AtomicReader r = (AtomicReader)reader.Leaves[0].Reader;
                Assert.IsNull(r.LiveDocs, "index should have no deletes after forceMerge");
                BinaryDocValues bdv = r.GetBinaryDocValues("bdv");
                Assert.IsNotNull(bdv);
                BytesRef scratch = new BytesRef();
                for (int i = 0; i < r.MaxDoc; i++)
                {
                    Assert.AreEqual(value, GetValue(bdv, i, scratch));
                }
                reader.Dispose();
            }

            writer.Dispose();
            dir.Dispose();
        }
		public void Should_Throw_SearchException_When_Field_Is_Missing(string fieldName)
		{
			// Arrange
			LuceneDocument document = new LuceneDocument();
			document.Add(CreateField("id", "123"));
			document.Add(CreateField("title", "the title"));
			document.Add(CreateField("contentsummary", "the summary"));
			document.Add(CreateField("tags", "tag1 tag2"));
			document.Add(CreateField("createdby", "gandhi"));
			document.Add(CreateField("contentlength", "999"));
			document.Add(CreateField("createdon", DateTime.Today.ToString()));

			document.RemoveField(fieldName);

			ScoreDoc scoreDoc = new ScoreDoc(0, 1f);

			// Act + Assert
			SearchResultViewModel model = new SearchResultViewModel(document, scoreDoc);
		}