optimize boolean filter for one clause case

This commit is contained in:
Shay Banon 2012-12-12 16:32:22 -08:00
parent 8d0d288a1c
commit 4778d5c2eb
1 changed files with 18 additions and 0 deletions

View File

@ -26,6 +26,9 @@ import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.FixedBitSet;
import org.elasticsearch.common.lucene.docset.AllDocIdSet;
import org.elasticsearch.common.lucene.docset.DocIdSets;
import org.elasticsearch.common.lucene.docset.NotDocIdSet;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -52,6 +55,21 @@ public class XBooleanFilter extends Filter implements Iterable<FilterClause> {
FixedBitSet res = null; FixedBitSet res = null;
final AtomicReader reader = context.reader(); final AtomicReader reader = context.reader();
// optimize single case...
if (clauses.size() == 1) {
FilterClause clause = clauses.get(0);
DocIdSet set = clause.getFilter().getDocIdSet(context, acceptDocs);
if (clause.getOccur() == Occur.MUST_NOT) {
if (DocIdSets.isEmpty(set)) {
return new AllDocIdSet(reader.maxDoc());
} else {
return new NotDocIdSet(set, reader.maxDoc());
}
}
// SHOULD or MUST, just return the set...
return set;
}
boolean hasShouldClauses = false; boolean hasShouldClauses = false;
for (final FilterClause fc : clauses) { for (final FilterClause fc : clauses) {
if (fc.getOccur() == Occur.SHOULD) { if (fc.getOccur() == Occur.SHOULD) {