LUCENE-7874: DisjunctionMaxQuery rewrites to a BooleanQuery when tiebreaker is set to 1.

This commit is contained in:
Jim Ferenczi 2017-06-16 17:06:59 +02:00
parent 0c683305a4
commit 68d29c9b4f
3 changed files with 25 additions and 0 deletions

View File

@ -112,6 +112,8 @@ Optimizations
query is a point (for 2D) or a is a simple date interval (e.g. 1 month). When
the strategy is marked as pointsOnly, the results is a TermQuery. (David Smiley)
* LUCENE-7874: DisjunctionMaxQuery rewrites to a BooleanQuery when tiebreaker is set to 1. (Jim Ferenczi)
Other
* LUCENE-7328: Remove LegacyNumericEncoding from GeoPointField. (Nick Knize)

View File

@ -178,6 +178,14 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>
return disjuncts[0];
}
if (tieBreakerMultiplier == 1.0f) {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (Query sub : disjuncts) {
builder.add(sub, BooleanClause.Occur.SHOULD);
}
return builder.build();
}
boolean actuallyRewritten = false;
List<Query> rewrittenDisjuncts = new ArrayList<>();
for (Query sub : disjuncts) {

View File

@ -524,6 +524,21 @@ public class TestDisjunctionMaxQuery extends LuceneTestCase {
}
}
public void testRewriteBoolean() throws Exception {
Query sub1 = tq("hed", "albino");
Query sub2 = tq("hed", "elephant");
DisjunctionMaxQuery q = new DisjunctionMaxQuery(
Arrays.asList(
sub1, sub2
), 1.0f);
Query rewritten = s.rewrite(q);
assertTrue(rewritten instanceof BooleanQuery);
BooleanQuery bq = (BooleanQuery) rewritten;
assertEquals(bq.clauses().size(), 2);
assertEquals(bq.clauses().get(0), new BooleanClause(sub1, BooleanClause.Occur.SHOULD));
assertEquals(bq.clauses().get(1), new BooleanClause(sub2, BooleanClause.Occur.SHOULD));
}
/** macro */
protected Query tq(String f, String t) {
return new TermQuery(new Term(f, t));