From 4778d5c2eba90025c3d8dff208d5523f9135a8f6 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Wed, 12 Dec 2012 16:32:22 -0800 Subject: [PATCH] optimize boolean filter for one clause case --- .../common/lucene/search/XBooleanFilter.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/org/elasticsearch/common/lucene/search/XBooleanFilter.java b/src/main/java/org/elasticsearch/common/lucene/search/XBooleanFilter.java index 687acdc2d2b..2d9972ca867 100644 --- a/src/main/java/org/elasticsearch/common/lucene/search/XBooleanFilter.java +++ b/src/main/java/org/elasticsearch/common/lucene/search/XBooleanFilter.java @@ -26,6 +26,9 @@ import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.Filter; import org.apache.lucene.util.Bits; 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.util.ArrayList; @@ -52,6 +55,21 @@ public class XBooleanFilter extends Filter implements Iterable { FixedBitSet res = null; 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; for (final FilterClause fc : clauses) { if (fc.getOccur() == Occur.SHOULD) {