public void TestReadSkip()
        {
            Directory         dir    = NewDirectory();
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));

            iwConf.SetMaxBufferedDocs(RandomInts.NextIntBetween(Random(), 2, 30));
            RandomIndexWriter iw = new RandomIndexWriter(Random(), dir, iwConf);

            FieldType ft = new FieldType();

            ft.Stored = true;
            ft.Freeze();

            string @string = TestUtil.RandomSimpleString(Random(), 50);

            sbyte[] bytes = @string.GetBytes(IOUtils.CHARSET_UTF_8);
            long    l     = Random().NextBoolean() ? Random().Next(42) : Random().NextLong();
            int     i     = Random().NextBoolean() ? Random().Next(42) : Random().Next();
            float   f     = Random().NextFloat();
            double  d     = Random().NextDouble();

            IList <Field> fields = Arrays.AsList(new Field("bytes", bytes, ft), new Field("string", @string, ft), new LongField("long", l, Field.Store.YES), new IntField("int", i, Field.Store.YES), new FloatField("float", f, Field.Store.YES), new DoubleField("double", d, Field.Store.YES)
                                                 );

            for (int k = 0; k < 100; ++k)
            {
                Document doc = new Document();
                foreach (Field fld in fields)
                {
                    doc.Add(fld);
                }
                iw.w.AddDocument(doc);
            }
            iw.Commit();

            DirectoryReader reader = DirectoryReader.Open(dir);
            int             docID  = Random().Next(100);

            foreach (Field fld in fields)
            {
                string         fldName = fld.Name();
                Document       sDoc    = reader.Document(docID, CollectionsHelper.Singleton(fldName));
                IndexableField sField  = sDoc.GetField(fldName);
                if (typeof(Field).Equals(fld.GetType()))
                {
                    Assert.AreEqual(fld.BinaryValue(), sField.BinaryValue());
                    Assert.AreEqual(fld.StringValue, sField.StringValue);
                }
                else
                {
                    Assert.AreEqual(fld.NumericValue, sField.NumericValue);
                }
            }
            reader.Dispose();
            iw.Dispose();
            dir.Dispose();
        }
Beispiel #2
0
        public virtual void TestBooleanQuery()
        {
            TermQuery aQuery = new TermQuery(new Term("f", "a"));
            TermQuery dQuery = new TermQuery(new Term("f", "d"));
            TermQuery cQuery = new TermQuery(new Term("f", "c"));
            TermQuery yQuery = new TermQuery(new Term("f", "y"));

            BooleanQuery query = new BooleanQuery();
            BooleanQuery inner = new BooleanQuery();

            inner.Add(cQuery, Occur.SHOULD);
            inner.Add(yQuery, Occur.MUST_NOT);
            query.Add(inner, Occur.MUST);
            query.Add(aQuery, Occur.MUST);
            query.Add(dQuery, Occur.MUST);

            // Only needed in Java6; Java7+ has a @SafeVarargs annotated Arrays#asList()!
            // see http://docs.oracle.com/javase/7/docs/api/java/lang/SafeVarargs.html
            IEnumerable <ISet <string> > occurList = Arrays.AsList(CollectionsHelper.Singleton("MUST"), new HashSet <string>(Arrays.AsList("MUST", "SHOULD")));

            foreach (HashSet <string> occur in occurList)
            {
                CountingCollector c = new CountingCollector(TopScoreDocCollector.Create(10, true), occur);
                s.Search(query, null, c);
                int maxDocs = s.IndexReader.MaxDoc;
                Assert.AreEqual(maxDocs, c.DocCounts.Count);
                bool includeOptional = occur.Contains("SHOULD");
                for (int i = 0; i < maxDocs; i++)
                {
                    IDictionary <Query, float?> doc0 = c.DocCounts[i];
                    Assert.AreEqual(includeOptional ? 5 : 4, doc0.Count);
                    Assert.AreEqual(1.0F, doc0[aQuery], FLOAT_TOLERANCE);
                    Assert.AreEqual(4.0F, doc0[dQuery], FLOAT_TOLERANCE);
                    if (includeOptional)
                    {
                        Assert.AreEqual(3.0F, doc0[cQuery], FLOAT_TOLERANCE);
                    }

                    IDictionary <Query, float?> doc1 = c.DocCounts[++i];
                    Assert.AreEqual(includeOptional ? 5 : 4, doc1.Count);
                    Assert.AreEqual(1.0F, doc1[aQuery], FLOAT_TOLERANCE);
                    Assert.AreEqual(1.0F, doc1[dQuery], FLOAT_TOLERANCE);
                    if (includeOptional)
                    {
                        Assert.AreEqual(1.0F, doc1[cQuery], FLOAT_TOLERANCE);
                    }
                }
            }
        }
Beispiel #3
0
            public virtual void TestDummy()
            {
                MockDirectoryWrapper dir = NewMockDirectory();

                dir.AssertNoUnrefencedFilesOnClose = true;
                IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(LuceneTestCase.TEST_VERSION_CURRENT, null));

                iw.AddDocument(new Document());
                iw.Dispose();
                IndexOutput output = dir.CreateOutput("_hello.world", IOContext.DEFAULT);

                output.writeString("i am unreferenced!");
                output.Dispose();
                dir.Sync(CollectionsHelper.Singleton("_hello.world"));
                dir.Dispose();
            }
Beispiel #4
0
        public virtual void TestFsyncDoesntCreateNewFiles()
        {
            //File path = CreateTempDir("nocreate");
            DirectoryInfo path = new DirectoryInfo(Path.Combine(AppSettings.Get("tempDir", ""), "nocreate"));

            Console.WriteLine(path.FullName);
            Directory fsdir = new SimpleFSDirectory(path);

            // write a file
            IndexOutput @out = fsdir.CreateOutput("afile", NewIOContext(Random()));

            @out.WriteString("boo");
            @out.Dispose();

            // delete it
            try
            {
                var newDir = new DirectoryInfo(Path.Combine(path.FullName, "afile"));
                newDir.Create();
                newDir.Delete();
            }
            catch (Exception)
            {
                Assert.Fail("Deletion of new Directory should never fail.");
            }

            // directory is empty
            Assert.AreEqual(0, fsdir.ListAll().Length);

            // fsync it
            try
            {
                fsdir.Sync(CollectionsHelper.Singleton("afile"));
                Assert.Fail("didn't get expected exception, instead fsync created new files: " + Arrays.AsList(fsdir.ListAll()));
            }
            catch (FileNotFoundException /*| NoSuchFileException*/ expected)
            {
                // ok
            }

            // directory is still empty
            Assert.AreEqual(0, fsdir.ListAll().Length);

            fsdir.Dispose();
        }
Beispiel #5
0
        public virtual void TestCompoundFileAppendTwice()
        {
            Directory             newDir = NewFSSwitchDirectory(CollectionsHelper.Singleton("cfs"));
            CompoundFileDirectory csw    = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random()), true);

            CreateSequenceFile(newDir, "d1", (sbyte)0, 15);
            IndexOutput @out = csw.CreateOutput("d.xyz", NewIOContext(Random()));

            @out.WriteInt(0);
            @out.Dispose();
            Assert.AreEqual(1, csw.ListAll().Length);
            Assert.AreEqual("d.xyz", csw.ListAll()[0]);

            csw.Dispose();

            CompoundFileDirectory cfr = new CompoundFileDirectory(newDir, "d.cfs", NewIOContext(Random()), false);

            Assert.AreEqual(1, cfr.ListAll().Length);
            Assert.AreEqual("d.xyz", cfr.ListAll()[0]);
            cfr.Dispose();
            newDir.Dispose();
        }