diff --git a/src/main/java/org/elasticsearch/index/query/NestedFilterParser.java b/src/main/java/org/elasticsearch/index/query/NestedFilterParser.java index e4b7ad00886..538152a39bc 100644 --- a/src/main/java/org/elasticsearch/index/query/NestedFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/NestedFilterParser.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; +import org.apache.lucene.search.join.FixedBitSetCachingWrapperFilter; import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.join.ToParentBlockJoinQuery; import org.elasticsearch.common.Strings; @@ -150,6 +151,9 @@ public class NestedFilterParser implements FilterParser { //} parentFilter = parseContext.cacheFilter(parentFilter, null); } + // if the filter cache is disabled, then we still have a filter that is not cached while ToParentBlockJoinQuery + // expects FixedBitSet instances + parentFilter = new FixedBitSetCachingWrapperFilter(parentFilter); Filter nestedFilter; if (join) { diff --git a/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java b/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java index b710d039477..9d73346b0c7 100644 --- a/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/NestedQueryParser.java @@ -23,6 +23,7 @@ import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.Filter; import org.apache.lucene.search.Query; +import org.apache.lucene.search.join.FixedBitSetCachingWrapperFilter; import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.join.ToParentBlockJoinQuery; import org.apache.lucene.util.Bits; @@ -154,6 +155,9 @@ public class NestedQueryParser implements QueryParser { //} parentFilter = parseContext.cacheFilter(parentFilter, null); } + // if the filter cache is disabled, then we still have a filter that is not cached while ToParentBlockJoinQuery + // expects FixedBitSet instances + parentFilter = new FixedBitSetCachingWrapperFilter(parentFilter); ToParentBlockJoinQuery joinQuery = new ToParentBlockJoinQuery(query, parentFilter, scoreMode); joinQuery.setBoost(boost); diff --git a/src/main/java/org/elasticsearch/index/search/nested/IncludeNestedDocsQuery.java b/src/main/java/org/elasticsearch/index/search/nested/IncludeNestedDocsQuery.java index 03e2b2b675b..cf74def8cb7 100644 --- a/src/main/java/org/elasticsearch/index/search/nested/IncludeNestedDocsQuery.java +++ b/src/main/java/org/elasticsearch/index/search/nested/IncludeNestedDocsQuery.java @@ -25,6 +25,7 @@ import org.apache.lucene.index.Term; import org.apache.lucene.search.*; import org.apache.lucene.util.Bits; import org.apache.lucene.util.FixedBitSet; +import org.elasticsearch.common.lucene.docset.DocIdSets; import java.io.IOException; import java.util.Collection; @@ -117,7 +118,13 @@ public class IncludeNestedDocsQuery extends Query { return null; } if (!(parents instanceof FixedBitSet)) { - throw new IllegalStateException("parentFilter must return FixedBitSet; got " + parents); + if (parents.isCacheable()) { + // the filter is cached, yet not with the right type + throw new IllegalStateException("parentFilter must return FixedBitSet; got " + parents); + } else { + // may happen if the filter cache type is none + parents = DocIdSets.toFixedBitSet(parents.iterator(), context.reader().maxDoc()); + } } int firstParentDoc = parentScorer.nextDoc(); diff --git a/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java b/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java index f42421fd720..ef455b6aeb4 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java +++ b/src/main/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregator.java @@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.bucket.nested; import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.Filter; +import org.apache.lucene.search.join.FixedBitSetCachingWrapperFilter; import org.apache.lucene.util.Bits; import org.apache.lucene.util.FixedBitSet; import org.elasticsearch.common.lucene.ReaderContextAware; @@ -82,6 +83,8 @@ public class NestedAggregator extends SingleBucketAggregator implements ReaderCo parentFilterNotCached = closestNestedAggregator.childFilter; } parentFilter = SearchContext.current().filterCache().cache(parentFilterNotCached); + // if the filter cache is disabled, we still need to produce bit sets + parentFilter = new FixedBitSetCachingWrapperFilter(parentFilter); } try {