LUCENE-944: Remove deprecated methods in boolean query.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@824870 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Busch 2009-10-13 18:26:37 +00:00
parent 7d0d4ecc44
commit b2852a6186
6 changed files with 77 additions and 152 deletions

View File

@ -65,6 +65,8 @@ API Changes
* LUCENE-1977: Remove deprecated Term field and accessors in * LUCENE-1977: Remove deprecated Term field and accessors in
MultiTermQuery. (Uwe Schindler) MultiTermQuery. (Uwe Schindler)
* LUCENE-944: Remove deprecated methods in BooleanQuery. (Michael Busch)
Bug fixes Bug fixes
* LUCENE-1951: When the text provided to WildcardQuery has no wildcard * LUCENE-1951: When the text provided to WildcardQuery has no wildcard

View File

@ -310,7 +310,6 @@ public class BooleanQuery extends Query {
} }
// Check if we can return a BooleanScorer // Check if we can return a BooleanScorer
scoreDocsInOrder |= !allowDocsOutOfOrder; // until it is removed, factor in the static setting.
if (!scoreDocsInOrder && topScorer && required.size() == 0 && prohibited.size() < 32) { if (!scoreDocsInOrder && topScorer && required.size() == 0 && prohibited.size() < 32) {
return new BooleanScorer(similarity, minNrShouldMatch, optional, prohibited); return new BooleanScorer(similarity, minNrShouldMatch, optional, prohibited);
} }
@ -350,68 +349,6 @@ public class BooleanQuery extends Query {
} }
/**
* Whether hit docs may be collected out of docid order.
*
* @deprecated this will not be needed anymore, as
* {@link Weight#scoresDocsOutOfOrder()} is used.
*/
private static boolean allowDocsOutOfOrder = true;
/**
* Expert: Indicates whether hit docs may be collected out of docid order.
*
* <p>
* Background: although the contract of the Scorer class requires that
* documents be iterated in order of doc id, this was not true in early
* versions of Lucene. Many pieces of functionality in the current Lucene code
* base have undefined behavior if this contract is not upheld, but in some
* specific simple cases may be faster. (For example: disjunction queries with
* less than 32 prohibited clauses; This setting has no effect for other
* queries.)
* </p>
*
* <p>
* Specifics: By setting this option to true, docid N might be scored for a
* single segment before docid N-1. Across multiple segments, docs may be
* scored out of order regardless of this setting - it only applies to scoring
* a single segment.
*
* Being static, this setting is system wide.
* </p>
*
* @deprecated this is not needed anymore, as
* {@link Weight#scoresDocsOutOfOrder()} is used.
*/
public static void setAllowDocsOutOfOrder(boolean allow) {
allowDocsOutOfOrder = allow;
}
/**
* Whether hit docs may be collected out of docid order.
*
* @see #setAllowDocsOutOfOrder(boolean)
* @deprecated this is not needed anymore, as
* {@link Weight#scoresDocsOutOfOrder()} is used.
*/
public static boolean getAllowDocsOutOfOrder() {
return allowDocsOutOfOrder;
}
/**
* @deprecated Use {@link #setAllowDocsOutOfOrder(boolean)} instead.
*/
public static void setUseScorer14(boolean use14) {
setAllowDocsOutOfOrder(use14);
}
/**
* @deprecated Use {@link #getAllowDocsOutOfOrder()} instead.
*/
public static boolean getUseScorer14() {
return getAllowDocsOutOfOrder();
}
public Weight createWeight(Searcher searcher) throws IOException { public Weight createWeight(Searcher searcher) throws IOException {
return new BooleanWeight(searcher); return new BooleanWeight(searcher);
} }

View File

@ -257,7 +257,7 @@ public class QueryUtils {
public static void checkSkipTo(final Query q, final IndexSearcher s) throws IOException { public static void checkSkipTo(final Query q, final IndexSearcher s) throws IOException {
//System.out.println("Checking "+q); //System.out.println("Checking "+q);
if (BooleanQuery.getAllowDocsOutOfOrder()) return; // in this case order of skipTo() might differ from that of next(). if (q.weight(s).scoresDocsOutOfOrder()) return; // in this case order of skipTo() might differ from that of next().
final int skip_op = 0; final int skip_op = 0;
final int next_op = 1; final int next_op = 1;
@ -277,46 +277,46 @@ public class QueryUtils {
// System.out.print(order[i]==skip_op ? " skip()":" next()"); // System.out.print(order[i]==skip_op ? " skip()":" next()");
// System.out.println(); // System.out.println();
final int opidx[] = { 0 }; final int opidx[] = { 0 };
final int lastDoc[] = {-1};
final Weight w = q.weight(s);
final Scorer scorer = w.scorer(s.getIndexReader(), true, false);
if (scorer == null) {
continue;
}
// FUTURE: ensure scorer.doc()==-1 // FUTURE: ensure scorer.doc()==-1
final int[] sdoc = new int[] { -1 };
final float maxDiff = 1e-5f; final float maxDiff = 1e-5f;
s.search(q, new Collector() { s.search(q, new Collector() {
private int base = 0;
private Scorer sc; private Scorer sc;
private IndexReader reader;
private Scorer scorer;
public void setScorer(Scorer scorer) throws IOException { public void setScorer(Scorer scorer) throws IOException {
this.sc = scorer; this.sc = scorer;
} }
public void collect(int doc) throws IOException { public void collect(int doc) throws IOException {
doc = doc + base;
float score = sc.score(); float score = sc.score();
lastDoc[0] = doc;
try { try {
if (scorer == null) {
Weight w = q.weight(s);
scorer = w.scorer(reader, true, false);
}
int op = order[(opidx[0]++) % order.length]; int op = order[(opidx[0]++) % order.length];
// System.out.println(op==skip_op ? // System.out.println(op==skip_op ?
// "skip("+(sdoc[0]+1)+")":"next()"); // "skip("+(sdoc[0]+1)+")":"next()");
boolean more = op == skip_op ? scorer.advance(sdoc[0] + 1) != DocIdSetIterator.NO_MORE_DOCS boolean more = op == skip_op ? scorer.advance(scorer.docID() + 1) != DocIdSetIterator.NO_MORE_DOCS
: scorer.nextDoc() != DocIdSetIterator.NO_MORE_DOCS; : scorer.nextDoc() != DocIdSetIterator.NO_MORE_DOCS;
sdoc[0] = scorer.docID(); int scorerDoc = scorer.docID();
float scorerScore = scorer.score(); float scorerScore = scorer.score();
float scorerScore2 = scorer.score(); float scorerScore2 = scorer.score();
float scoreDiff = Math.abs(score - scorerScore); float scoreDiff = Math.abs(score - scorerScore);
float scorerDiff = Math.abs(scorerScore2 - scorerScore); float scorerDiff = Math.abs(scorerScore2 - scorerScore);
if (!more || doc != sdoc[0] || scoreDiff > maxDiff if (!more || doc != scorerDoc || scoreDiff > maxDiff
|| scorerDiff > maxDiff) { || scorerDiff > maxDiff) {
StringBuilder sbord = new StringBuilder(); StringBuilder sbord = new StringBuilder();
for (int i = 0; i < order.length; i++) for (int i = 0; i < order.length; i++)
sbord.append(order[i] == skip_op ? " skip()" : " next()"); sbord.append(order[i] == skip_op ? " skip()" : " next()");
throw new RuntimeException("ERROR matching docs:" + "\n\t" throw new RuntimeException("ERROR matching docs:" + "\n\t"
+ (doc != sdoc[0] ? "--> " : "") + "doc=" + sdoc[0] + (doc != scorerDoc ? "--> " : "") + "doc=" + doc + ", scorerDoc=" + scorerDoc
+ "\n\t" + (!more ? "--> " : "") + "tscorer.more=" + more + "\n\t" + (!more ? "--> " : "") + "tscorer.more=" + more
+ "\n\t" + (scoreDiff > maxDiff ? "--> " : "") + "\n\t" + (scoreDiff > maxDiff ? "--> " : "")
+ "scorerScore=" + scorerScore + " scoreDiff=" + scoreDiff + "scorerScore=" + scorerScore + " scoreDiff=" + scoreDiff
@ -335,7 +335,9 @@ public class QueryUtils {
} }
public void setNextReader(IndexReader reader, int docBase) { public void setNextReader(IndexReader reader, int docBase) {
base = docBase; this.reader = reader;
this.scorer = null;
lastDoc[0] = -1;
} }
public boolean acceptsDocsOutOfOrder() { public boolean acceptsDocsOutOfOrder() {
@ -343,12 +345,21 @@ public class QueryUtils {
} }
}); });
// make sure next call to scorer is false. List<IndexReader> readerList = new ArrayList<IndexReader>();
int op = order[(opidx[0]++) % order.length]; ReaderUtil.gatherSubReaders(readerList, s.getIndexReader());
// System.out.println(op==skip_op ? "last: skip()":"last: next()"); IndexReader[] readers = (IndexReader[]) readerList.toArray(new IndexReader[0]);
boolean more = (op == skip_op ? scorer.advance(sdoc[0] + 1) : scorer for(int i = 0; i < readers.length; i++) {
.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS; IndexReader reader = readers[i];
Assert.assertFalse(more); Weight w = q.weight(s);
Scorer scorer = w.scorer(reader, true, false);
if (scorer != null) {
boolean more = scorer.advance(lastDoc[0] + 1) != DocIdSetIterator.NO_MORE_DOCS;
if (more && lastDoc[0] != -1)
Assert.assertFalse("query's last doc was "+ lastDoc[0] +" but skipTo("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
}
}
} }
} }
@ -391,7 +402,7 @@ public class QueryUtils {
} }
}); });
List readerList = new ArrayList(); List<IndexReader> readerList = new ArrayList<IndexReader>();
ReaderUtil.gatherSubReaders(readerList, s.getIndexReader()); ReaderUtil.gatherSubReaders(readerList, s.getIndexReader());
IndexReader[] readers = (IndexReader[]) readerList.toArray(new IndexReader[0]); IndexReader[] readers = (IndexReader[]) readerList.toArray(new IndexReader[0]);
for(int i = 0; i < readers.length; i++) { for(int i = 0; i < readers.length; i++) {

View File

@ -66,19 +66,18 @@ public class TestBoolean2 extends LuceneTestCase {
public void queriesTest(String queryText, int[] expDocNrs) throws Exception { public void queriesTest(String queryText, int[] expDocNrs) throws Exception {
//System.out.println(); //System.out.println();
//System.out.println("Query: " + queryText); //System.out.println("Query: " + queryText);
try {
Query query1 = makeQuery(queryText);
BooleanQuery.setAllowDocsOutOfOrder(true);
ScoreDoc[] hits1 = searcher.search(query1, null, 1000).scoreDocs;
Query query2 = makeQuery(queryText); // there should be no need to parse again... Query query1 = makeQuery(queryText);
BooleanQuery.setAllowDocsOutOfOrder(false); TopScoreDocCollector collector = TopScoreDocCollector.create(1000, false);
ScoreDoc[] hits2 = searcher.search(query2, null, 1000).scoreDocs; searcher.search(query1, null, collector);
ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
CheckHits.checkHitsQuery(query2, hits1, hits2, expDocNrs);
} finally { // even when a test fails. Query query2 = makeQuery(queryText); // there should be no need to parse again...
BooleanQuery.setAllowDocsOutOfOrder(false); collector = TopScoreDocCollector.create(1000, true);
} searcher.search(query2, null, collector);
ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
CheckHits.checkHitsQuery(query2, hits1, hits2, expDocNrs);
} }
public void testQueries01() throws Exception { public void testQueries01() throws Exception {
@ -165,14 +164,19 @@ public class TestBoolean2 extends LuceneTestCase {
// match up. // match up.
Sort sort = Sort.INDEXORDER; Sort sort = Sort.INDEXORDER;
BooleanQuery.setAllowDocsOutOfOrder(false);
QueryUtils.check(q1,searcher); QueryUtils.check(q1,searcher);
ScoreDoc[] hits1 = searcher.search(q1,null, 1000, sort).scoreDocs; TopFieldCollector collector = TopFieldCollector.create(sort, 1000,
false, true, true, true);
BooleanQuery.setAllowDocsOutOfOrder(true); searcher.search(q1, null, collector);
ScoreDoc[] hits2 = searcher.search(q1,null, 1000, sort).scoreDocs; ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
collector = TopFieldCollector.create(sort, 1000,
false, true, true, false);
searcher.search(q1, null, collector);
ScoreDoc[] hits2 = collector.topDocs().scoreDocs;
tot+=hits2.length; tot+=hits2.length;
CheckHits.checkEqual(q1, hits1, hits2); CheckHits.checkEqual(q1, hits1, hits2);
} }
@ -181,8 +185,6 @@ public class TestBoolean2 extends LuceneTestCase {
// For easier debugging // For easier debugging
System.out.println("failed query: " + q1); System.out.println("failed query: " + q1);
throw e; throw e;
} finally { // even when a test fails.
BooleanQuery.setAllowDocsOutOfOrder(false);
} }
// System.out.println("Total hits:"+tot); // System.out.println("Total hits:"+tot);

View File

@ -836,11 +836,6 @@ public class TestSort extends LuceneTestCase implements Serializable {
"OutOfOrderOneComparatorScoringMaxScoreCollector" "OutOfOrderOneComparatorScoringMaxScoreCollector"
}; };
// Save the original value to set later.
boolean origVal = BooleanQuery.getAllowDocsOutOfOrder();
BooleanQuery.setAllowDocsOutOfOrder(true);
BooleanQuery bq = new BooleanQuery(); BooleanQuery bq = new BooleanQuery();
// Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2 // Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
// which delegates to BS if there are no mandatory clauses. // which delegates to BS if there are no mandatory clauses.
@ -848,28 +843,20 @@ public class TestSort extends LuceneTestCase implements Serializable {
// Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
// the clause instead of BQ. // the clause instead of BQ.
bq.setMinimumNumberShouldMatch(1); bq.setMinimumNumberShouldMatch(1);
try { for (int i = 0; i < sort.length; i++) {
for (int i = 0; i < sort.length; i++) { for (int j = 0; j < tfcOptions.length; j++) {
for (int j = 0; j < tfcOptions.length; j++) { TopDocsCollector tdc = TopFieldCollector.create(sort[i], 10,
TopDocsCollector tdc = TopFieldCollector.create(sort[i], 10, tfcOptions[j][0], tfcOptions[j][1], tfcOptions[j][2], false);
tfcOptions[j][0], tfcOptions[j][1], tfcOptions[j][2], false);
assertTrue(tdc.getClass().getName().endsWith("$"+actualTFCClasses[j])); assertTrue(tdc.getClass().getName().endsWith("$"+actualTFCClasses[j]));
full.search(bq, tdc); full.search(bq, tdc);
TopDocs td = tdc.topDocs(); TopDocs td = tdc.topDocs();
ScoreDoc[] sd = td.scoreDocs; ScoreDoc[] sd = td.scoreDocs;
assertEquals(10, sd.length); assertEquals(10, sd.length);
}
} }
} finally {
// Whatever happens, reset BooleanQuery.allowDocsOutOfOrder to the
// original value. Don't set it to false in case the implementation in BQ
// will change some day.
BooleanQuery.setAllowDocsOutOfOrder(origVal);
} }
} }
public void testSortWithScoreAndMaxScoreTrackingNoResults() throws Exception { public void testSortWithScoreAndMaxScoreTrackingNoResults() throws Exception {

View File

@ -50,11 +50,6 @@ public class TestTopScoreDocCollector extends LuceneTestCase {
"InOrderTopScoreDocCollector" "InOrderTopScoreDocCollector"
}; };
// Save the original value to set later.
boolean origVal = BooleanQuery.getAllowDocsOutOfOrder();
BooleanQuery.setAllowDocsOutOfOrder(true);
BooleanQuery bq = new BooleanQuery(); BooleanQuery bq = new BooleanQuery();
// Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2 // Add a Query with SHOULD, since bw.scorer() returns BooleanScorer2
// which delegates to BS if there are no mandatory clauses. // which delegates to BS if there are no mandatory clauses.
@ -62,28 +57,19 @@ public class TestTopScoreDocCollector extends LuceneTestCase {
// Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return // Set minNrShouldMatch to 1 so that BQ will not optimize rewrite to return
// the clause instead of BQ. // the clause instead of BQ.
bq.setMinimumNumberShouldMatch(1); bq.setMinimumNumberShouldMatch(1);
try { IndexSearcher searcher = new IndexSearcher(dir, true);
for (int i = 0; i < inOrder.length; i++) {
TopDocsCollector tdc = TopScoreDocCollector.create(3, inOrder[i]);
assertEquals("org.apache.lucene.search.TopScoreDocCollector$" + actualTSDCClass[i], tdc.getClass().getName());
IndexSearcher searcher = new IndexSearcher(dir, true); searcher.search(new MatchAllDocsQuery(), tdc);
for (int i = 0; i < inOrder.length; i++) {
TopDocsCollector tdc = TopScoreDocCollector.create(3, inOrder[i]); ScoreDoc[] sd = tdc.topDocs().scoreDocs;
assertEquals("org.apache.lucene.search.TopScoreDocCollector$" + actualTSDCClass[i], tdc.getClass().getName()); assertEquals(3, sd.length);
for (int j = 0; j < sd.length; j++) {
searcher.search(new MatchAllDocsQuery(), tdc); assertEquals("expected doc Id " + j + " found " + sd[j].doc, j, sd[j].doc);
ScoreDoc[] sd = tdc.topDocs().scoreDocs;
assertEquals(3, sd.length);
for (int j = 0; j < sd.length; j++) {
assertEquals("expected doc Id " + j + " found " + sd[j].doc, j, sd[j].doc);
}
} }
} finally {
// Whatever happens, reset BooleanQuery.allowDocsOutOfOrder to the
// original value. Don't set it to false in case the implementation in BQ
// will change some day.
BooleanQuery.setAllowDocsOutOfOrder(origVal);
} }
} }
} }