diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index a8da9bd6c5a..1f60debcfa0 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -730,6 +730,11 @@ API Changes deprecated. IndexCommit.isOptimized was replaced with getSegmentCount. (Robert Muir, Mike McCandless) +* LUCENE-3205: Deprecated MultiTermQuery.getTotalNumerOfTerms() [and + related methods], as the numbers returned are not useful + for multi-segment indexes. They were only needed for tests of + NumericRangeQuery. (Mike McCandless, Uwe Schindler) + New Features * LUCENE-3448: Added FixedBitSet.and(other/DISI), andNot(other/DISI). diff --git a/lucene/src/java/org/apache/lucene/search/ConstantScoreAutoRewrite.java b/lucene/src/java/org/apache/lucene/search/ConstantScoreAutoRewrite.java index 69be445e256..37b82fc1795 100644 --- a/lucene/src/java/org/apache/lucene/search/ConstantScoreAutoRewrite.java +++ b/lucene/src/java/org/apache/lucene/search/ConstantScoreAutoRewrite.java @@ -111,7 +111,6 @@ class ConstantScoreAutoRewrite extends TermCollectingRewrite { // Strip scores final Query result = new ConstantScoreQuery(bq); result.setBoost(query.getBoost()); - query.incTotalNumberOfTerms(size); return result; } } diff --git a/lucene/src/java/org/apache/lucene/search/MultiTermQuery.java b/lucene/src/java/org/apache/lucene/search/MultiTermQuery.java index 899d166a3aa..20e91a905c5 100644 --- a/lucene/src/java/org/apache/lucene/search/MultiTermQuery.java +++ b/lucene/src/java/org/apache/lucene/search/MultiTermQuery.java @@ -64,7 +64,6 @@ import org.apache.lucene.util.TermContext; public abstract class MultiTermQuery extends Query { protected final String field; protected RewriteMethod rewriteMethod = CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; - transient int numberOfTerms = 0; /** Abstract class that defines how the query is rewritten. */ public static abstract class RewriteMethod { @@ -270,38 +269,6 @@ public abstract class MultiTermQuery extends Query { return getTermsEnum(terms, new AttributeSource()); } - /** - * Expert: Return the number of unique terms visited during execution of the query. - * If there are many of them, you may consider using another query type - * or reduce your total term count in index. - *

This method is not thread safe, be sure to only call it when no query is running! - * If you re-use the same query instance for another - * search, be sure to first reset the term counter - * with {@link #clearTotalNumberOfTerms}. - *

On single-segment indexes / no MultiReaders, you get the correct number of - * unique terms for the whole index. Use this number to compare different queries. - * For multi-segment indexes this number can also be achieved in - * non-constant-score mode. In constant-score mode you get the total number of - * terms seeked for all segments / sub-readers. - * @see #clearTotalNumberOfTerms - */ - public int getTotalNumberOfTerms() { - return numberOfTerms; - } - - /** - * Expert: Resets the counting of unique terms. - * Do this before executing the query/filter. - * @see #getTotalNumberOfTerms - */ - public void clearTotalNumberOfTerms() { - numberOfTerms = 0; - } - - protected void incTotalNumberOfTerms(int inc) { - numberOfTerms += inc; - } - /** * To rewrite to a simpler form, instead return a simpler * enum from {@link #getTermsEnum(Terms, AttributeSource)}. For example, diff --git a/lucene/src/java/org/apache/lucene/search/MultiTermQueryWrapperFilter.java b/lucene/src/java/org/apache/lucene/search/MultiTermQueryWrapperFilter.java index 3cd39287aa8..267b7096747 100644 --- a/lucene/src/java/org/apache/lucene/search/MultiTermQueryWrapperFilter.java +++ b/lucene/src/java/org/apache/lucene/search/MultiTermQueryWrapperFilter.java @@ -77,29 +77,6 @@ public class MultiTermQueryWrapperFilter extends Filte /** Returns the field name for this query */ public final String getField() { return query.getField(); } - /** - * Expert: Return the number of unique terms visited during execution of the filter. - * If there are many of them, you may consider using another filter type - * or optimize your total term count in index. - *

This method is not thread safe, be sure to only call it when no filter is running! - * If you re-use the same filter instance for another - * search, be sure to first reset the term counter - * with {@link #clearTotalNumberOfTerms}. - * @see #clearTotalNumberOfTerms - */ - public int getTotalNumberOfTerms() { - return query.getTotalNumberOfTerms(); - } - - /** - * Expert: Resets the counting of unique terms. - * Do this before executing the filter. - * @see #getTotalNumberOfTerms - */ - public void clearTotalNumberOfTerms() { - query.clearTotalNumberOfTerms(); - } - /** * Returns a DocIdSet with documents that should be permitted in search * results. @@ -146,7 +123,6 @@ public class MultiTermQueryWrapperFilter extends Filte } while (termsEnum.next() != null); // System.out.println(" done termCount=" + termCount); - query.incTotalNumberOfTerms(termCount); return bitSet; } else { return DocIdSet.EMPTY_DOCIDSET; diff --git a/lucene/src/java/org/apache/lucene/search/ScoringRewrite.java b/lucene/src/java/org/apache/lucene/search/ScoringRewrite.java index 098d8b4a8b7..736934d463f 100644 --- a/lucene/src/java/org/apache/lucene/search/ScoringRewrite.java +++ b/lucene/src/java/org/apache/lucene/search/ScoringRewrite.java @@ -125,7 +125,6 @@ public abstract class ScoringRewrite extends TermCollectingRewr addClause(result, term, termStates[pos].docFreq(), query.getBoost() * boost[pos], termStates[pos]); } } - query.incTotalNumberOfTerms(size); return result; } diff --git a/lucene/src/java/org/apache/lucene/search/TopTermsRewrite.java b/lucene/src/java/org/apache/lucene/search/TopTermsRewrite.java index 0a75f37e727..0dd862b810c 100644 --- a/lucene/src/java/org/apache/lucene/search/TopTermsRewrite.java +++ b/lucene/src/java/org/apache/lucene/search/TopTermsRewrite.java @@ -163,7 +163,6 @@ public abstract class TopTermsRewrite extends TermCollectingRew assert reader.docFreq(term) == st.termState.docFreq() : "reader DF is " + reader.docFreq(term) + " vs " + st.termState.docFreq() + " term=" + term; addClause(q, term, st.termState.docFreq(), query.getBoost() * st.boost, st.termState); // add to query } - query.incTotalNumberOfTerms(scoreTerms.length); return q; } diff --git a/lucene/src/test/org/apache/lucene/search/FieldCacheRewriteMethod.java b/lucene/src/test/org/apache/lucene/search/FieldCacheRewriteMethod.java index d89bd5b9586..0315b682e0b 100644 --- a/lucene/src/test/org/apache/lucene/search/FieldCacheRewriteMethod.java +++ b/lucene/src/test/org/apache/lucene/search/FieldCacheRewriteMethod.java @@ -82,29 +82,6 @@ public final class FieldCacheRewriteMethod extends MultiTermQuery.RewriteMethod /** Returns the field name for this query */ public final String getField() { return query.getField(); } - /** - * Expert: Return the number of unique terms visited during execution of the filter. - * If there are many of them, you may consider using another filter type - * or reduce your total term count in index. - *

This method is not thread safe, be sure to only call it when no filter is running! - * If you re-use the same filter instance for another - * search, be sure to first reset the term counter - * with {@link #clearTotalNumberOfTerms}. - * @see #clearTotalNumberOfTerms - */ - public int getTotalNumberOfTerms() { - return query.getTotalNumberOfTerms(); - } - - /** - * Expert: Resets the counting of unique terms. - * Do this before executing the filter. - * @see #getTotalNumberOfTerms - */ - public void clearTotalNumberOfTerms() { - query.clearTotalNumberOfTerms(); - } - /** * Returns a DocIdSet with documents that should be permitted in search * results. @@ -158,8 +135,6 @@ public final class FieldCacheRewriteMethod extends MultiTermQuery.RewriteMethod termCount++; } } while (termsEnum.next() != null); - - query.incTotalNumberOfTerms(termCount); } else { return DocIdSet.EMPTY_DOCIDSET; } diff --git a/lucene/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java b/lucene/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java index e04a4f5e0da..657bb6cba46 100644 --- a/lucene/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java +++ b/lucene/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java @@ -118,35 +118,27 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3); NumericRangeQuery q = NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true); NumericRangeFilter f = NumericRangeFilter.newIntRange(field, precisionStep, lower, upper, true, true); - int lastTerms = 0; for (byte i=0; i<3; i++) { TopDocs topDocs; - int terms; 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; } - if (VERBOSE) System.out.println("Found "+terms+" distinct terms in range for field '"+field+"'"+type+"."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count"+type, count, sd.length ); @@ -154,12 +146,6 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { assertEquals("First doc"+type, 2*distance+startOffset, Integer.parseInt(doc.get(field)) ); doc=searcher.doc(sd[sd.length-1].doc); assertEquals("Last doc"+type, (1+count)*distance+startOffset, Integer.parseInt(doc.get(field)) ); - if (i>0 && - (searcher.getIndexReader().getSequentialSubReaders() == null || - searcher.getIndexReader().getSequentialSubReaders().length == 1)) { - assertEquals("Distinct term number is equal for all query types", lastTerms, terms); - } - lastTerms = terms; } } @@ -180,7 +166,7 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { @Test public void testInverseRange() throws Exception { - AtomicReaderContext context = (AtomicReaderContext) new SlowMultiReaderWrapper(searcher.getIndexReader()).getTopReaderContext(); + AtomicReaderContext context = (AtomicReaderContext) new SlowMultiReaderWrapper(reader).getTopReaderContext(); NumericRangeFilter f = NumericRangeFilter.newIntRange("field8", 8, 1000, -1000, true, true); assertSame("A inverse range should return the EMPTY_DOCIDSET instance", DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(context, context.reader.getLiveDocs())); f = NumericRangeFilter.newIntRange("field8", 8, Integer.MAX_VALUE, null, false, false); @@ -207,7 +193,6 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { int upper=(count-1)*distance + (distance/3) + startOffset; NumericRangeQuery q=NumericRangeQuery.newIntRange(field, precisionStep, null, upper, true, true); TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); - if (VERBOSE) System.out.println("Found "+q.getTotalNumberOfTerms()+" distinct terms in left open range for field '"+field+"'."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", count, sd.length ); @@ -248,7 +233,6 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { int lower=(count-1)*distance + (distance/3) +startOffset; NumericRangeQuery q=NumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, true); TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); - if (VERBOSE) System.out.println("Found "+q.getTotalNumberOfTerms()+" distinct terms in right open range for field '"+field+"'."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", noDocs-count, sd.length ); @@ -335,7 +319,7 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception { String field="field"+precisionStep; - int termCountT=0,termCountC=0; + 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; @@ -353,44 +337,73 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { TopDocs tTopDocs = searcher.search(tq, 1); TopDocs cTopDocs = searcher.search(cq, 1); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); - termCountT += tq.getTotalNumberOfTerms(); - termCountC += cq.getTotalNumberOfTerms(); + 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); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); - termCountT += tq.getTotalNumberOfTerms(); - termCountC += cq.getTotalNumberOfTerms(); + 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); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); - termCountT += tq.getTotalNumberOfTerms(); - termCountC += cq.getTotalNumberOfTerms(); + 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); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); - termCountT += tq.getTotalNumberOfTerms(); - termCountC += cq.getTotalNumberOfTerms(); + totalTermCountT += termCountT = countTerms(tq); + totalTermCountC += termCountC = countTerms(cq); + checkTermCounts(precisionStep, termCountT, termCountC); } - if (precisionStep == Integer.MAX_VALUE && - (searcher.getIndexReader().getSequentialSubReaders() == null || - searcher.getIndexReader().getSequentialSubReaders().length == 1)) { - assertEquals("Total number of terms should be equal for unlimited precStep", termCountT, termCountC); - } else if (VERBOSE) { + + checkTermCounts(precisionStep, totalTermCountT, totalTermCountC); + if (VERBOSE && precisionStep != Integer.MAX_VALUE) { System.out.println("Average number of terms during random search on '" + field + "':"); - System.out.println(" Trie query: " + (((double)termCountT)/(num * 4))); - System.out.println(" Classical query: " + (((double)termCountC)/(num * 4))); + System.out.println(" Numeric query: " + (((double)totalTermCountT)/(num * 4))); + System.out.println(" Classical query: " + (((double)totalTermCountC)/(num * 4))); } } + private int countTerms(MultiTermQuery q) throws Exception { + final Terms terms = MultiFields.getTerms(reader, q.getField()); + if (terms == null) + return 0; + final TermsEnum termEnum = q.getTermsEnum(terms); + assertNotNull(termEnum); + int count = 0; + BytesRef cur, last = null; + while ((cur = termEnum.next()) != null) { + count++; + if (last != null) { + assertTrue(last.compareTo(cur) < 0); + } + last = new BytesRef(cur); + } + assertNull(termEnum.next()); + return count; + } + + private void checkTermCounts(int precisionStep, int termCountT, int termCountC) { + if (precisionStep == Integer.MAX_VALUE) { + assertEquals("Number of terms should be equal for unlimited precStep", termCountC, termCountT); + } else { + assertTrue("Number of terms for NRQ should be <= compared to classical TRQ", termCountT <= termCountC); + } + } + @Test public void testRandomTrieAndClassicRangeQuery_8bit() throws Exception { testRandomTrieAndClassicRangeQuery(8); @@ -566,40 +579,4 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { assertFalse(q2.equals(q1)); } - private void testEnum(int lower, int upper) throws Exception { - NumericRangeQuery q = NumericRangeQuery.newIntRange("field4", 4, - lower, upper, true, true); - Terms terms = MultiFields.getTerms(searcher.getIndexReader(), "field4"); - TermsEnum termEnum = q.getTermsEnum(terms); - int count = 0; - while (termEnum.next() != null) { - final BytesRef t = termEnum.term(); - if (t != null) { - final int val = NumericUtils.prefixCodedToInt(t); - assertTrue("value not in bounds " + val + " >= " + lower + " && " - + val + " <= " + upper, val >= lower && val <= upper); - count++; - } else - break; - } - assertNull(termEnum.next()); - if (VERBOSE) System.out.println("TermEnum on 'field4' for range [" + lower + "," + upper - + "] contained " + count + " terms."); - - } - - @Test - public void testEnum() throws Exception { - int count=3000; - int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3); - // test enum with values - testEnum(lower, upper); - // test empty enum - testEnum(upper, lower); - // test empty enum outside of bounds - lower = distance*noDocs+startOffset; - upper = 2 * lower; - testEnum(lower, upper); - } - } diff --git a/lucene/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java b/lucene/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java index 8d8abc55ae5..b7bc0598fd8 100644 --- a/lucene/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java +++ b/lucene/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java @@ -22,8 +22,11 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.NumericField; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.MultiFields; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.SlowMultiReaderWrapper; +import org.apache.lucene.index.Terms; +import org.apache.lucene.index.TermsEnum; import org.apache.lucene.index.IndexReader.AtomicReaderContext; import org.apache.lucene.store.Directory; import org.apache.lucene.util.BytesRef; @@ -118,35 +121,27 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { long lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3); NumericRangeQuery q = NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true); NumericRangeFilter f = NumericRangeFilter.newLongRange(field, precisionStep, lower, upper, true, true); - int lastTerms = 0; for (byte i=0; i<3; i++) { TopDocs topDocs; - int terms; 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; } - if (VERBOSE) System.out.println("Found "+terms+" distinct terms in range for field '"+field+"'"+type+"."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count"+type, count, sd.length ); @@ -154,12 +149,6 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { assertEquals("First doc"+type, 2*distance+startOffset, Long.parseLong(doc.get(field)) ); doc=searcher.doc(sd[sd.length-1].doc); assertEquals("Last doc"+type, (1+count)*distance+startOffset, Long.parseLong(doc.get(field)) ); - if (i>0 && - (searcher.getIndexReader().getSequentialSubReaders() == null || - searcher.getIndexReader().getSequentialSubReaders().length == 1)) { - assertEquals("Distinct term number is equal for all query types", lastTerms, terms); - } - lastTerms = terms; } } @@ -213,7 +202,6 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { long upper=(count-1)*distance + (distance/3) + startOffset; NumericRangeQuery q=NumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true); TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); - if (VERBOSE) System.out.println("Found "+q.getTotalNumberOfTerms()+" distinct terms in left open range for field '"+field+"'."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", count, sd.length ); @@ -259,7 +247,6 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { long lower=(count-1)*distance + (distance/3) +startOffset; NumericRangeQuery q=NumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, true); TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); - if (VERBOSE) System.out.println("Found "+q.getTotalNumberOfTerms()+" distinct terms in right open range for field '"+field+"'."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", noDocs-count, sd.length ); @@ -352,7 +339,7 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception { String field="field"+precisionStep; - int termCountT=0,termCountC=0; + 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; @@ -370,44 +357,73 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { TopDocs tTopDocs = searcher.search(tq, 1); TopDocs cTopDocs = searcher.search(cq, 1); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); - termCountT += tq.getTotalNumberOfTerms(); - termCountC += cq.getTotalNumberOfTerms(); + 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); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); - termCountT += tq.getTotalNumberOfTerms(); - termCountC += cq.getTotalNumberOfTerms(); + 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); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); - termCountT += tq.getTotalNumberOfTerms(); - termCountC += cq.getTotalNumberOfTerms(); + 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); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); - termCountT += tq.getTotalNumberOfTerms(); - termCountC += cq.getTotalNumberOfTerms(); + totalTermCountT += termCountT = countTerms(tq); + totalTermCountC += termCountC = countTerms(cq); + checkTermCounts(precisionStep, termCountT, termCountC); } - if (precisionStep == Integer.MAX_VALUE && - (searcher.getIndexReader().getSequentialSubReaders() == null || - searcher.getIndexReader().getSequentialSubReaders().length == 1)) { - assertEquals("Total number of terms should be equal for unlimited precStep", termCountT, termCountC); - } else if (VERBOSE) { + + checkTermCounts(precisionStep, totalTermCountT, totalTermCountC); + if (VERBOSE && precisionStep != Integer.MAX_VALUE) { System.out.println("Average number of terms during random search on '" + field + "':"); - System.out.println(" Trie query: " + (((double)termCountT)/(num * 4))); - System.out.println(" Classical query: " + (((double)termCountC)/(num * 4))); + System.out.println(" Numeric query: " + (((double)totalTermCountT)/(num * 4))); + System.out.println(" Classical query: " + (((double)totalTermCountC)/(num * 4))); } } + private int countTerms(MultiTermQuery q) throws Exception { + final Terms terms = MultiFields.getTerms(reader, q.getField()); + if (terms == null) + return 0; + final TermsEnum termEnum = q.getTermsEnum(terms); + assertNotNull(termEnum); + int count = 0; + BytesRef cur, last = null; + while ((cur = termEnum.next()) != null) { + count++; + if (last != null) { + assertTrue(last.compareTo(cur) < 0); + } + last = new BytesRef(cur); + } + assertNull(termEnum.next()); + return count; + } + + private void checkTermCounts(int precisionStep, int termCountT, int termCountC) { + if (precisionStep == Integer.MAX_VALUE) { + assertEquals("Number of terms should be equal for unlimited precStep", termCountC, termCountT); + } else { + assertTrue("Number of terms for NRQ should be <= compared to classical TRQ", termCountT <= termCountC); + } + } + @Test public void testRandomTrieAndClassicRangeQuery_8bit() throws Exception { testRandomTrieAndClassicRangeQuery(8); diff --git a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/ReadTask.java b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/ReadTask.java index e3065b1c044..ec742efccd3 100644 --- a/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/ReadTask.java +++ b/modules/benchmark/src/java/org/apache/lucene/benchmark/byTask/tasks/ReadTask.java @@ -136,9 +136,6 @@ public abstract class ReadTask extends PerfTask { final String printHitsField = getRunData().getConfig().get("print.hits.field", null); if (hits != null && printHitsField != null && printHitsField.length() > 0) { - if (q instanceof MultiTermQuery) { - System.out.println("MultiTermQuery term count = " + ((MultiTermQuery) q).getTotalNumberOfTerms()); - } System.out.println("totalHits = " + hits.totalHits); System.out.println("maxDoc() = " + reader.maxDoc()); System.out.println("numDocs() = " + reader.numDocs());