LUCENE-3205: Remove MultiTermQuery get/inc/clear totalNumberOfTerms

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1201329 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2011-11-12 21:13:20 +00:00
parent c4be668e90
commit 19b9b3c96f
10 changed files with 99 additions and 189 deletions

View File

@ -730,6 +730,11 @@ API Changes
deprecated. IndexCommit.isOptimized was replaced with deprecated. IndexCommit.isOptimized was replaced with
getSegmentCount. (Robert Muir, Mike McCandless) 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 New Features
* LUCENE-3448: Added FixedBitSet.and(other/DISI), andNot(other/DISI). * LUCENE-3448: Added FixedBitSet.and(other/DISI), andNot(other/DISI).

View File

@ -111,7 +111,6 @@ class ConstantScoreAutoRewrite extends TermCollectingRewrite<BooleanQuery> {
// Strip scores // Strip scores
final Query result = new ConstantScoreQuery(bq); final Query result = new ConstantScoreQuery(bq);
result.setBoost(query.getBoost()); result.setBoost(query.getBoost());
query.incTotalNumberOfTerms(size);
return result; return result;
} }
} }

View File

@ -64,7 +64,6 @@ import org.apache.lucene.util.TermContext;
public abstract class MultiTermQuery extends Query { public abstract class MultiTermQuery extends Query {
protected final String field; protected final String field;
protected RewriteMethod rewriteMethod = CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; protected RewriteMethod rewriteMethod = CONSTANT_SCORE_AUTO_REWRITE_DEFAULT;
transient int numberOfTerms = 0;
/** Abstract class that defines how the query is rewritten. */ /** Abstract class that defines how the query is rewritten. */
public static abstract class RewriteMethod { public static abstract class RewriteMethod {
@ -270,38 +269,6 @@ public abstract class MultiTermQuery extends Query {
return getTermsEnum(terms, new AttributeSource()); 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.
* <p>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}.
* <p>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 * To rewrite to a simpler form, instead return a simpler
* enum from {@link #getTermsEnum(Terms, AttributeSource)}. For example, * enum from {@link #getTermsEnum(Terms, AttributeSource)}. For example,

View File

@ -77,29 +77,6 @@ public class MultiTermQueryWrapperFilter<Q extends MultiTermQuery> extends Filte
/** Returns the field name for this query */ /** Returns the field name for this query */
public final String getField() { return query.getField(); } 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.
* <p>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 * Returns a DocIdSet with documents that should be permitted in search
* results. * results.
@ -146,7 +123,6 @@ public class MultiTermQueryWrapperFilter<Q extends MultiTermQuery> extends Filte
} while (termsEnum.next() != null); } while (termsEnum.next() != null);
// System.out.println(" done termCount=" + termCount); // System.out.println(" done termCount=" + termCount);
query.incTotalNumberOfTerms(termCount);
return bitSet; return bitSet;
} else { } else {
return DocIdSet.EMPTY_DOCIDSET; return DocIdSet.EMPTY_DOCIDSET;

View File

@ -125,7 +125,6 @@ public abstract class ScoringRewrite<Q extends Query> extends TermCollectingRewr
addClause(result, term, termStates[pos].docFreq(), query.getBoost() * boost[pos], termStates[pos]); addClause(result, term, termStates[pos].docFreq(), query.getBoost() * boost[pos], termStates[pos]);
} }
} }
query.incTotalNumberOfTerms(size);
return result; return result;
} }

View File

@ -163,7 +163,6 @@ public abstract class TopTermsRewrite<Q extends Query> extends TermCollectingRew
assert reader.docFreq(term) == st.termState.docFreq() : "reader DF is " + reader.docFreq(term) + " vs " + st.termState.docFreq() + " term=" + term; 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 addClause(q, term, st.termState.docFreq(), query.getBoost() * st.boost, st.termState); // add to query
} }
query.incTotalNumberOfTerms(scoreTerms.length);
return q; return q;
} }

View File

@ -82,29 +82,6 @@ public final class FieldCacheRewriteMethod extends MultiTermQuery.RewriteMethod
/** Returns the field name for this query */ /** Returns the field name for this query */
public final String getField() { return query.getField(); } 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.
* <p>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 * Returns a DocIdSet with documents that should be permitted in search
* results. * results.
@ -158,8 +135,6 @@ public final class FieldCacheRewriteMethod extends MultiTermQuery.RewriteMethod
termCount++; termCount++;
} }
} while (termsEnum.next() != null); } while (termsEnum.next() != null);
query.incTotalNumberOfTerms(termCount);
} else { } else {
return DocIdSet.EMPTY_DOCIDSET; return DocIdSet.EMPTY_DOCIDSET;
} }

View File

@ -118,35 +118,27 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3); int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
NumericRangeQuery<Integer> q = NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true); NumericRangeQuery<Integer> q = NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true);
NumericRangeFilter<Integer> f = NumericRangeFilter.newIntRange(field, precisionStep, lower, upper, true, true); NumericRangeFilter<Integer> f = NumericRangeFilter.newIntRange(field, precisionStep, lower, upper, true, true);
int lastTerms = 0;
for (byte i=0; i<3; i++) { for (byte i=0; i<3; i++) {
TopDocs topDocs; TopDocs topDocs;
int terms;
String type; String type;
q.clearTotalNumberOfTerms();
f.clearTotalNumberOfTerms();
switch (i) { switch (i) {
case 0: case 0:
type = " (constant score filter rewrite)"; type = " (constant score filter rewrite)";
q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
terms = q.getTotalNumberOfTerms();
break; break;
case 1: case 1:
type = " (constant score boolean rewrite)"; type = " (constant score boolean rewrite)";
q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE); q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
terms = q.getTotalNumberOfTerms();
break; break;
case 2: case 2:
type = " (filter)"; type = " (filter)";
topDocs = searcher.search(new MatchAllDocsQuery(), f, noDocs, Sort.INDEXORDER); topDocs = searcher.search(new MatchAllDocsQuery(), f, noDocs, Sort.INDEXORDER);
terms = f.getTotalNumberOfTerms();
break; break;
default: default:
return; return;
} }
if (VERBOSE) System.out.println("Found "+terms+" distinct terms in range for field '"+field+"'"+type+".");
ScoreDoc[] sd = topDocs.scoreDocs; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count"+type, count, sd.length ); 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)) ); assertEquals("First doc"+type, 2*distance+startOffset, Integer.parseInt(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc); doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc"+type, (1+count)*distance+startOffset, Integer.parseInt(doc.get(field)) ); 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 @Test
public void testInverseRange() throws Exception { public void testInverseRange() throws Exception {
AtomicReaderContext context = (AtomicReaderContext) new SlowMultiReaderWrapper(searcher.getIndexReader()).getTopReaderContext(); AtomicReaderContext context = (AtomicReaderContext) new SlowMultiReaderWrapper(reader).getTopReaderContext();
NumericRangeFilter<Integer> f = NumericRangeFilter.newIntRange("field8", 8, 1000, -1000, true, true); NumericRangeFilter<Integer> 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())); 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); 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; int upper=(count-1)*distance + (distance/3) + startOffset;
NumericRangeQuery<Integer> q=NumericRangeQuery.newIntRange(field, precisionStep, null, upper, true, true); NumericRangeQuery<Integer> q=NumericRangeQuery.newIntRange(field, precisionStep, null, upper, true, true);
TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); 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; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count", count, sd.length ); assertEquals("Score doc count", count, sd.length );
@ -248,7 +233,6 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
int lower=(count-1)*distance + (distance/3) +startOffset; int lower=(count-1)*distance + (distance/3) +startOffset;
NumericRangeQuery<Integer> q=NumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, true); NumericRangeQuery<Integer> q=NumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, true);
TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); 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; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count", noDocs-count, sd.length ); assertEquals("Score doc count", noDocs-count, sd.length );
@ -335,7 +319,7 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception { private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception {
String field="field"+precisionStep; String field="field"+precisionStep;
int termCountT=0,termCountC=0; int totalTermCountT=0,totalTermCountC=0,termCountT,termCountC;
int num = _TestUtil.nextInt(random, 10, 20); int num = _TestUtil.nextInt(random, 10, 20);
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
int lower=(int)(random.nextDouble()*noDocs*distance)+startOffset; int lower=(int)(random.nextDouble()*noDocs*distance)+startOffset;
@ -353,44 +337,73 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
TopDocs tTopDocs = searcher.search(tq, 1); TopDocs tTopDocs = searcher.search(tq, 1);
TopDocs cTopDocs = searcher.search(cq, 1); TopDocs cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
termCountT += tq.getTotalNumberOfTerms(); totalTermCountT += termCountT = countTerms(tq);
termCountC += cq.getTotalNumberOfTerms(); totalTermCountC += termCountC = countTerms(cq);
checkTermCounts(precisionStep, termCountT, termCountC);
// test exclusive range // test exclusive range
tq=NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, false); tq=NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, false);
cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, false); cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, false);
tTopDocs = searcher.search(tq, 1); tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1); cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
termCountT += tq.getTotalNumberOfTerms(); totalTermCountT += termCountT = countTerms(tq);
termCountC += cq.getTotalNumberOfTerms(); totalTermCountC += termCountC = countTerms(cq);
checkTermCounts(precisionStep, termCountT, termCountC);
// test left exclusive range // test left exclusive range
tq=NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, true); tq=NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, true);
cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, true); cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, true);
tTopDocs = searcher.search(tq, 1); tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1); cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
termCountT += tq.getTotalNumberOfTerms(); totalTermCountT += termCountT = countTerms(tq);
termCountC += cq.getTotalNumberOfTerms(); totalTermCountC += termCountC = countTerms(cq);
checkTermCounts(precisionStep, termCountT, termCountC);
// test right exclusive range // test right exclusive range
tq=NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, false); tq=NumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, false);
cq=new TermRangeQuery(field, lowerBytes, upperBytes, true, false); cq=new TermRangeQuery(field, lowerBytes, upperBytes, true, false);
tTopDocs = searcher.search(tq, 1); tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1); cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
termCountT += tq.getTotalNumberOfTerms(); totalTermCountT += termCountT = countTerms(tq);
termCountC += cq.getTotalNumberOfTerms(); totalTermCountC += termCountC = countTerms(cq);
checkTermCounts(precisionStep, termCountT, termCountC);
} }
if (precisionStep == Integer.MAX_VALUE &&
(searcher.getIndexReader().getSequentialSubReaders() == null || checkTermCounts(precisionStep, totalTermCountT, totalTermCountC);
searcher.getIndexReader().getSequentialSubReaders().length == 1)) { if (VERBOSE && precisionStep != Integer.MAX_VALUE) {
assertEquals("Total number of terms should be equal for unlimited precStep", termCountT, termCountC);
} else if (VERBOSE) {
System.out.println("Average number of terms during random search on '" + field + "':"); System.out.println("Average number of terms during random search on '" + field + "':");
System.out.println(" Trie query: " + (((double)termCountT)/(num * 4))); System.out.println(" Numeric query: " + (((double)totalTermCountT)/(num * 4)));
System.out.println(" Classical query: " + (((double)termCountC)/(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 @Test
public void testRandomTrieAndClassicRangeQuery_8bit() throws Exception { public void testRandomTrieAndClassicRangeQuery_8bit() throws Exception {
testRandomTrieAndClassicRangeQuery(8); testRandomTrieAndClassicRangeQuery(8);
@ -566,40 +579,4 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
assertFalse(q2.equals(q1)); assertFalse(q2.equals(q1));
} }
private void testEnum(int lower, int upper) throws Exception {
NumericRangeQuery<Integer> 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);
}
} }

View File

@ -22,8 +22,11 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.NumericField; import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.MultiFields;
import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.SlowMultiReaderWrapper; 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.index.IndexReader.AtomicReaderContext;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef; 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); long lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
NumericRangeQuery<Long> q = NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true); NumericRangeQuery<Long> q = NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);
NumericRangeFilter<Long> f = NumericRangeFilter.newLongRange(field, precisionStep, lower, upper, true, true); NumericRangeFilter<Long> f = NumericRangeFilter.newLongRange(field, precisionStep, lower, upper, true, true);
int lastTerms = 0;
for (byte i=0; i<3; i++) { for (byte i=0; i<3; i++) {
TopDocs topDocs; TopDocs topDocs;
int terms;
String type; String type;
q.clearTotalNumberOfTerms();
f.clearTotalNumberOfTerms();
switch (i) { switch (i) {
case 0: case 0:
type = " (constant score filter rewrite)"; type = " (constant score filter rewrite)";
q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
terms = q.getTotalNumberOfTerms();
break; break;
case 1: case 1:
type = " (constant score boolean rewrite)"; type = " (constant score boolean rewrite)";
q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE); q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
terms = q.getTotalNumberOfTerms();
break; break;
case 2: case 2:
type = " (filter)"; type = " (filter)";
topDocs = searcher.search(new MatchAllDocsQuery(), f, noDocs, Sort.INDEXORDER); topDocs = searcher.search(new MatchAllDocsQuery(), f, noDocs, Sort.INDEXORDER);
terms = f.getTotalNumberOfTerms();
break; break;
default: default:
return; return;
} }
if (VERBOSE) System.out.println("Found "+terms+" distinct terms in range for field '"+field+"'"+type+".");
ScoreDoc[] sd = topDocs.scoreDocs; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count"+type, count, sd.length ); 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)) ); assertEquals("First doc"+type, 2*distance+startOffset, Long.parseLong(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc); doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc"+type, (1+count)*distance+startOffset, Long.parseLong(doc.get(field)) ); 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; long upper=(count-1)*distance + (distance/3) + startOffset;
NumericRangeQuery<Long> q=NumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true); NumericRangeQuery<Long> q=NumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true);
TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); 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; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count", count, sd.length ); assertEquals("Score doc count", count, sd.length );
@ -259,7 +247,6 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
long lower=(count-1)*distance + (distance/3) +startOffset; long lower=(count-1)*distance + (distance/3) +startOffset;
NumericRangeQuery<Long> q=NumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, true); NumericRangeQuery<Long> q=NumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, true);
TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); 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; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count", noDocs-count, sd.length ); assertEquals("Score doc count", noDocs-count, sd.length );
@ -352,7 +339,7 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception { private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception {
String field="field"+precisionStep; String field="field"+precisionStep;
int termCountT=0,termCountC=0; int totalTermCountT=0,totalTermCountC=0,termCountT,termCountC;
int num = _TestUtil.nextInt(random, 10, 20); int num = _TestUtil.nextInt(random, 10, 20);
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
long lower=(long)(random.nextDouble()*noDocs*distance)+startOffset; long lower=(long)(random.nextDouble()*noDocs*distance)+startOffset;
@ -370,44 +357,73 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
TopDocs tTopDocs = searcher.search(tq, 1); TopDocs tTopDocs = searcher.search(tq, 1);
TopDocs cTopDocs = searcher.search(cq, 1); TopDocs cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
termCountT += tq.getTotalNumberOfTerms(); totalTermCountT += termCountT = countTerms(tq);
termCountC += cq.getTotalNumberOfTerms(); totalTermCountC += termCountC = countTerms(cq);
checkTermCounts(precisionStep, termCountT, termCountC);
// test exclusive range // test exclusive range
tq=NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, false); tq=NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, false);
cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, false); cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, false);
tTopDocs = searcher.search(tq, 1); tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1); cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
termCountT += tq.getTotalNumberOfTerms(); totalTermCountT += termCountT = countTerms(tq);
termCountC += cq.getTotalNumberOfTerms(); totalTermCountC += termCountC = countTerms(cq);
checkTermCounts(precisionStep, termCountT, termCountC);
// test left exclusive range // test left exclusive range
tq=NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, true); tq=NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, true);
cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, true); cq=new TermRangeQuery(field, lowerBytes, upperBytes, false, true);
tTopDocs = searcher.search(tq, 1); tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1); cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
termCountT += tq.getTotalNumberOfTerms(); totalTermCountT += termCountT = countTerms(tq);
termCountC += cq.getTotalNumberOfTerms(); totalTermCountC += termCountC = countTerms(cq);
checkTermCounts(precisionStep, termCountT, termCountC);
// test right exclusive range // test right exclusive range
tq=NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, false); tq=NumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, false);
cq=new TermRangeQuery(field, lowerBytes, upperBytes, true, false); cq=new TermRangeQuery(field, lowerBytes, upperBytes, true, false);
tTopDocs = searcher.search(tq, 1); tTopDocs = searcher.search(tq, 1);
cTopDocs = searcher.search(cq, 1); cTopDocs = searcher.search(cq, 1);
assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits ); assertEquals("Returned count for NumericRangeQuery and TermRangeQuery must be equal", cTopDocs.totalHits, tTopDocs.totalHits );
termCountT += tq.getTotalNumberOfTerms(); totalTermCountT += termCountT = countTerms(tq);
termCountC += cq.getTotalNumberOfTerms(); totalTermCountC += termCountC = countTerms(cq);
checkTermCounts(precisionStep, termCountT, termCountC);
} }
if (precisionStep == Integer.MAX_VALUE &&
(searcher.getIndexReader().getSequentialSubReaders() == null || checkTermCounts(precisionStep, totalTermCountT, totalTermCountC);
searcher.getIndexReader().getSequentialSubReaders().length == 1)) { if (VERBOSE && precisionStep != Integer.MAX_VALUE) {
assertEquals("Total number of terms should be equal for unlimited precStep", termCountT, termCountC);
} else if (VERBOSE) {
System.out.println("Average number of terms during random search on '" + field + "':"); System.out.println("Average number of terms during random search on '" + field + "':");
System.out.println(" Trie query: " + (((double)termCountT)/(num * 4))); System.out.println(" Numeric query: " + (((double)totalTermCountT)/(num * 4)));
System.out.println(" Classical query: " + (((double)termCountC)/(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 @Test
public void testRandomTrieAndClassicRangeQuery_8bit() throws Exception { public void testRandomTrieAndClassicRangeQuery_8bit() throws Exception {
testRandomTrieAndClassicRangeQuery(8); testRandomTrieAndClassicRangeQuery(8);

View File

@ -136,9 +136,6 @@ public abstract class ReadTask extends PerfTask {
final String printHitsField = getRunData().getConfig().get("print.hits.field", null); final String printHitsField = getRunData().getConfig().get("print.hits.field", null);
if (hits != null && printHitsField != null && printHitsField.length() > 0) { 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("totalHits = " + hits.totalHits);
System.out.println("maxDoc() = " + reader.maxDoc()); System.out.println("maxDoc() = " + reader.maxDoc());
System.out.println("numDocs() = " + reader.numDocs()); System.out.println("numDocs() = " + reader.numDocs());