properly handle cases where filter returns null docIdSet with and/or/not filters

This commit is contained in:
kimchy 2011-04-19 21:28:08 +03:00
parent 5cc943c7db
commit 8e027b3baf
3 changed files with 16 additions and 0 deletions

View File

@ -53,6 +53,9 @@ public class AndFilter extends Filter {
boolean allAreDocSet = true;
for (Filter filter : filters) {
DocIdSet set = filter.getDocIdSet(reader);
if (set == null) { // none matching for this filter, we AND, so return EMPTY
return DocSet.EMPTY_DOC_SET;
}
if (!(set instanceof DocSet)) {
allAreDocSet = false;
}

View File

@ -22,6 +22,7 @@ package org.elasticsearch.common.lucene.search;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
import org.elasticsearch.common.lucene.docset.AllDocSet;
import org.elasticsearch.common.lucene.docset.DocSet;
import org.elasticsearch.common.lucene.docset.NotDocIdSet;
import org.elasticsearch.common.lucene.docset.NotDocSet;
@ -45,6 +46,9 @@ public class NotFilter extends Filter {
@Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
DocIdSet set = filter.getDocIdSet(reader);
if (set == null) {
return new AllDocSet(reader.maxDoc());
}
if (set instanceof DocSet) {
return new NotDocSet((DocSet) set, reader.maxDoc());
}

View File

@ -53,11 +53,20 @@ public class OrFilter extends Filter {
boolean allAreDocSet = true;
for (Filter filter : filters) {
DocIdSet set = filter.getDocIdSet(reader);
if (set == null) { // none matching for this filter, continue
continue;
}
if (!(set instanceof DocSet)) {
allAreDocSet = false;
}
sets.add(set);
}
if (sets.size() == 0) {
return DocSet.EMPTY_DOC_SET;
}
if (sets.size() == 1) {
return (DocIdSet) sets.get(0);
}
if (allAreDocSet) {
return new OrDocSet(sets);
}