From 8e027b3baf77b13bb7108930ce66c87bdc8770ae Mon Sep 17 00:00:00 2001 From: kimchy Date: Tue, 19 Apr 2011 21:28:08 +0300 Subject: [PATCH] properly handle cases where filter returns null docIdSet with and/or/not filters --- .../elasticsearch/common/lucene/search/AndFilter.java | 3 +++ .../elasticsearch/common/lucene/search/NotFilter.java | 4 ++++ .../org/elasticsearch/common/lucene/search/OrFilter.java | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/AndFilter.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/AndFilter.java index 5ee5ee2a064..29d902b79d2 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/AndFilter.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/AndFilter.java @@ -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; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/NotFilter.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/NotFilter.java index 8902d15b4d6..3e5ade23c86 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/NotFilter.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/NotFilter.java @@ -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()); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/OrFilter.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/OrFilter.java index dbb8937f51a..286d992cb43 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/OrFilter.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/OrFilter.java @@ -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); }