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"); }
/// <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; } }
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))); } }