mirror of https://github.com/apache/lucene.git
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:
parent
c4be668e90
commit
19b9b3c96f
|
@ -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).
|
||||
|
|
|
@ -111,7 +111,6 @@ class ConstantScoreAutoRewrite extends TermCollectingRewrite<BooleanQuery> {
|
|||
// Strip scores
|
||||
final Query result = new ConstantScoreQuery(bq);
|
||||
result.setBoost(query.getBoost());
|
||||
query.incTotalNumberOfTerms(size);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
* <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
|
||||
* enum from {@link #getTermsEnum(Terms, AttributeSource)}. For example,
|
||||
|
|
|
@ -77,29 +77,6 @@ public class MultiTermQueryWrapperFilter<Q extends MultiTermQuery> 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.
|
||||
* <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
|
||||
* results.
|
||||
|
@ -146,7 +123,6 @@ public class MultiTermQueryWrapperFilter<Q extends MultiTermQuery> extends Filte
|
|||
} while (termsEnum.next() != null);
|
||||
// System.out.println(" done termCount=" + termCount);
|
||||
|
||||
query.incTotalNumberOfTerms(termCount);
|
||||
return bitSet;
|
||||
} else {
|
||||
return DocIdSet.EMPTY_DOCIDSET;
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
query.incTotalNumberOfTerms(size);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
addClause(q, term, st.termState.docFreq(), query.getBoost() * st.boost, st.termState); // add to query
|
||||
}
|
||||
query.incTotalNumberOfTerms(scoreTerms.length);
|
||||
return q;
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
* <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
|
||||
* 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;
|
||||
}
|
||||
|
|
|
@ -118,35 +118,27 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
|
|||
int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
|
||||
NumericRangeQuery<Integer> q = NumericRangeQuery.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++) {
|
||||
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<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()));
|
||||
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<Integer> 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<Integer> 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<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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Long> q = NumericRangeQuery.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++) {
|
||||
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<Long> 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<Long> 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);
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue