diff --git a/modules/elasticsearch/src/main/java/org/apache/lucene/queryParser/MissingFieldQueryExtension.java b/modules/elasticsearch/src/main/java/org/apache/lucene/queryParser/MissingFieldQueryExtension.java index 197494b8853..b6242eadc5e 100644 --- a/modules/elasticsearch/src/main/java/org/apache/lucene/queryParser/MissingFieldQueryExtension.java +++ b/modules/elasticsearch/src/main/java/org/apache/lucene/queryParser/MissingFieldQueryExtension.java @@ -58,6 +58,6 @@ public class MissingFieldQueryExtension implements FieldQueryExtension { filter = wrapSmartNameFilter(filter, smartNameFieldMappers, parseContext); - return new DeletionAwareConstantScoreQuery(filter, true); + return new DeletionAwareConstantScoreQuery(filter); } } diff --git a/modules/elasticsearch/src/main/java/org/apache/lucene/search/DeletionAwareConstantScoreQuery.java b/modules/elasticsearch/src/main/java/org/apache/lucene/search/DeletionAwareConstantScoreQuery.java index 6607d665fbe..fe8c16cf935 100644 --- a/modules/elasticsearch/src/main/java/org/apache/lucene/search/DeletionAwareConstantScoreQuery.java +++ b/modules/elasticsearch/src/main/java/org/apache/lucene/search/DeletionAwareConstantScoreQuery.java @@ -33,15 +33,9 @@ import java.util.Set; // See more: https://issues.apache.org/jira/browse/LUCENE-2468 public class DeletionAwareConstantScoreQuery extends Query { protected final Filter filter; - protected final boolean deletionAware; public DeletionAwareConstantScoreQuery(Filter filter) { - this(filter, false); - } - - public DeletionAwareConstantScoreQuery(Filter filter, boolean deletionAware) { this.filter = filter; - this.deletionAware = deletionAware; } /** @@ -102,7 +96,7 @@ public class DeletionAwareConstantScoreQuery extends Query { public Explanation explain(IndexReader reader, int doc) throws IOException { ConstantScorer cs = new ConstantScorer(similarity, reader, this); - boolean exists = cs.docIdSetIterator.advance(doc) == doc; + boolean exists = cs._innerIter.advance(doc) == doc; ComplexExplanation result = new ComplexExplanation(); @@ -125,8 +119,9 @@ public class DeletionAwareConstantScoreQuery extends Query { protected class ConstantScorer extends Scorer { final IndexReader reader; - final DocIdSetIterator docIdSetIterator; + final DocIdSetIterator _innerIter; final float theScore; + private int doc = -1; public ConstantScorer(Similarity similarity, IndexReader reader, Weight w) throws IOException { super(similarity); @@ -134,35 +129,30 @@ public class DeletionAwareConstantScoreQuery extends Query { theScore = w.getValue(); DocIdSet docIdSet = filter.getDocIdSet(reader); if (docIdSet == null) { - docIdSetIterator = DocIdSet.EMPTY_DOCIDSET.iterator(); + _innerIter = DocIdSet.EMPTY_DOCIDSET.iterator(); } else { DocIdSetIterator iter = docIdSet.iterator(); if (iter == null) { - docIdSetIterator = DocIdSet.EMPTY_DOCIDSET.iterator(); + _innerIter = DocIdSet.EMPTY_DOCIDSET.iterator(); } else { - docIdSetIterator = iter; + _innerIter = iter; } } } @Override public int nextDoc() throws IOException { - if (deletionAware) { - int nextDoc; - while ((nextDoc = docIdSetIterator.nextDoc()) != NO_MORE_DOCS) { - if (!reader.isDeleted(nextDoc)) { - return nextDoc; - } + while ((doc = _innerIter.nextDoc()) != NO_MORE_DOCS) { + if (!reader.isDeleted(doc)) { + return doc; } - return nextDoc; - } else { - return docIdSetIterator.nextDoc(); } + return doc; } @Override public int docID() { - return docIdSetIterator.docID(); + return doc; } @Override @@ -172,23 +162,20 @@ public class DeletionAwareConstantScoreQuery extends Query { @Override public int advance(int target) throws IOException { - if (deletionAware) { - int doc = docIdSetIterator.advance(target); - if (doc == NO_MORE_DOCS) { - return doc; - } + doc = _innerIter.advance(target); + if (doc != NO_MORE_DOCS) { if (!reader.isDeleted(doc)) { return doc; - } - while ((doc = nextDoc()) < target) { - if (!reader.isDeleted(doc)) { - return doc; + } else { + while ((doc = _innerIter.nextDoc()) != NO_MORE_DOCS) { + if (!reader.isDeleted(doc)) { + return doc; + } } + return doc; } - return doc; - } else { - return docIdSetIterator.advance(target); } + return doc; } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/Queries.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/Queries.java index b44dd7d71bf..3fa933b243a 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/Queries.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/Queries.java @@ -30,7 +30,7 @@ import java.util.List; public class Queries { // We don't use MatchAllDocsQuery, its slower than the one below ... (much slower) - public final static Query MATCH_ALL_QUERY = new DeletionAwareConstantScoreQuery(new MatchAllDocsFilter(), true); + public final static Query MATCH_ALL_QUERY = new DeletionAwareConstantScoreQuery(new MatchAllDocsFilter()); /** * A match all docs filter. Note, requires no caching!. diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/ConstantScoreQueryParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/ConstantScoreQueryParser.java index 21029bd19ef..3565dcbc6d7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/ConstantScoreQueryParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/ConstantScoreQueryParser.java @@ -81,7 +81,7 @@ public class ConstantScoreQueryParser extends AbstractIndexComponent implements filter = parseContext.cacheFilter(filter); } - Query query = new DeletionAwareConstantScoreQuery(filter, true); + Query query = new DeletionAwareConstantScoreQuery(filter); query.setBoost(boost); return query; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/FacetsPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/FacetsPhase.java index 91b740ba31f..4dfad884d4e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/FacetsPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/FacetsPhase.java @@ -19,7 +19,10 @@ package org.elasticsearch.search.facet; -import org.apache.lucene.search.*; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.FilterClause; +import org.apache.lucene.search.FilteredQuery; +import org.apache.lucene.search.Query; import org.elasticsearch.ElasticSearchException; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.collect.Lists; @@ -61,7 +64,7 @@ public class FacetsPhase implements SearchPhase { // run global facets ... if (context.searcher().globalCollectors() != null) { - Query query = new DeletionAwareConstantScoreQuery(Queries.MATCH_ALL_FILTER, true); // no need to cache a MATCH ALL FILTER + Query query = Queries.MATCH_ALL_QUERY; if (context.types().length > 0) { if (context.types().length == 1) { String type = context.types()[0]; diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/cache/filter/FilterCacheTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/cache/filter/FilterCacheTests.java index f63b8342ba3..1bb9b801443 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/cache/filter/FilterCacheTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/cache/filter/FilterCacheTests.java @@ -82,7 +82,7 @@ public class FilterCacheTests { long constantScoreCount = filter == cachedFilter ? 0 : 1; // sadly, when caching based on cacheKey with NRT, this fails, that's why we have DeletionAware one assertThat(Lucene.count(searcher, new ConstantScoreQuery(cachedFilter), -1), equalTo(constantScoreCount)); - assertThat(Lucene.count(searcher, new DeletionAwareConstantScoreQuery(cachedFilter, true), -1), equalTo(0l)); + assertThat(Lucene.count(searcher, new DeletionAwareConstantScoreQuery(cachedFilter), -1), equalTo(0l)); assertThat(Lucene.count(searcher, new FilteredQuery(new MatchAllDocsQuery(), cachedFilter), -1), equalTo(0l)); indexWriter.close();