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++) { long lower = (long)(Random().NextDouble() * NoDocs * Distance) + StartOffset; long upper = (long)(Random().NextDouble() * NoDocs * Distance) + StartOffset; if (lower > upper) { long a = lower; lower = upper; upper = a; } Query tq = NumericRangeQuery.NewLongRange(field, precisionStep, lower, upper, true, true); TopDocs topDocs = Searcher.Search(tq, null, NoDocs, new Sort(new SortField(field, SortField.Type_e.LONG, true))); if (topDocs.TotalHits == 0) { continue; } ScoreDoc[] sd = topDocs.ScoreDocs; Assert.IsNotNull(sd); long last = (long)Searcher.Doc(sd[0].Doc).GetField(field).NumericValue; for (int j = 1; j < sd.Length; j++) { long act = (long)Searcher.Doc(sd[j].Doc).GetField(field).NumericValue; Assert.IsTrue(last > act, "Docs should be sorted backwards"); last = act; } } }
private void TestRightOpenRange(int precisionStep) { string field = "field" + precisionStep; int count = 3000; long lower = (count - 1) * Distance + (Distance / 3) + StartOffset; NumericRangeQuery <long> q = NumericRangeQuery.NewLongRange(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, (long)doc.GetField(field).NumericValue, "First doc"); doc = Searcher.Doc(sd[sd.Length - 1].Doc); Assert.AreEqual((NoDocs - 1) * Distance + StartOffset, (long)doc.GetField(field).NumericValue, "Last doc"); q = NumericRangeQuery.NewLongRange(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, (long)doc.GetField(field).NumericValue, "First doc"); doc = Searcher.Doc(sd[sd.Length - 1].Doc); Assert.AreEqual((NoDocs - 1) * Distance + StartOffset, (long)doc.GetField(field).NumericValue, "Last doc"); }
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++) { long lower = (long)(rnd.NextDouble() * noDocs * distance) + startOffset; long upper = (long)(rnd.NextDouble() * noDocs * distance) + startOffset; if (lower > upper) { long a = lower; lower = upper; upper = a; } System.Int64 tempAux = (long)lower; System.Int64 tempAux2 = (long)upper; Query tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux, tempAux2, true, true); TopDocs topDocs = searcher.Search(tq, null, noDocs, new Sort(new SortField(field, SortField.LONG, true))); if (topDocs.totalHits == 0) { continue; } ScoreDoc[] sd = topDocs.scoreDocs; Assert.IsNotNull(sd); long last = System.Int64.Parse(searcher.Doc(sd[0].doc).Get(field)); for (int j = 1; j < sd.Length; j++) { long act = System.Int64.Parse(searcher.Doc(sd[j].doc).Get(field)); 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 TestRangeSplit(int precisionStep) { string field = "ascfield" + precisionStep; // 10 random tests int num = TestUtil.NextInt(Random(), 10, 20); for (int i = 0; i < num; i++) { long lower = (long)(Random().NextDouble() * NoDocs - NoDocs / 2); long upper = (long)(Random().NextDouble() * NoDocs - NoDocs / 2); if (lower > upper) { long a = lower; lower = upper; upper = a; } // test inclusive range Query tq = NumericRangeQuery.NewLongRange(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.NewLongRange(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.NewLongRange(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.NewLongRange(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 TestEqualsAndHash() { QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test1", 4, 10L, 20L, true, true)); QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test2", 4, 10L, 20L, false, true)); QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test3", 4, 10L, 20L, true, false)); System.Int64 tempAux7 = 10L; QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test4", 4, 10L, 20L, false, false)); QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test5", 4, 10L, null, true, true)); QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test6", 4, null, 20L, true, true)); QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test7", 4, null, null, true, true)); QueryUtils.CheckEqual(NumericRangeQuery.NewLongRange("test8", 4, 10L, 20L, true, true), NumericRangeQuery.NewLongRange("test8", 4, 10L, 20L, true, true)); QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test9", 4, 10L, 20L, true, true), NumericRangeQuery.NewLongRange("test9", 8, 10L, 20L, true, true)); QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test10a", 4, 10L, 20L, true, true), NumericRangeQuery.NewLongRange("test10b", 4, 10L, 20L, true, true)); QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test11", 4, 10L, 20L, true, true), NumericRangeQuery.NewLongRange("test11", 4, 20L, 10L, true, true)); QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test12", 4, 10L, 20L, true, true), NumericRangeQuery.NewLongRange("test12", 4, 10L, 20L, false, true)); QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test13", 4, 10L, 20L, true, true), NumericRangeQuery.NewFloatRange("test13", 4, 10f, 20f, true, true)); // difference to int range is tested in TestNumericRangeQuery32 }
public virtual void TestOneMatchQuery() { NumericRangeQuery <long> q = NumericRangeQuery.NewLongRange("ascfield8", 8, 1000L, 1000L, 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 TestEqualsAndHash() { System.Int64 tempAux = 10L; System.Int64 tempAux2 = 20L; QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test1", 4, tempAux, tempAux2, true, true)); System.Int64 tempAux3 = 10L; System.Int64 tempAux4 = 20L; QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test2", 4, tempAux3, tempAux4, false, true)); System.Int64 tempAux5 = 10L; System.Int64 tempAux6 = 20L; QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test3", 4, tempAux5, tempAux6, true, false)); System.Int64 tempAux7 = 10L; System.Int64 tempAux8 = 20L; QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test4", 4, tempAux7, tempAux8, false, false)); //UPGRADE_TODO: The 'System.Int64' structure does not have an equivalent to NULL. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1291'" System.Int64 tempAux9 = 10L; QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test5", 4, tempAux9, null, true, true)); //UPGRADE_TODO: The 'System.Int64' structure does not have an equivalent to NULL. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1291'" System.Int64 tempAux10 = 20L; QueryUtils.CheckHashEquals(NumericRangeQuery.NewLongRange("test6", 4, null, tempAux10, true, true)); //UPGRADE_TODO: The 'System.Int64' 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.NewLongRange("test7", 4, null, null, true, true)); System.Int64 tempAux11 = 10L; System.Int64 tempAux12 = 20L; System.Int64 tempAux13 = 10L; System.Int64 tempAux14 = 20L; QueryUtils.CheckEqual(NumericRangeQuery.NewLongRange("test8", 4, tempAux11, tempAux12, true, true), NumericRangeQuery.NewLongRange("test8", 4, tempAux13, tempAux14, true, true)); System.Int64 tempAux15 = 10L; System.Int64 tempAux16 = 20L; System.Int64 tempAux17 = 10L; System.Int64 tempAux18 = 20L; QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test9", 4, tempAux15, tempAux16, true, true), NumericRangeQuery.NewLongRange("test9", 8, tempAux17, tempAux18, true, true)); System.Int64 tempAux19 = 10L; System.Int64 tempAux20 = 20L; System.Int64 tempAux21 = 10L; System.Int64 tempAux22 = 20L; QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test10a", 4, tempAux19, tempAux20, true, true), NumericRangeQuery.NewLongRange("test10b", 4, tempAux21, tempAux22, true, true)); System.Int64 tempAux23 = 10L; System.Int64 tempAux24 = 20L; System.Int64 tempAux25 = 20L; System.Int64 tempAux26 = 10L; QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test11", 4, tempAux23, tempAux24, true, true), NumericRangeQuery.NewLongRange("test11", 4, tempAux25, tempAux26, true, true)); System.Int64 tempAux27 = 10L; System.Int64 tempAux28 = 20L; System.Int64 tempAux29 = 10L; System.Int64 tempAux30 = 20L; QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test12", 4, tempAux27, tempAux28, true, true), NumericRangeQuery.NewLongRange("test12", 4, tempAux29, tempAux30, false, true)); System.Int64 tempAux31 = 10L; System.Int64 tempAux32 = 20L; System.Single tempAux33 = (float)10f; System.Single tempAux34 = (float)20f; QueryUtils.CheckUnequal(NumericRangeQuery.NewLongRange("test13", 4, tempAux31, tempAux32, true, true), NumericRangeQuery.NewFloatRange("test13", 4, tempAux33, tempAux34, true, true)); // difference to int range is tested in TestNumericRangeQuery32 }
public virtual void TestOneMatchQuery() { System.Int64 tempAux = 1000L; //UPGRADE_NOTE: ref keyword was added to struct-type parameters. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1303'" System.Int64 tempAux2 = 1000L; NumericRangeQuery q = NumericRangeQuery.NewLongRange("ascfield8", 8, tempAux, tempAux2, true, true); Assert.AreSame(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE, q.GetRewriteMethod()); TopDocs topDocs = searcher.Search(q, noDocs); ScoreDoc[] sd = topDocs.scoreDocs; Assert.IsNotNull(sd); Assert.AreEqual(1, sd.Length, "Score doc count"); }
public virtual void TestEmptyEnums() { int count = 3000; long lower = (Distance * 3 / 2) + StartOffset, upper = lower + count * Distance + (Distance / 3); // test empty enum Debug.Assert(lower < upper); Assert.IsTrue(0 < CountTerms(NumericRangeQuery.NewLongRange("field4", 4, lower, upper, true, true))); Assert.AreEqual(0, CountTerms(NumericRangeQuery.NewLongRange("field4", 4, upper, lower, true, true))); // test empty enum outside of bounds lower = Distance * NoDocs + StartOffset; upper = 2L * lower; Debug.Assert(lower < upper); Assert.AreEqual(0, CountTerms(NumericRangeQuery.NewLongRange("field4", 4, lower, upper, true, true))); }
/// <summary> /// test for constant score + boolean query + filter, the other tests only use the constant score mode </summary> private void TestRange(int precisionStep) { string field = "field" + precisionStep; int count = 3000; long lower = (Distance * 3 / 2) + StartOffset, upper = lower + count * Distance + (Distance / 3); NumericRangeQuery <long> q = NumericRangeQuery.NewLongRange(field, precisionStep, lower, upper, true, true); NumericRangeFilter <long> f = NumericRangeFilter.NewLongRange(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, (long)doc.GetField(field).NumericValue, "First doc" + type); doc = Searcher.Doc(sd[sd.Length - 1].Doc); Assert.AreEqual((1 + count) * Distance + StartOffset, (long)doc.GetField(field).NumericValue, "Last doc" + type); } }
private void TestRightOpenRange(int precisionStep) { System.String field = "field" + precisionStep; int count = 3000; long lower = (count - 1) * distance + (distance / 3) + startOffset; //UPGRADE_TODO: The 'System.Int64' structure does not have an equivalent to NULL. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1291'" System.Int64 tempAux = (long)lower; NumericRangeQuery q = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux, null, true, true); TopDocs topDocs = searcher.Search(q, null, noDocs, Sort.INDEXORDER); System.Console.Out.WriteLine("Found " + q.GetTotalNumberOfTerms() + " 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); Assert.AreEqual(count * distance + startOffset, System.Int64.Parse(doc.Get(field)), "First doc"); doc = searcher.Doc(sd[sd.Length - 1].doc); Assert.AreEqual((noDocs - 1) * distance + startOffset, System.Int64.Parse(doc.Get(field)), "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++) { long lower = (long)(rnd.NextDouble() * noDocs - noDocs / 2); long upper = (long)(rnd.NextDouble() * noDocs - noDocs / 2); if (lower > upper) { long a = lower; lower = upper; upper = a; } // test inclusive range System.Int64 tempAux = (long)lower; System.Int64 tempAux2 = (long)upper; Query tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux, tempAux2, 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 System.Int64 tempAux3 = (long)lower; System.Int64 tempAux4 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux3, tempAux4, false, false); tTopDocs = searcher.Search(tq, 1); 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.Int64 tempAux5 = (long)lower; System.Int64 tempAux6 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux5, tempAux6, 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 System.Int64 tempAux7 = (long)lower; System.Int64 tempAux8 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux7, tempAux8, 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"); } }
/// <summary>test for constant score + boolean query + filter, the other tests only use the constant score mode </summary> private void TestRange(int precisionStep) { System.String field = "field" + precisionStep; int count = 3000; long lower = (distance * 3 / 2) + startOffset, upper = lower + count * distance + (distance / 3); System.Int64 tempAux = (long)lower; System.Int64 tempAux2 = (long)upper; NumericRangeQuery q = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux, tempAux2, true, true); System.Int64 tempAux3 = (long)lower; System.Int64 tempAux4 = (long)upper; NumericRangeFilter f = NumericRangeFilter.NewLongRange(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.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); topDocs = searcher.Search(q, null, noDocs, Sort.INDEXORDER); terms = q.GetTotalNumberOfTerms(); break; case 1: type = " (constant score boolean rewrite)"; q.SetRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE); topDocs = searcher.Search(q, null, noDocs, Sort.INDEXORDER); terms = q.GetTotalNumberOfTerms(); break; case 2: type = " (filter)"; topDocs = searcher.Search(new MatchAllDocsQuery(), f, noDocs, Sort.INDEXORDER); terms = f.GetTotalNumberOfTerms(); 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); Assert.AreEqual(2 * distance + startOffset, System.Int64.Parse(doc.Get(field)), "First doc" + type); doc = searcher.Doc(sd[sd.Length - 1].doc); Assert.AreEqual((1 + count) * distance + startOffset, System.Int64.Parse(doc.Get(field)), "Last doc" + type); if (i > 0) { Assert.AreEqual(lastTerms, terms, "Distinct term number is equal for all query types"); } lastTerms = terms; } }
/// <summary> Factory that creates a <code>NumericRangeFilter</code>, that queries a <code>long</code> /// range using the default <code>precisionStep</code> {@link NumericUtils#PRECISION_STEP_DEFAULT} (4). /// You can have half-open ranges (which are in fact </≤ or >/≥ 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 NewLongRange(System.String field, System.ValueType min, System.ValueType max, bool minInclusive, bool maxInclusive) { return(new NumericRangeFilter(NumericRangeQuery.NewLongRange(field, min, max, minInclusive, maxInclusive))); }
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++) { long lower = (long)(rnd.NextDouble() * noDocs * distance) + startOffset; long upper = (long)(rnd.NextDouble() * noDocs * distance) + startOffset; if (lower > upper) { long a = lower; lower = upper; upper = a; } // test inclusive range System.Int64 tempAux = (long)lower; System.Int64 tempAux2 = (long)upper; NumericRangeQuery tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux, tempAux2, true, true); TermRangeQuery cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), 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"); termCountT += tq.GetTotalNumberOfTerms(); termCountC += cq.GetTotalNumberOfTerms(); // test exclusive range System.Int64 tempAux3 = (long)lower; System.Int64 tempAux4 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux3, tempAux4, false, false); cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), 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"); termCountT += tq.GetTotalNumberOfTerms(); termCountC += cq.GetTotalNumberOfTerms(); // test left exclusive range System.Int64 tempAux5 = (long)lower; System.Int64 tempAux6 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux5, tempAux6, false, true); cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), 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"); termCountT += tq.GetTotalNumberOfTerms(); termCountC += cq.GetTotalNumberOfTerms(); // test right exclusive range System.Int64 tempAux7 = (long)lower; System.Int64 tempAux8 = (long)upper; tq = NumericRangeQuery.NewLongRange(field, precisionStep, tempAux7, tempAux8, true, false); cq = new TermRangeQuery(field, NumericUtils.LongToPrefixCoded(lower), NumericUtils.LongToPrefixCoded(upper), 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"); termCountT += tq.GetTotalNumberOfTerms(); termCountC += cq.GetTotalNumberOfTerms(); } 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))); } }
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 DoubleField("double", double.NegativeInfinity, Field.Store.NO)); doc.Add(new LongField("long", long.MinValue, Field.Store.NO)); writer.AddDocument(doc); doc = new Document(); doc.Add(new DoubleField("double", double.PositiveInfinity, Field.Store.NO)); doc.Add(new LongField("long", long.MaxValue, Field.Store.NO)); writer.AddDocument(doc); doc = new Document(); doc.Add(new DoubleField("double", 0.0, Field.Store.NO)); doc.Add(new LongField("long", 0L, Field.Store.NO)); writer.AddDocument(doc); foreach (double d in TestNumericUtils.DOUBLE_NANs) { doc = new Document(); doc.Add(new DoubleField("double", d, Field.Store.NO)); writer.AddDocument(doc); } writer.Dispose(); IndexReader r = DirectoryReader.Open(dir); IndexSearcher s = NewSearcher(r); Query q = NumericRangeQuery.NewLongRange("long", null, null, true, true); TopDocs topDocs = s.Search(q, 10); Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count"); q = NumericRangeQuery.NewLongRange("long", null, null, false, false); topDocs = s.Search(q, 10); Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count"); q = NumericRangeQuery.NewLongRange("long", long.MinValue, long.MaxValue, true, true); topDocs = s.Search(q, 10); Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count"); q = NumericRangeQuery.NewLongRange("long", long.MinValue, long.MaxValue, false, false); topDocs = s.Search(q, 10); Assert.AreEqual(1, topDocs.ScoreDocs.Length, "Score doc count"); q = NumericRangeQuery.NewDoubleRange("double", null, null, true, true); topDocs = s.Search(q, 10); Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count"); q = NumericRangeQuery.NewDoubleRange("double", null, null, false, false); topDocs = s.Search(q, 10); Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count"); q = NumericRangeQuery.NewDoubleRange("double", double.NegativeInfinity, double.PositiveInfinity, true, true); topDocs = s.Search(q, 10); Assert.AreEqual(3, topDocs.ScoreDocs.Length, "Score doc count"); q = NumericRangeQuery.NewDoubleRange("double", double.NegativeInfinity, double.PositiveInfinity, false, false); topDocs = s.Search(q, 10); Assert.AreEqual(1, topDocs.ScoreDocs.Length, "Score doc count"); q = NumericRangeQuery.NewDoubleRange("double", double.NaN, double.NaN, true, true); topDocs = s.Search(q, 10); Assert.AreEqual(TestNumericUtils.DOUBLE_NANs.Length, topDocs.ScoreDocs.Length, "Score doc count"); r.Dispose(); dir.Dispose(); }
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)); }
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++) { long lower = (long)(Random().NextDouble() * NoDocs * Distance) + StartOffset; long upper = (long)(Random().NextDouble() * NoDocs * Distance) + StartOffset; if (lower > upper) { long a = lower; lower = upper; upper = a; } BytesRef lowerBytes = new BytesRef(NumericUtils.BUF_SIZE_LONG), upperBytes = new BytesRef(NumericUtils.BUF_SIZE_LONG); NumericUtils.LongToPrefixCodedBytes(lower, 0, lowerBytes); NumericUtils.LongToPrefixCodedBytes(upper, 0, upperBytes); // test inclusive range NumericRangeQuery <long> tq = NumericRangeQuery.NewLongRange(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.NewLongRange(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.NewLongRange(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.NewLongRange(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))); } }