private void testEnum(int lower, int upper)
        {
            NumericRangeQuery <int> q        = NumericRangeQuery.NewIntRange("field4", 4, lower, upper, true, true);
            FilteredTermEnum        termEnum = q.GetEnum(searcher.IndexReader, null);

            try
            {
                int count = 0;
                do
                {
                    Term t = termEnum.Term;
                    if (t != null)
                    {
                        int val = NumericUtils.PrefixCodedToInt(t.Text);
                        Assert.True(val >= lower && val <= upper, "value not in bounds");
                        count++;
                    }
                    else
                    {
                        break;
                    }
                } while (termEnum.Next(null));
                Assert.False(termEnum.Next(null));
                Console.WriteLine("TermEnum on 'field4' for range [" + lower + "," + upper + "] contained " + count +
                                  " terms.");
            }
            finally
            {
                termEnum.Close();
            }
        }
        private void TestSorting(int precisionStep)
        {
            string field = "field" + precisionStep;
            // 10 random tests, the index order is ascending,
            // so using a reverse sort field should retun descending documents
            int num = TestUtil.NextInt(Random(), 10, 20);

            for (int i = 0; i < num; i++)
            {
                int lower = (int)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
                int upper = (int)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
                if (lower > upper)
                {
                    int a = lower;
                    lower = upper;
                    upper = a;
                }
                Query   tq      = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, true);
                TopDocs topDocs = Searcher.Search(tq, null, NoDocs, new Sort(new SortField(field, SortField.Type_e.INT, true)));
                if (topDocs.TotalHits == 0)
                {
                    continue;
                }
                ScoreDoc[] sd = topDocs.ScoreDocs;
                Assert.IsNotNull(sd);
                int last = (int)Searcher.Doc(sd[0].Doc).GetField(field).NumericValue;
                for (int j = 1; j < sd.Length; j++)
                {
                    int act = (int)Searcher.Doc(sd[j].Doc).GetField(field).NumericValue;
                    Assert.IsTrue(last > act, "Docs should be sorted backwards");
                    last = act;
                }
            }
        }
 private void  TestSorting(int precisionStep)
 {
     System.Random rnd   = NewRandom();
     System.String field = "field" + precisionStep;
     // 10 random tests, the index order is ascending,
     // so using a reverse sort field should retun descending documents
     for (int i = 0; i < 10; i++)
     {
         int lower = (int)(rnd.NextDouble() * noDocs * distance) + startOffset;
         int upper = (int)(rnd.NextDouble() * noDocs * distance) + startOffset;
         if (lower > upper)
         {
             int a = lower; lower = upper; upper = a;
         }
         System.Int32 tempAux  = (System.Int32)lower;
         System.Int32 tempAux2 = (System.Int32)upper;
         Query        tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux, tempAux2, true, true);
         TopDocs      topDocs  = searcher.Search(tq, null, noDocs, new Sort(new SortField(field, SortField.INT, true)), null);
         if (topDocs.TotalHits == 0)
         {
             continue;
         }
         ScoreDoc[] sd = topDocs.ScoreDocs;
         Assert.IsNotNull(sd);
         int last = System.Int32.Parse(searcher.Doc(sd[0].Doc, null).Get(field, null));
         for (int j = 1; j < sd.Length; j++)
         {
             int act = System.Int32.Parse(searcher.Doc(sd[j].Doc, null).Get(field, null));
             Assert.IsTrue(last > act, "Docs should be sorted backwards");
             last = act;
         }
     }
 }
        public virtual void  TestEqualsAndHash()
        {
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test1", 4, 10, 20, true, true));
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test2", 4, 10, 20, false, true));
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test3", 4, 10, 20, true, false));
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test4", 4, 10, 20, false, false));
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test5", 4, 10, null, true, true));
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test6", 4, null, 20, true, true));
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test7", 4, null, null, true, true));
            QueryUtils.CheckEqual(NumericRangeQuery.NewIntRange("test8", 4, 10, 20, true, true),
                                  NumericRangeQuery.NewIntRange("test8", 4, 10, 20, true, true));

            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test9", 4, 10, 20, true, true),
                                    NumericRangeQuery.NewIntRange("test9", 8, 10, 20, true, true));

            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test10a", 4, 10, 20, true, true),
                                    NumericRangeQuery.NewIntRange("test10b", 4, 10, 20, true, true));

            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test11", 4, 10, 20, true, true),
                                    NumericRangeQuery.NewIntRange("test11", 4, 20, 10, true, true));

            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test12", 4, 10, 20, true, true),
                                    NumericRangeQuery.NewIntRange("test12", 4, 10, 20, false, true));

            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test13", 4, 10, 20, true, true),
                                    NumericRangeQuery.NewFloatRange("test13", 4, 10f, 20f, true, true));
            // the following produces a hash collision, because Long and Integer have the same hashcode, so only test equality:

            Query q1 = NumericRangeQuery.NewIntRange("test14", 4, 10, 20, true, true);
            Query q2 = NumericRangeQuery.NewLongRange("test14", 4, 10L, 20L, true, true);

            Assert.IsFalse(q1.Equals(q2));
            Assert.IsFalse(q2.Equals(q1));
        }
        private void TestRightOpenRange(int precisionStep)
        {
            string field = "field" + precisionStep;
            int    count = 3000;
            int    lower = (count - 1) * Distance + (Distance / 3) + StartOffset;
            NumericRangeQuery <int> q = NumericRangeQuery.NewIntRange(field, precisionStep, lower, null, true, true);
            TopDocs topDocs           = Searcher.Search(q, null, NoDocs, Sort.INDEXORDER);

            ScoreDoc[] sd = topDocs.ScoreDocs;
            Assert.IsNotNull(sd);
            Assert.AreEqual(NoDocs - count, sd.Length, "Score doc count");
            Document doc = Searcher.Doc(sd[0].Doc);

            Assert.AreEqual(count * Distance + StartOffset, (int)doc.GetField(field).NumericValue, "First doc");
            doc = Searcher.Doc(sd[sd.Length - 1].Doc);
            Assert.AreEqual((NoDocs - 1) * Distance + StartOffset, (int)doc.GetField(field).NumericValue, "Last doc");

            q       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, null, true, false);
            topDocs = Searcher.Search(q, null, NoDocs, Sort.INDEXORDER);
            sd      = topDocs.ScoreDocs;
            Assert.IsNotNull(sd);
            Assert.AreEqual(NoDocs - count, sd.Length, "Score doc count");
            doc = Searcher.Doc(sd[0].Doc);
            Assert.AreEqual(count * Distance + StartOffset, (int)doc.GetField(field).NumericValue, "First doc");
            doc = Searcher.Doc(sd[sd.Length - 1].Doc);
            Assert.AreEqual((NoDocs - 1) * Distance + StartOffset, (int)doc.GetField(field).NumericValue, "Last doc");
        }
        private void TestRangeSplit(int precisionStep)
        {
            string field = "ascfield" + precisionStep;
            // 10 random tests
            int num = TestUtil.NextInt(Random(), 10, 20);

            for (int i = 0; i < num; i++)
            {
                int lower = (int)(Random().NextDouble() * NoDocs - NoDocs / 2);
                int upper = (int)(Random().NextDouble() * NoDocs - NoDocs / 2);
                if (lower > upper)
                {
                    int a = lower;
                    lower = upper;
                    upper = a;
                }
                // test inclusive range
                Query   tq       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, true);
                TopDocs tTopDocs = Searcher.Search(tq, 1);
                Assert.AreEqual(upper - lower + 1, tTopDocs.TotalHits, "Returned count of range query must be equal to inclusive range length");
                // test exclusive range
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, false, false);
                tTopDocs = Searcher.Search(tq, 1);
                Assert.AreEqual(Math.Max(upper - lower - 1, 0), tTopDocs.TotalHits, "Returned count of range query must be equal to exclusive range length");
                // test left exclusive range
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, false, true);
                tTopDocs = Searcher.Search(tq, 1);
                Assert.AreEqual(upper - lower, tTopDocs.TotalHits, "Returned count of range query must be equal to half exclusive range length");
                // test right exclusive range
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, false);
                tTopDocs = Searcher.Search(tq, 1);
                Assert.AreEqual(upper - lower, tTopDocs.TotalHits, "Returned count of range query must be equal to half exclusive range length");
            }
        }
        public virtual void TestOneMatchQuery()
        {
            NumericRangeQuery <int> q = NumericRangeQuery.NewIntRange("ascfield8", 8, 1000, 1000, true, true);
            TopDocs topDocs           = Searcher.Search(q, NoDocs);

            ScoreDoc[] sd = topDocs.ScoreDocs;
            Assert.IsNotNull(sd);
            Assert.AreEqual(1, sd.Length, "Score doc count");
        }
        public virtual void  TestOneMatchQuery()
        {
            System.Int32            tempAux  = 1000;
            System.Int32            tempAux2 = 1000;
            NumericRangeQuery <int> q        = NumericRangeQuery.NewIntRange("ascfield8", 8, tempAux, tempAux2, true, true);

            Assert.AreSame(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE, q.RewriteMethod);
            TopDocs topDocs = searcher.Search(q, noDocs, null);

            ScoreDoc[] sd = topDocs.ScoreDocs;
            Assert.IsNotNull(sd);
            Assert.AreEqual(1, sd.Length, "Score doc count");
        }
        public virtual void TestMultiValuedNRQ()
        {
            Directory         directory = NewDirectory();
            RandomIndexWriter writer    = new RandomIndexWriter(Random(), directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMaxBufferedDocs(TestUtil.NextInt(Random(), 50, 1000)));

            //DecimalFormat format = new DecimalFormat("00000000000", new DecimalFormatSymbols(Locale.ROOT));
            NumberFormatInfo f = new NumberFormatInfo();

            f.NumberDecimalSeparator = ".";
            f.NumberDecimalDigits    = 0;

            int num = AtLeast(500);

            for (int l = 0; l < num; l++)
            {
                Document doc = new Document();
                for (int m = 0, c = Random().Next(10); m <= c; m++)
                {
                    int value = Random().Next(int.MaxValue);
                    doc.Add(NewStringField("asc", value.ToString(f), Field.Store.NO));
                    doc.Add(new IntField("trie", value, Field.Store.NO));
                }
                writer.AddDocument(doc);
            }
            IndexReader reader = writer.Reader;

            writer.Dispose();

            IndexSearcher searcher = NewSearcher(reader);

            num = AtLeast(50);
            for (int i = 0; i < num; i++)
            {
                int lower = Random().Next(int.MaxValue);
                int upper = Random().Next(int.MaxValue);
                if (lower > upper)
                {
                    int a = lower;
                    lower = upper;
                    upper = a;
                }
                TermRangeQuery          cq = TermRangeQuery.NewStringRange("asc", lower.ToString(f), upper.ToString(f), true, true);
                NumericRangeQuery <int> tq = NumericRangeQuery.NewIntRange("trie", lower, upper, true, true);
                TopDocs trTopDocs          = searcher.Search(cq, 1);
                TopDocs nrTopDocs          = searcher.Search(tq, 1);
                Assert.AreEqual(trTopDocs.TotalHits, nrTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
            }
            reader.Dispose();
            directory.Dispose();
        }
        public virtual void TestEmptyEnums()
        {
            int count = 3000;
            int lower = (Distance * 3 / 2) + StartOffset, upper = lower + count * Distance + (Distance / 3);

            // test empty enum
            Debug.Assert(lower < upper);
            Assert.IsTrue(0 < CountTerms(NumericRangeQuery.NewIntRange("field4", 4, lower, upper, true, true)));
            Assert.AreEqual(0, CountTerms(NumericRangeQuery.NewIntRange("field4", 4, upper, lower, true, true)));
            // test empty enum outside of bounds
            lower = Distance * NoDocs + StartOffset;
            upper = 2 * lower;
            Debug.Assert(lower < upper);
            Assert.AreEqual(0, CountTerms(NumericRangeQuery.NewIntRange("field4", 4, lower, upper, true, true)));
        }
        private void  TestRightOpenRange(int precisionStep)
        {
            System.String           field   = "field" + precisionStep;
            int                     count   = 3000;
            int                     lower   = (count - 1) * distance + (distance / 3) + startOffset;
            NumericRangeQuery <int> q       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, null, true, true);
            TopDocs                 topDocs = searcher.Search(q, null, noDocs, Sort.INDEXORDER, null);

            System.Console.Out.WriteLine("Found " + q.TotalNumberOfTerms + " distinct terms in right open range for field '" + field + "'.");
            ScoreDoc[] sd = topDocs.ScoreDocs;
            Assert.IsNotNull(sd);
            Assert.AreEqual(noDocs - count, sd.Length, "Score doc count");
            Document doc = searcher.Doc(sd[0].Doc, null);

            Assert.AreEqual(count * distance + startOffset, System.Int32.Parse(doc.Get(field, null)), "First doc");
            doc = searcher.Doc(sd[sd.Length - 1].Doc, null);
            Assert.AreEqual((noDocs - 1) * distance + startOffset, System.Int32.Parse(doc.Get(field, null)), "Last doc");
        }
Beispiel #12
0
        public virtual void  TestMultiValuedNRQ()
        {
            System.Random rnd = NewRandom();

            RAMDirectory directory = new RAMDirectory();
            IndexWriter  writer    = new IndexWriter(directory, new WhitespaceAnalyzer(), true, MaxFieldLength.UNLIMITED);

            //DecimalFormat format = new DecimalFormat("00000000000", new System.Globalization.CultureInfo("en-US").NumberFormat);

            for (int l = 0; l < 5000; l++)
            {
                Document doc = new Document();
                for (int m = 0, c = rnd.Next(10); m <= c; m++)
                {
                    int value_Renamed = rnd.Next(System.Int32.MaxValue);
                    doc.Add(new Field("asc", value_Renamed.ToString().PadLeft(11, '0'), Field.Store.NO, Field.Index.NOT_ANALYZED));
                    doc.Add(new NumericField("trie", Field.Store.NO, true).SetIntValue(value_Renamed));
                }
                writer.AddDocument(doc);
            }
            writer.Close();

            Searcher searcher = new IndexSearcher(directory, true);

            for (int i = 0; i < 50; i++)
            {
                int lower = rnd.Next(System.Int32.MaxValue);
                int upper = rnd.Next(System.Int32.MaxValue);
                if (lower > upper)
                {
                    int a = lower; lower = upper; upper = a;
                }
                TermRangeQuery    cq        = new TermRangeQuery("asc", lower.ToString().PadLeft(11, '0'), upper.ToString().PadLeft(11, '0'), true, true);
                System.Int32      tempAux   = (System.Int32)lower;
                System.Int32      tempAux2  = (System.Int32)upper;
                NumericRangeQuery tq        = NumericRangeQuery.NewIntRange("trie", tempAux, tempAux2, true, true);
                TopDocs           trTopDocs = searcher.Search(cq, 1);
                TopDocs           nrTopDocs = searcher.Search(tq, 1);
                Assert.AreEqual(trTopDocs.TotalHits, nrTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
            }
            searcher.Close();

            directory.Close();
        }
        /// <summary>
        /// test for both constant score and boolean query, the other tests only use the constant score mode </summary>
        private void TestRange(int precisionStep)
        {
            string field = "field" + precisionStep;
            int    count = 3000;
            int    lower = (Distance * 3 / 2) + StartOffset, upper = lower + count * Distance + (Distance / 3);
            NumericRangeQuery <int>  q = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, true);
            NumericRangeFilter <int> f = NumericRangeFilter.NewIntRange(field, precisionStep, lower, upper, true, true);

            for (sbyte i = 0; i < 3; i++)
            {
                TopDocs topDocs;
                string  type;
                switch (i)
                {
                case 0:
                    type = " (constant score filter rewrite)";
                    q.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
                    topDocs = Searcher.Search(q, null, NoDocs, Sort.INDEXORDER);
                    break;

                case 1:
                    type = " (constant score boolean rewrite)";
                    q.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
                    topDocs = Searcher.Search(q, null, NoDocs, Sort.INDEXORDER);
                    break;

                case 2:
                    type    = " (filter)";
                    topDocs = Searcher.Search(new MatchAllDocsQuery(), f, NoDocs, Sort.INDEXORDER);
                    break;

                default:
                    return;
                }
                ScoreDoc[] sd = topDocs.ScoreDocs;
                Assert.IsNotNull(sd);
                Assert.AreEqual(count, sd.Length, "Score doc count" + type);
                Document doc = Searcher.Doc(sd[0].Doc);
                Assert.AreEqual(2 * Distance + StartOffset, (int)doc.GetField(field).NumericValue, "First doc" + type);
                doc = Searcher.Doc(sd[sd.Length - 1].Doc);
                Assert.AreEqual((1 + count) * Distance + StartOffset, (int)doc.GetField(field).NumericValue, "Last doc" + type);
            }
        }
        private void  TestLeftOpenRange(int precisionStep)
        {
            System.String field = "field" + precisionStep;
            int           count = 3000;
            int           upper = (count - 1) * distance + (distance / 3) + startOffset;

            //UPGRADE_TODO: The 'System.Int32' structure does not have an equivalent to NULL. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1291'"
            System.Int32            tempAux = (System.Int32)upper;
            NumericRangeQuery <int> q       = NumericRangeQuery.NewIntRange(field, precisionStep, null, tempAux, true, true);
            TopDocs topDocs = searcher.Search(q, null, noDocs, Sort.INDEXORDER, null);

            System.Console.Out.WriteLine("Found " + q.TotalNumberOfTerms + " distinct terms in left open range for field '" + field + "'.");
            ScoreDoc[] sd = topDocs.ScoreDocs;
            Assert.IsNotNull(sd);
            Assert.AreEqual(count, sd.Length, "Score doc count");
            Document doc = searcher.Doc(sd[0].Doc, null);

            Assert.AreEqual(startOffset, System.Int32.Parse(doc.Get(field, null)), "First doc");
            doc = searcher.Doc(sd[sd.Length - 1].Doc, null);
            Assert.AreEqual((count - 1) * distance + startOffset, System.Int32.Parse(doc.Get(field, null)), "Last doc");
        }
 private void  TestRangeSplit(int precisionStep)
 {
     System.Random rnd   = NewRandom();
     System.String field = "ascfield" + precisionStep;
     // 50 random tests
     for (int i = 0; i < 50; i++)
     {
         int lower = (int)(rnd.NextDouble() * noDocs - noDocs / 2);
         int upper = (int)(rnd.NextDouble() * noDocs - noDocs / 2);
         if (lower > upper)
         {
             int a = lower; lower = upper; upper = a;
         }
         // test inclusive range
         System.Int32 tempAux  = (System.Int32)lower;
         System.Int32 tempAux2 = (System.Int32)upper;
         Query        tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux, tempAux2, true, true);
         TopDocs      tTopDocs = searcher.Search(tq, 1, null);
         Assert.AreEqual(upper - lower + 1, tTopDocs.TotalHits, "Returned count of range query must be equal to inclusive range length");
         // test exclusive range
         System.Int32 tempAux3 = (System.Int32)lower;
         System.Int32 tempAux4 = (System.Int32)upper;
         tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux3, tempAux4, false, false);
         tTopDocs = searcher.Search(tq, 1, null);
         Assert.AreEqual(System.Math.Max(upper - lower - 1, 0), tTopDocs.TotalHits, "Returned count of range query must be equal to exclusive range length");
         // test left exclusive range
         System.Int32 tempAux5 = (System.Int32)lower;
         System.Int32 tempAux6 = (System.Int32)upper;
         tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux5, tempAux6, false, true);
         tTopDocs = searcher.Search(tq, 1, null);
         Assert.AreEqual(upper - lower, tTopDocs.TotalHits, "Returned count of range query must be equal to half exclusive range length");
         // test right exclusive range
         System.Int32 tempAux7 = (System.Int32)lower;
         System.Int32 tempAux8 = (System.Int32)upper;
         tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux7, tempAux8, true, false);
         tTopDocs = searcher.Search(tq, 1, null);
         Assert.AreEqual(upper - lower, tTopDocs.TotalHits, "Returned count of range query must be equal to half exclusive range length");
     }
 }
        private void TestRandomTrieAndClassicRangeQuery(int precisionStep)
        {
            string field = "field" + precisionStep;
            int    totalTermCountT = 0, totalTermCountC = 0, termCountT, termCountC;
            int    num = TestUtil.NextInt(Random(), 10, 20);

            for (int i = 0; i < num; i++)
            {
                int lower = (int)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
                int upper = (int)(Random().NextDouble() * NoDocs * Distance) + StartOffset;
                if (lower > upper)
                {
                    int a = lower;
                    lower = upper;
                    upper = a;
                }
                BytesRef lowerBytes = new BytesRef(NumericUtils.BUF_SIZE_INT), upperBytes = new BytesRef(NumericUtils.BUF_SIZE_INT);
                NumericUtils.IntToPrefixCodedBytes(lower, 0, lowerBytes);
                NumericUtils.IntToPrefixCodedBytes(upper, 0, upperBytes);

                // test inclusive range
                NumericRangeQuery <int> tq = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, true);
                TermRangeQuery          cq = new TermRangeQuery(field, lowerBytes, upperBytes, true, true);
                TopDocs tTopDocs           = Searcher.Search(tq, 1);
                TopDocs cTopDocs           = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test exclusive range
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, false, false);
                cq       = new TermRangeQuery(field, lowerBytes, upperBytes, false, false);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test left exclusive range
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, false, true);
                cq       = new TermRangeQuery(field, lowerBytes, upperBytes, false, true);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
                // test right exclusive range
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, lower, upper, true, false);
                cq       = new TermRangeQuery(field, lowerBytes, upperBytes, true, false);
                tTopDocs = Searcher.Search(tq, 1);
                cTopDocs = Searcher.Search(cq, 1);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                totalTermCountT += termCountT = CountTerms(tq);
                totalTermCountC += termCountC = CountTerms(cq);
                CheckTermCounts(precisionStep, termCountT, termCountC);
            }

            CheckTermCounts(precisionStep, totalTermCountT, totalTermCountC);
            if (VERBOSE && precisionStep != int.MaxValue)
            {
                Console.WriteLine("Average number of terms during random search on '" + field + "':");
                Console.WriteLine(" Numeric query: " + (((double)totalTermCountT) / (num * 4)));
                Console.WriteLine(" Classical query: " + (((double)totalTermCountC) / (num * 4)));
            }
        }
        public virtual void TestInfiniteValues()
        {
            Directory         dir    = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())));
            Document          doc    = new Document();

            doc.Add(new FloatField("float", float.NegativeInfinity, Field.Store.NO));
            doc.Add(new IntField("int", int.MinValue, Field.Store.NO));
            writer.AddDocument(doc);

            doc = new Document();
            doc.Add(new FloatField("float", float.PositiveInfinity, Field.Store.NO));
            doc.Add(new IntField("int", int.MaxValue, Field.Store.NO));
            writer.AddDocument(doc);

            doc = new Document();
            doc.Add(new FloatField("float", 0.0f, Field.Store.NO));
            doc.Add(new IntField("int", 0, Field.Store.NO));
            writer.AddDocument(doc);

            foreach (float f in TestNumericUtils.FLOAT_NANs)
            {
                doc = new Document();
                doc.Add(new FloatField("float", f, Field.Store.NO));
                writer.AddDocument(doc);
            }

            writer.Dispose();

            IndexReader   r = DirectoryReader.Open(dir);
            IndexSearcher s = NewSearcher(r);

            Query   q       = NumericRangeQuery.NewIntRange("int", null, null, true, true);
            TopDocs topDocs = s.Search(q, 10);

            Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count");

            q       = NumericRangeQuery.NewIntRange("int", null, null, false, false);
            topDocs = s.Search(q, 10);
            Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count");

            q       = NumericRangeQuery.NewIntRange("int", int.MinValue, int.MaxValue, true, true);
            topDocs = s.Search(q, 10);
            Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count");

            q       = NumericRangeQuery.NewIntRange("int", int.MinValue, int.MaxValue, false, false);
            topDocs = s.Search(q, 10);
            Assert.AreEqual(1, topDocs.ScoreDocs.Length, "Score doc count");

            q       = NumericRangeQuery.NewFloatRange("float", null, null, true, true);
            topDocs = s.Search(q, 10);
            Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count");

            q       = NumericRangeQuery.NewFloatRange("float", null, null, false, false);
            topDocs = s.Search(q, 10);
            Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count");

            q       = NumericRangeQuery.NewFloatRange("float", float.NegativeInfinity, float.PositiveInfinity, true, true);
            topDocs = s.Search(q, 10);
            Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count");

            q       = NumericRangeQuery.NewFloatRange("float", float.NegativeInfinity, float.PositiveInfinity, false, false);
            topDocs = s.Search(q, 10);
            Assert.AreEqual(1, topDocs.ScoreDocs.Length, "Score doc count");

            q       = NumericRangeQuery.NewFloatRange("float", float.NaN, float.NaN, true, true);
            topDocs = s.Search(q, 10);
            Assert.AreEqual(TestNumericUtils.FLOAT_NANs.Length, topDocs.ScoreDocs.Length, "Score doc count");

            r.Dispose();
            dir.Dispose();
        }
        private void  TestRandomTrieAndClassicRangeQuery(int precisionStep)
        {
            System.Random rnd = NewRandom();
            System.String field = "field" + precisionStep;
            int           termCountT = 0, termCountC = 0;

            for (int i = 0; i < 50; i++)
            {
                int lower = (int)(rnd.NextDouble() * noDocs * distance) + startOffset;
                int upper = (int)(rnd.NextDouble() * noDocs * distance) + startOffset;
                if (lower > upper)
                {
                    int a = lower; lower = upper; upper = a;
                }
                // test inclusive range
                System.Int32            tempAux  = (System.Int32)lower;
                System.Int32            tempAux2 = (System.Int32)upper;
                NumericRangeQuery <int> tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux, tempAux2, true, true);
                TermRangeQuery          cq       = new TermRangeQuery(field, NumericUtils.IntToPrefixCoded(lower), NumericUtils.IntToPrefixCoded(upper), true, true);
                TopDocs tTopDocs = searcher.Search(tq, 1, null);
                TopDocs cTopDocs = searcher.Search(cq, 1, null);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                termCountT += tq.TotalNumberOfTerms;
                termCountC += cq.TotalNumberOfTerms;
                // test exclusive range
                System.Int32 tempAux3 = (System.Int32)lower;
                System.Int32 tempAux4 = (System.Int32)upper;
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux3, tempAux4, false, false);
                cq       = new TermRangeQuery(field, NumericUtils.IntToPrefixCoded(lower), NumericUtils.IntToPrefixCoded(upper), false, false);
                tTopDocs = searcher.Search(tq, 1, null);
                cTopDocs = searcher.Search(cq, 1, null);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                termCountT += tq.TotalNumberOfTerms;
                termCountC += cq.TotalNumberOfTerms;
                // test left exclusive range
                System.Int32 tempAux5 = (System.Int32)lower;
                System.Int32 tempAux6 = (System.Int32)upper;
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux5, tempAux6, false, true);
                cq       = new TermRangeQuery(field, NumericUtils.IntToPrefixCoded(lower), NumericUtils.IntToPrefixCoded(upper), false, true);
                tTopDocs = searcher.Search(tq, 1, null);
                cTopDocs = searcher.Search(cq, 1, null);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                termCountT += tq.TotalNumberOfTerms;
                termCountC += cq.TotalNumberOfTerms;
                // test right exclusive range
                System.Int32 tempAux7 = (System.Int32)lower;
                System.Int32 tempAux8 = (System.Int32)upper;
                tq       = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux7, tempAux8, true, false);
                cq       = new TermRangeQuery(field, NumericUtils.IntToPrefixCoded(lower), NumericUtils.IntToPrefixCoded(upper), true, false);
                tTopDocs = searcher.Search(tq, 1, null);
                cTopDocs = searcher.Search(cq, 1, null);
                Assert.AreEqual(cTopDocs.TotalHits, tTopDocs.TotalHits, "Returned count for NumericRangeQuery and TermRangeQuery must be equal");
                termCountT += tq.TotalNumberOfTerms;
                termCountC += cq.TotalNumberOfTerms;
            }
            if (precisionStep == System.Int32.MaxValue)
            {
                Assert.AreEqual(termCountT, termCountC, "Total number of terms should be equal for unlimited precStep");
            }
            else
            {
                System.Console.Out.WriteLine("Average number of terms during random search on '" + field + "':");
                System.Console.Out.WriteLine(" Trie query: " + (((double)termCountT) / (50 * 4)));
                System.Console.Out.WriteLine(" Classical query: " + (((double)termCountC) / (50 * 4)));
            }
        }
        /// <summary>test for both constant score and boolean query, the other tests only use the constant score mode </summary>
        private void  TestRange(int precisionStep)
        {
            System.String field = "field" + precisionStep;
            int           count = 3000;
            int           lower = (distance * 3 / 2) + startOffset, upper = lower + count * distance + (distance / 3);

            System.Int32            tempAux  = (System.Int32)lower;
            System.Int32            tempAux2 = (System.Int32)upper;
            NumericRangeQuery <int> q        = NumericRangeQuery.NewIntRange(field, precisionStep, tempAux, tempAux2, true, true);

            System.Int32             tempAux3 = (System.Int32)lower;
            System.Int32             tempAux4 = (System.Int32)upper;
            NumericRangeFilter <int> f        = NumericRangeFilter.NewIntRange(field, precisionStep, tempAux3, tempAux4, true, true);
            int lastTerms = 0;

            for (sbyte i = 0; i < 3; i++)
            {
                TopDocs       topDocs;
                int           terms;
                System.String type;
                q.ClearTotalNumberOfTerms();
                f.ClearTotalNumberOfTerms();
                switch (i)
                {
                case 0:
                    type            = " (constant score filter rewrite)";
                    q.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE;
                    topDocs         = searcher.Search(q, null, noDocs, Sort.INDEXORDER, null);
                    terms           = q.TotalNumberOfTerms;
                    break;

                case 1:
                    type            = " (constant score boolean rewrite)";
                    q.RewriteMethod = MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE;
                    topDocs         = searcher.Search(q, null, noDocs, Sort.INDEXORDER, null);
                    terms           = q.TotalNumberOfTerms;
                    break;

                case 2:
                    type    = " (filter)";
                    topDocs = searcher.Search(new MatchAllDocsQuery(), f, noDocs, Sort.INDEXORDER, null);
                    terms   = f.TotalNumberOfTerms;
                    break;

                default:
                    return;
                }
                System.Console.Out.WriteLine("Found " + terms + " distinct terms in range for field '" + field + "'" + type + ".");
                ScoreDoc[] sd = topDocs.ScoreDocs;
                Assert.IsNotNull(sd);
                Assert.AreEqual(count, sd.Length, "Score doc count" + type);
                Document doc = searcher.Doc(sd[0].Doc, null);
                Assert.AreEqual(2 * distance + startOffset, System.Int32.Parse(doc.Get(field, null)), "First doc" + type);
                doc = searcher.Doc(sd[sd.Length - 1].Doc, null);
                Assert.AreEqual((1 + count) * distance + startOffset, System.Int32.Parse(doc.Get(field, null)), "Last doc" + type);
                if (i > 0)
                {
                    Assert.AreEqual(lastTerms, terms, "Distinct term number is equal for all query types");
                }
                lastTerms = terms;
            }
        }
Beispiel #20
0
 /// <summary> Factory that creates a <code>NumericRangeFilter</code>, that queries a <code>int</code>
 /// range using the default <code>precisionStep</code> {@link NumericUtils#PRECISION_STEP_DEFAULT} (4).
 /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
 /// by setting the min or max value to <code>null</code>. By setting inclusive to false, it will
 /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
 /// </summary>
 public static NumericRangeFilter NewIntRange(System.String field, System.ValueType min, System.ValueType max, bool minInclusive, bool maxInclusive)
 {
     return(new NumericRangeFilter(NumericRangeQuery.NewIntRange(field, min, max, minInclusive, maxInclusive)));
 }
Beispiel #21
0
        public virtual void  TestEqualsAndHash()
        {
            System.Int32 tempAux  = 10;
            System.Int32 tempAux2 = 20;
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test1", 4, tempAux, tempAux2, true, true));
            System.Int32 tempAux3 = 10;
            System.Int32 tempAux4 = 20;
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test2", 4, tempAux3, tempAux4, false, true));
            System.Int32 tempAux5 = 10;
            System.Int32 tempAux6 = 20;
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test3", 4, tempAux5, tempAux6, true, false));
            System.Int32 tempAux7 = 10;
            System.Int32 tempAux8 = 20;
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test4", 4, tempAux7, tempAux8, false, false));
            //UPGRADE_TODO: The 'System.Int32' structure does not have an equivalent to NULL. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1291'"
            System.Int32 tempAux9 = 10;
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test5", 4, tempAux9, null, true, true));
            //UPGRADE_TODO: The 'System.Int32' structure does not have an equivalent to NULL. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1291'"
            System.Int32 tempAux10 = 20;
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test6", 4, null, tempAux10, true, true));
            //UPGRADE_TODO: The 'System.Int32' structure does not have an equivalent to NULL. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1291'"
            QueryUtils.CheckHashEquals(NumericRangeQuery.NewIntRange("test7", 4, null, null, true, true));
            System.Int32 tempAux11 = 10;
            System.Int32 tempAux12 = 20;
            System.Int32 tempAux13 = 10;
            System.Int32 tempAux14 = 20;
            QueryUtils.CheckEqual(NumericRangeQuery.NewIntRange("test8", 4, tempAux11, tempAux12, true, true), NumericRangeQuery.NewIntRange("test8", 4, tempAux13, tempAux14, true, true));
            System.Int32 tempAux15 = 10;
            System.Int32 tempAux16 = 20;
            System.Int32 tempAux17 = 10;
            System.Int32 tempAux18 = 20;
            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test9", 4, tempAux15, tempAux16, true, true), NumericRangeQuery.NewIntRange("test9", 8, tempAux17, tempAux18, true, true));
            System.Int32 tempAux19 = 10;
            System.Int32 tempAux20 = 20;
            System.Int32 tempAux21 = 10;
            System.Int32 tempAux22 = 20;
            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test10a", 4, tempAux19, tempAux20, true, true), NumericRangeQuery.NewIntRange("test10b", 4, tempAux21, tempAux22, true, true));
            System.Int32 tempAux23 = 10;
            System.Int32 tempAux24 = 20;
            System.Int32 tempAux25 = 20;
            System.Int32 tempAux26 = 10;
            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test11", 4, tempAux23, tempAux24, true, true), NumericRangeQuery.NewIntRange("test11", 4, tempAux25, tempAux26, true, true));
            System.Int32 tempAux27 = 10;
            System.Int32 tempAux28 = 20;
            System.Int32 tempAux29 = 10;
            System.Int32 tempAux30 = 20;
            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test12", 4, tempAux27, tempAux28, true, true), NumericRangeQuery.NewIntRange("test12", 4, tempAux29, tempAux30, false, true));
            System.Int32  tempAux31 = 10;
            System.Int32  tempAux32 = 20;
            System.Single tempAux33 = (float)10f;
            System.Single tempAux34 = (float)20f;
            QueryUtils.CheckUnequal(NumericRangeQuery.NewIntRange("test13", 4, tempAux31, tempAux32, true, true), NumericRangeQuery.NewFloatRange("test13", 4, tempAux33, tempAux34, true, true));
            // the following produces a hash collision, because Long and Integer have the same hashcode, so only test equality:
            System.Int32 tempAux35 = 10;
            System.Int32 tempAux36 = 20;
            Query        q1        = NumericRangeQuery.NewIntRange("test14", 4, tempAux35, tempAux36, true, true);

            System.Int64 tempAux37 = 10L;
            System.Int64 tempAux38 = 20L;
            Query        q2        = NumericRangeQuery.NewLongRange("test14", 4, tempAux37, tempAux38, true, true);

            Assert.IsFalse(q1.Equals(q2));
            Assert.IsFalse(q2.Equals(q1));
        }