diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/docset/AllDocSet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/docset/AllDocSet.java new file mode 100644 index 00000000000..cccb1af57d2 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/docset/AllDocSet.java @@ -0,0 +1,80 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.lucene.docset; + +import org.apache.lucene.search.DocIdSetIterator; + +import java.io.IOException; + +/** + * An always positive + * + * @author kimchy (shay.banon) + */ +public class AllDocSet extends DocSet { + + private final int maxDoc; + + public AllDocSet(int maxDoc) { + this.maxDoc = maxDoc; + } + + @Override public boolean isCacheable() { + return true; + } + + @Override public boolean get(int doc) throws IOException { + return doc < maxDoc; + } + + @Override public DocIdSetIterator iterator() throws IOException { + return new AllDocIdSetIterator(maxDoc); + } + + private final class AllDocIdSetIterator extends DocIdSetIterator { + + private final int maxDoc; + + private int doc = -1; + + private AllDocIdSetIterator(int maxDoc) { + this.maxDoc = maxDoc; + } + + @Override public int docID() { + return doc; + } + + @Override public int nextDoc() throws IOException { + if (++doc < maxDoc) { + return doc; + } + return NO_MORE_DOCS; + } + + @Override public int advance(int target) throws IOException { + doc = target; + if (doc < maxDoc) { + return doc; + } + return NO_MORE_DOCS; + } + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilter.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilter.java new file mode 100644 index 00000000000..263d16ea966 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilter.java @@ -0,0 +1,58 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +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 java.io.IOException; + +/** + * A filter that matches on all docs. + * + * @author kimchy (shay.banon) + */ +public class MatchAllDocsFilter extends Filter { + + @Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException { + return new AllDocSet(reader.maxDoc()); + } + + @Override public int hashCode() { + return this.getClass().hashCode(); + } + + @Override public boolean equals(Object obj) { + if (this == obj) + return true; + + if (obj == null) { + return false; + } + + if (obj.getClass() == this.getClass()) { + return true; + } + + return false; + } +} 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 35705f8fffd..9aa1fc66d5e 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 @@ -31,7 +31,10 @@ public class Queries { public final static MatchAllDocsQuery MATCH_ALL_QUERY = new MatchAllDocsQuery(); - public final static QueryWrapperFilter MATCH_ALL_FILTER = new QueryWrapperFilter(MATCH_ALL_QUERY); + /** + * A match all docs filter. Note, requires no caching!. + */ + public final static MatchAllDocsFilter MATCH_ALL_FILTER = new MatchAllDocsFilter(); private final static Field disjuncts; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java index 4e383a3610d..fadc96c1c1a 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/FacetsPhase.java @@ -61,7 +61,7 @@ public class FacetsPhase implements SearchPhase { // run global facets ... if (context.searcher().globalCollectors() != null) { - Query query = new DeletionAwareConstantScoreQuery(context.filterCache().cache(Queries.MATCH_ALL_FILTER)); + Query query = new DeletionAwareConstantScoreQuery(Queries.MATCH_ALL_FILTER); // no need to cache a MATCH ALL FILTER 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/common/lucene/search/MatchAllDocsFilterTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilterTests.java new file mode 100644 index 00000000000..ab3ff7e42c9 --- /dev/null +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/common/lucene/search/MatchAllDocsFilterTests.java @@ -0,0 +1,57 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.lucene.search; + +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.search.DeletionAwareConstantScoreQuery; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.RAMDirectory; +import org.elasticsearch.common.lucene.Lucene; +import org.testng.annotations.Test; + +import static org.elasticsearch.common.lucene.DocumentBuilder.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +/** + * @author kimchy (shay.banon) + */ +public class MatchAllDocsFilterTests { + + @Test public void testMatchAllDocsFilter() throws Exception { + Directory dir = new RAMDirectory(); + IndexWriter indexWriter = new IndexWriter(dir, Lucene.STANDARD_ANALYZER, true, IndexWriter.MaxFieldLength.UNLIMITED); + + indexWriter.addDocument(doc().add(field("_id", "1")).add(field("text", "lucene")).build()); + indexWriter.addDocument(doc().add(field("_id", "2")).add(field("text", "lucene release")).build()); + + IndexReader reader = indexWriter.getReader(); + IndexSearcher searcher = new IndexSearcher(reader); + + DeletionAwareConstantScoreQuery query = new DeletionAwareConstantScoreQuery(Queries.MATCH_ALL_FILTER); + long count = Lucene.count(searcher, query, -1); + assertThat(count, equalTo(2l)); + + reader.close(); + indexWriter.close(); + } +} diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/engine/EngineSearcherTotalHitsMatcher.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/engine/EngineSearcherTotalHitsMatcher.java index c62af1fb311..4ea071a1478 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/engine/EngineSearcherTotalHitsMatcher.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/engine/EngineSearcherTotalHitsMatcher.java @@ -19,9 +19,9 @@ package org.elasticsearch.index.engine; -import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.common.lucene.Lucene; +import org.elasticsearch.common.lucene.search.Queries; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; @@ -60,6 +60,6 @@ public final class EngineSearcherTotalHitsMatcher extends TypeSafeMatcher engineSearcherTotalHits(int totalHits) { - return new EngineSearcherTotalHitsMatcher(new MatchAllDocsQuery(), totalHits); + return new EngineSearcherTotalHitsMatcher(Queries.MATCH_ALL_QUERY, totalHits); } }