From b3d191832cc355149c67e4c095fd08aae5ad7d4e Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Mon, 12 Oct 2015 12:15:07 +0000 Subject: [PATCH] LUCENE-6301: Removal of org.apache.lucene.Filter. From a Lucene perspective Filter is gone. However it was still used for things like DocSet and SolrConstantScoreQuery in Solr, so it has been moved to the oas.search package for now, even though in the long term it would be nice for Solr to move to the Query API entirely as well. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1708097 13f79535-47bb-0310-9956-ffa450edef68 --- lucene/CHANGES.txt | 5 + lucene/MIGRATE.txt | 8 +- .../search/FilteredDocIdSetIterator.java | 3 +- .../apache/lucene/search/package-info.java | 5 +- .../lucene/search/JustCompileSearch.java | 29 --- .../org/apache/lucene/search/MockFilter.java | 47 ----- .../lucene/search/SingleDocTestFilter.java | 59 ------ .../search/TestCachingWrapperQuery.java | 51 ++++- .../lucene/search/TestConstantScoreQuery.java | 60 ++---- .../apache/lucene/search/TestDateFilter.java | 197 ------------------ .../apache/lucene/search/TestNeedsScores.java | 7 - ...olicy.java => TestQueryCachingPolicy.java} | 10 +- .../apache/lucene/search/TestScorerPerf.java | 24 ++- .../apache/lucene/search/TestSortRandom.java | 48 +++-- .../expressions/TestExpressionSorts.java | 2 - .../lucene/facet/TestDrillSideways.java | 63 +++--- .../search/grouping/GroupingSearchTest.java | 6 +- .../search/highlight/HighlighterTest.java | 11 +- .../vectorhighlight/FieldQueryTest.java | 28 --- .../lucene/search/join/package-info.java | 2 +- .../search/join/TestCheckJoinIndex.java | 5 +- .../search/BlockJoinComparatorSource.java | 17 +- .../lucene/index/TestBlockJoinSorter.java | 78 +------ .../uninverting/TestFieldCacheSortRandom.java | 51 +++-- .../lucene/search/TestTermAutomatonQuery.java | 35 ++-- .../accumulator/FacetingAccumulator.java | 2 +- .../analytics/request/AnalyticsStats.java | 2 +- .../TestHierarchicalDocBuilder.java | 3 +- .../handler/component/ExpandComponent.java | 2 +- .../org/apache/solr/query/SolrRangeQuery.java | 4 +- .../apache/solr/request/DocValuesFacets.java | 2 +- .../apache/solr/request/DocValuesStats.java | 2 +- .../apache/solr/request/IntervalFacets.java | 2 +- .../PerSegmentSingleValuedFaceting.java | 2 +- .../org/apache/solr/request/SimpleFacets.java | 2 +- .../transform/ChildDocTransformerFactory.java | 2 +- .../org/apache/solr/schema/CurrencyField.java | 4 +- .../org/apache/solr/search/BitDocSet.java | 2 - .../solr}/search/BitsFilteredDocIdSet.java | 8 +- .../java/org/apache/solr/search/DocSet.java | 1 - .../org/apache/solr/search/DocSetBase.java | 2 - .../org/apache/solr/search/DocSetUtil.java | 5 - .../java/org/apache/solr}/search/Filter.java | 11 +- .../apache/solr}/search/FilteredDocIdSet.java | 5 +- .../java/org/apache/solr/search/Grouping.java | 1 - .../apache/solr/search/HashQParserPlugin.java | 49 ++--- .../apache/solr/search/JoinQParserPlugin.java | 1 - .../solr}/search/QueryWrapperFilter.java | 10 +- .../solr/search/SolrConstantScoreQuery.java | 2 - .../org/apache/solr/search/SolrFilter.java | 1 - .../apache/solr/search/SortedIntDocSet.java | 2 - .../solr/search/TermsQParserPlugin.java | 2 - .../apache/solr/search/facet/FacetField.java | 2 +- .../function/ValueSourceRangeFilter.java | 2 +- .../solr/search/grouping/CommandHandler.java | 2 - .../search/join/BlockJoinParentQParser.java | 9 +- .../apache/solr/search/join/GraphQuery.java | 2 +- .../org/apache/solr/search/TestDocSet.java | 1 - .../solr/search/TestFilteredDocIdSet.java | 12 +- .../solr}/search/TestQueryWrapperFilter.java | 15 +- .../test/org/apache/solr/search/TestSort.java | 3 - .../solr/update/AddBlockUpdateTest.java | 6 +- 62 files changed, 313 insertions(+), 723 deletions(-) delete mode 100644 lucene/core/src/test/org/apache/lucene/search/MockFilter.java delete mode 100644 lucene/core/src/test/org/apache/lucene/search/SingleDocTestFilter.java delete mode 100644 lucene/core/src/test/org/apache/lucene/search/TestDateFilter.java rename lucene/core/src/test/org/apache/lucene/search/{TestFilterCachingPolicy.java => TestQueryCachingPolicy.java} (86%) rename {lucene/core/src/java/org/apache/lucene => solr/core/src/java/org/apache/solr}/search/BitsFilteredDocIdSet.java (92%) rename {lucene/core/src/java/org/apache/lucene => solr/core/src/java/org/apache/solr}/search/Filter.java (92%) rename {lucene/core/src/java/org/apache/lucene => solr/core/src/java/org/apache/solr}/search/FilteredDocIdSet.java (94%) rename {lucene/core/src/java/org/apache/lucene => solr/core/src/java/org/apache/solr}/search/QueryWrapperFilter.java (88%) rename lucene/core/src/test/org/apache/lucene/search/TestDocIdSet.java => solr/core/src/test/org/apache/solr/search/TestFilteredDocIdSet.java (94%) rename {lucene/core/src/test/org/apache/lucene => solr/core/src/test/org/apache/solr}/search/TestQueryWrapperFilter.java (92%) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 348f025bf30..26d99b29a21 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -99,6 +99,11 @@ API Changes * LUCENE-6803: Deprecate sandbox Regexp Query. (Uwe Schindler) +* LUCENE-6301: org.apache.lucene.search.Filter is now deprecated. You should use + Query objects instead of Filters, and the BooleanClause.Occur.FILTER clause in + order to let Lucene know that a Query should be used for filtering but not + scoring. + Optimizations * LUCENE-6708: TopFieldCollector does not compute the score several times on the diff --git a/lucene/MIGRATE.txt b/lucene/MIGRATE.txt index 8e1f770a673..98d7bc1f1e9 100644 --- a/lucene/MIGRATE.txt +++ b/lucene/MIGRATE.txt @@ -31,10 +31,12 @@ situations where some documents do not have values for fields wrapped in other ValueSources. Users who want to preserve the previous behavior may need to wrap their ValueSources in a "DefFunction" along with a ConstValueSource of "0.0". -## Removal of FilteredQuery (LUCENE-6583) +## Removal of Filter and FilteredQuery (LUCENE-6301,LUCENE-6583) -FilteredQuery has been removed. Instead, you can construct a BooleanQuery with -one MUST clause for the query, and one FILTER clause for the filter. +Filter and FilteredQuery have been removed. Regular queries can be used instead +of filters as they have been optimized for the filtering case. And you can +construct a BooleanQuery with one MUST clause for the query, and one FILTER +clause for the filter in order to have similar behaviour to FilteredQuery. ## PhraseQuery and BooleanQuery made immutable (LUCENE-6531 LUCENE-6570) diff --git a/lucene/core/src/java/org/apache/lucene/search/FilteredDocIdSetIterator.java b/lucene/core/src/java/org/apache/lucene/search/FilteredDocIdSetIterator.java index 92a8735f7e7..016788fdd35 100644 --- a/lucene/core/src/java/org/apache/lucene/search/FilteredDocIdSetIterator.java +++ b/lucene/core/src/java/org/apache/lucene/search/FilteredDocIdSetIterator.java @@ -22,8 +22,7 @@ import java.io.IOException; /** * Abstract decorator class of a DocIdSetIterator * implementation that provides on-demand filter/validation - * mechanism on an underlying DocIdSetIterator. See {@link - * FilteredDocIdSet}. + * mechanism on an underlying DocIdSetIterator. */ public abstract class FilteredDocIdSetIterator extends DocIdSetIterator { protected DocIdSetIterator _innerIter; diff --git a/lucene/core/src/java/org/apache/lucene/search/package-info.java b/lucene/core/src/java/org/apache/lucene/search/package-info.java index 899eb316f32..107a47f6421 100644 --- a/lucene/core/src/java/org/apache/lucene/search/package-info.java +++ b/lucene/core/src/java/org/apache/lucene/search/package-info.java @@ -500,8 +500,6 @@ * Weight object is an internal representation of the Query that allows the Query * to be reused by the IndexSearcher. *
  • The IndexSearcher that initiated the call.
  • - *
  • A {@link org.apache.lucene.search.Filter Filter} for limiting the result set. - * Note, the Filter may be null.
  • *
  • A {@link org.apache.lucene.search.Sort Sort} object for specifying how to sort * the results if the standard score-based sort method is not desired.
  • * @@ -509,8 +507,7 @@ * we call one of the search methods of the IndexSearcher, passing in the * {@link org.apache.lucene.search.Weight Weight} object created by * {@link org.apache.lucene.search.IndexSearcher#createNormalizedWeight(org.apache.lucene.search.Query,boolean) - * IndexSearcher.createNormalizedWeight(Query,boolean)}, - * {@link org.apache.lucene.search.Filter Filter} and the number of results we want. + * IndexSearcher.createNormalizedWeight(Query,boolean)} and the number of results we want. * This method returns a {@link org.apache.lucene.search.TopDocs TopDocs} object, * which is an internal collection of search results. The IndexSearcher creates * a {@link org.apache.lucene.search.TopScoreDocCollector TopScoreDocCollector} and diff --git a/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java b/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java index f6ccdc258a6..699803f096f 100644 --- a/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java +++ b/lucene/core/src/test/org/apache/lucene/search/JustCompileSearch.java @@ -24,7 +24,6 @@ import org.apache.lucene.index.FieldInvertState; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.Term; import org.apache.lucene.search.similarities.Similarity; -import org.apache.lucene.util.Bits; import org.apache.lucene.util.PriorityQueue; /** @@ -130,34 +129,6 @@ final class JustCompileSearch { } - static final class JustCompileFilter extends Filter { - // Filter is just an abstract class with no abstract methods. However it is - // still added here in case someone will add abstract methods in the future. - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) { - return null; - } - - @Override - public String toString(String field) { - return "JustCompileFilter"; - } - } - - static final class JustCompileFilteredDocIdSet extends FilteredDocIdSet { - - public JustCompileFilteredDocIdSet(DocIdSet innerSet) { - super(innerSet); - } - - @Override - protected boolean match(int docid) { - throw new UnsupportedOperationException(UNSUPPORTED_MSG); - } - - } - static final class JustCompileFilteredDocIdSetIterator extends FilteredDocIdSetIterator { public JustCompileFilteredDocIdSetIterator(DocIdSetIterator innerIter) { diff --git a/lucene/core/src/test/org/apache/lucene/search/MockFilter.java b/lucene/core/src/test/org/apache/lucene/search/MockFilter.java deleted file mode 100644 index 9333226fd1e..00000000000 --- a/lucene/core/src/test/org/apache/lucene/search/MockFilter.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.apache.lucene.search; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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. - */ - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.util.BitDocIdSet; -import org.apache.lucene.util.FixedBitSet; -import org.apache.lucene.util.Bits; - -public class MockFilter extends Filter { - private boolean wasCalled; - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) { - wasCalled = true; - FixedBitSet bits = new FixedBitSet(context.reader().maxDoc()); - return new BitDocIdSet(bits); - } - - @Override - public String toString(String field) { - return "MockFilter"; - } - - public void clear() { - wasCalled = false; - } - - public boolean wasCalled() { - return wasCalled; - } -} diff --git a/lucene/core/src/test/org/apache/lucene/search/SingleDocTestFilter.java b/lucene/core/src/test/org/apache/lucene/search/SingleDocTestFilter.java deleted file mode 100644 index eb6f64018f1..00000000000 --- a/lucene/core/src/test/org/apache/lucene/search/SingleDocTestFilter.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.apache.lucene.search; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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. - */ - -import java.io.IOException; - -import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.BitDocIdSet; -import org.apache.lucene.util.FixedBitSet; - -public class SingleDocTestFilter extends Filter { - private int doc; - - public SingleDocTestFilter(int doc) { - this.doc = doc; - } - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - FixedBitSet bits = new FixedBitSet(context.reader().maxDoc()); - bits.set(doc); - if (acceptDocs != null && !acceptDocs.get(doc)) bits.clear(doc); - return new BitDocIdSet(bits); - } - - @Override - public String toString(String field) { - return "SingleDocTestFilter(" + doc + ")"; - } - - @Override - public boolean equals(Object obj) { - if (super.equals(obj) == false) { - return false; - } - return doc == ((SingleDocTestFilter) obj).doc; - } - - @Override - public int hashCode() { - return 31 * super.hashCode() + doc; - } -} diff --git a/lucene/core/src/test/org/apache/lucene/search/TestCachingWrapperQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestCachingWrapperQuery.java index 9015e372911..1431cc084e0 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestCachingWrapperQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestCachingWrapperQuery.java @@ -18,6 +18,7 @@ package org.apache.lucene.search; */ import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; @@ -39,7 +40,7 @@ public class TestCachingWrapperQuery extends LuceneTestCase { DirectoryReader ir; IndexSearcher is; RandomIndexWriter iw; - + @Override public void setUp() throws Exception { super.setUp(); @@ -60,7 +61,7 @@ public class TestCachingWrapperQuery extends LuceneTestCase { ir = iw.getReader(); is = newSearcher(ir); } - + @Override public void tearDown() throws Exception { iw.close(); @@ -80,14 +81,14 @@ public class TestCachingWrapperQuery extends LuceneTestCase { assertEquals(hits3.totalHits, hits4.totalHits); CheckHits.checkEqual(f1, hits3.scoreDocs, hits4.scoreDocs); } - + /** test null iterator */ public void testEmpty() throws Exception { BooleanQuery.Builder expected = new BooleanQuery.Builder(); Query cached = new CachingWrapperQuery(expected.build(), MAYBE_CACHE_POLICY); assertQueryEquals(expected.build(), cached); } - + /** test iterator returns NO_MORE_DOCS */ public void testEmpty2() throws Exception { BooleanQuery.Builder expected = new BooleanQuery.Builder(); @@ -96,7 +97,7 @@ public class TestCachingWrapperQuery extends LuceneTestCase { Query cached = new CachingWrapperQuery(expected.build(), MAYBE_CACHE_POLICY); assertQueryEquals(expected.build(), cached); } - + /** test iterator returns single document */ public void testSingle() throws Exception { for (int i = 0; i < 10; i++) { @@ -106,7 +107,7 @@ public class TestCachingWrapperQuery extends LuceneTestCase { assertQueryEquals(expected, cached); } } - + /** test sparse filters (match single documents) */ public void testSparse() throws Exception { for (int i = 0; i < 10; i++) { @@ -118,15 +119,43 @@ public class TestCachingWrapperQuery extends LuceneTestCase { assertQueryEquals(expected, cached); } } - + /** test dense filters (match entire index) */ public void testDense() throws Exception { - Query query = new MatchAllDocsQuery(); - Filter expected = new QueryWrapperFilter(query); + Query expected = new MatchAllDocsQuery(); Query cached = new CachingWrapperQuery(expected, MAYBE_CACHE_POLICY); assertQueryEquals(expected, cached); } + private static class MockQuery extends Query { + + private final AtomicBoolean wasCalled = new AtomicBoolean(); + + public boolean wasCalled() { + return wasCalled.get(); + } + + public void clear() { + wasCalled.set(false); + } + + @Override + public String toString(String field) { + return "Mock"; + } + + @Override + public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + return new ConstantScoreWeight(this) { + @Override + public Scorer scorer(LeafReaderContext context) throws IOException { + wasCalled.set(true); + return new ConstantScoreScorer(this, score(), DocIdSetIterator.all(context.reader().maxDoc())); + } + }; + } + } + public void testCachingWorks() throws Exception { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); @@ -135,7 +164,7 @@ public class TestCachingWrapperQuery extends LuceneTestCase { IndexReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(dir)); IndexSearcher searcher = newSearcher(reader); LeafReaderContext context = (LeafReaderContext) reader.getContext(); - MockFilter filter = new MockFilter(); + MockQuery filter = new MockQuery(); CachingWrapperQuery cacher = new CachingWrapperQuery(filter, QueryCachingPolicy.ALWAYS_CACHE); // first time, nested filter is called @@ -257,7 +286,7 @@ public class TestCachingWrapperQuery extends LuceneTestCase { reader = refreshReader(reader); searcher = newSearcher(reader, false); - + docs = searcher.search(new ConstantScoreQuery(query), 1); assertEquals("[query + filter] Should find 2 hits...", 2, docs.totalHits); assertTrue(query.missCount > missCount); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java index ff5820a38f0..65a269772a3 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java @@ -26,14 +26,12 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.MultiReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.similarities.ClassicSimilarity; import org.apache.lucene.store.Directory; -import org.apache.lucene.util.Bits; import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; @@ -130,31 +128,32 @@ public class TestConstantScoreQuery extends LuceneTestCase { } } - // a filter for which other queries don't have special rewrite rules - private static class FilterWrapper extends Filter { + // a query for which other queries don't have special rewrite rules + private static class QueryWrapper extends Query { - private final Filter in; - - FilterWrapper(Filter in) { + private final Query in; + + QueryWrapper(Query in) { this.in = in; } - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - return in.getDocIdSet(context, acceptDocs); - } @Override public String toString(String field) { - return in.toString(field); + return "MockQuery"; } + @Override + public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + return in.createWeight(searcher, needsScores); + } + @Override public boolean equals(Object obj) { if (super.equals(obj) == false) { return false; } - return in.equals(((FilterWrapper) obj).in); + QueryWrapper that = (QueryWrapper) obj; + return in.equals(that.in); } @Override @@ -175,7 +174,7 @@ public class TestConstantScoreQuery extends LuceneTestCase { IndexReader r = w.getReader(); w.close(); - Filter filterB = new FilterWrapper(new QueryWrapperFilter(new TermQuery(new Term("field", "b")))); + Query filterB = new QueryWrapper(new TermQuery(new Term("field", "b"))); Query query = new ConstantScoreQuery(filterB); IndexSearcher s = newSearcher(r); @@ -185,7 +184,7 @@ public class TestConstantScoreQuery extends LuceneTestCase { .build(); assertEquals(1, s.search(filtered, 1).totalHits); // Query for field:b, Filter field:b - Filter filterA = new FilterWrapper(new QueryWrapperFilter(new TermQuery(new Term("field", "a")))); + Query filterA = new QueryWrapper(new TermQuery(new Term("field", "a"))); query = new ConstantScoreQuery(filterA); filtered = new BooleanQuery.Builder() @@ -198,35 +197,6 @@ public class TestConstantScoreQuery extends LuceneTestCase { d.close(); } - // LUCENE-5307 - // don't reuse the scorer of filters since they have been created with bulkScorer=false - public void testQueryWrapperFilter() throws IOException { - Directory d = newDirectory(); - RandomIndexWriter w = new RandomIndexWriter(random(), d); - Document doc = new Document(); - doc.add(newStringField("field", "a", Field.Store.NO)); - w.addDocument(doc); - IndexReader r = w.getReader(); - w.close(); - - final Query wrapped = AssertingQuery.wrap(random(), new TermQuery(new Term("field", "a"))); - Filter filter = new QueryWrapperFilter(wrapped); - IndexSearcher s = newSearcher(r); - assert s instanceof AssertingIndexSearcher; - // this used to fail - s.search(new ConstantScoreQuery(filter), new TotalHitCountCollector()); - - // check the rewrite - Query rewritten = filter; - for (Query q = rewritten.rewrite(r); q != rewritten; q = rewritten.rewrite(r)) { - rewritten = q; - } - assertEquals(new BoostQuery(new ConstantScoreQuery(wrapped), 0), rewritten); - - r.close(); - d.close(); - } - public void testPropagatesApproximations() throws IOException { Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestDateFilter.java b/lucene/core/src/test/org/apache/lucene/search/TestDateFilter.java deleted file mode 100644 index f4b3e532f81..00000000000 --- a/lucene/core/src/test/org/apache/lucene/search/TestDateFilter.java +++ /dev/null @@ -1,197 +0,0 @@ -package org.apache.lucene.search; - -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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. - */ - -import org.apache.lucene.document.Field; -import org.apache.lucene.search.BooleanClause.Occur; -import org.apache.lucene.store.Directory; -import org.apache.lucene.util.LuceneTestCase; -import org.apache.lucene.document.DateTools; -import org.apache.lucene.document.Document; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.RandomIndexWriter; -import org.apache.lucene.index.Term; - -import java.io.IOException; - -/** - * DateFilter JUnit tests. - * - * - */ -public class TestDateFilter extends LuceneTestCase { - - /** - * - */ - public void testBefore() throws IOException { - // create an index - Directory indexStore = newDirectory(); - RandomIndexWriter writer = new RandomIndexWriter(random(), indexStore); - - long now = System.currentTimeMillis(); - - Document doc = new Document(); - // add time that is in the past - doc.add(newStringField("datefield", DateTools.timeToString(now - 1000, DateTools.Resolution.MILLISECOND), Field.Store.YES)); - doc.add(newTextField("body", "Today is a very sunny day in New York City", Field.Store.YES)); - writer.addDocument(doc); - - IndexReader reader = writer.getReader(); - writer.close(); - IndexSearcher searcher = newSearcher(reader); - - // filter that should preserve matches - // DateFilter df1 = DateFilter.Before("datefield", now); - Filter df1 = new QueryWrapperFilter(TermRangeQuery.newStringRange("datefield", DateTools - .timeToString(now - 2000, DateTools.Resolution.MILLISECOND), DateTools - .timeToString(now, DateTools.Resolution.MILLISECOND), false, true)); - // filter that should discard matches - // DateFilter df2 = DateFilter.Before("datefield", now - 999999); - Filter df2 = new QueryWrapperFilter(TermRangeQuery.newStringRange("datefield", DateTools - .timeToString(0, DateTools.Resolution.MILLISECOND), DateTools - .timeToString(now - 2000, DateTools.Resolution.MILLISECOND), true, - false)); - - // search something that doesn't exist with DateFilter - Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); - - // search for something that does exists - Query query2 = new TermQuery(new Term("body", "sunny")); - - ScoreDoc[] result; - - // ensure that queries return expected results without DateFilter first - result = searcher.search(query1, 1000).scoreDocs; - assertEquals(0, result.length); - - result = searcher.search(query2, 1000).scoreDocs; - assertEquals(1, result.length); - - // run queries with DateFilter - Query filtered = new BooleanQuery.Builder() - .add(query1, Occur.MUST) - .add(df1, Occur.FILTER) - .build(); - result = searcher.search(filtered, 1000).scoreDocs; - assertEquals(0, result.length); - - filtered = new BooleanQuery.Builder() - .add(query1, Occur.MUST) - .add(df2, Occur.FILTER) - .build(); - result = searcher.search(filtered, 1000).scoreDocs; - assertEquals(0, result.length); - - filtered = new BooleanQuery.Builder() - .add(query2, Occur.MUST) - .add(df1, Occur.FILTER) - .build(); - result = searcher.search(filtered, 1000).scoreDocs; - assertEquals(1, result.length); - - filtered = new BooleanQuery.Builder() - .add(query2, Occur.MUST) - .add(df2, Occur.FILTER) - .build(); - result = searcher.search(filtered, 1000).scoreDocs; - assertEquals(0, result.length); - reader.close(); - indexStore.close(); - } - - /** - * - */ - public void testAfter() throws IOException { - // create an index - Directory indexStore = newDirectory(); - RandomIndexWriter writer = new RandomIndexWriter(random(), indexStore); - - long now = System.currentTimeMillis(); - - Document doc = new Document(); - // add time that is in the future - doc.add(newStringField("datefield", DateTools.timeToString(now + 888888, DateTools.Resolution.MILLISECOND), Field.Store.YES)); - doc.add(newTextField("body", "Today is a very sunny day in New York City", Field.Store.YES)); - writer.addDocument(doc); - - IndexReader reader = writer.getReader(); - writer.close(); - IndexSearcher searcher = newSearcher(reader); - - // filter that should preserve matches - // DateFilter df1 = DateFilter.After("datefield", now); - Filter df1 = new QueryWrapperFilter(TermRangeQuery.newStringRange("datefield", DateTools - .timeToString(now, DateTools.Resolution.MILLISECOND), DateTools - .timeToString(now + 999999, DateTools.Resolution.MILLISECOND), true, - false)); - // filter that should discard matches - // DateFilter df2 = DateFilter.After("datefield", now + 999999); - Filter df2 = new QueryWrapperFilter(TermRangeQuery.newStringRange("datefield", DateTools - .timeToString(now + 999999, DateTools.Resolution.MILLISECOND), - DateTools.timeToString(now + 999999999, - DateTools.Resolution.MILLISECOND), false, true)); - - // search something that doesn't exist with DateFilter - Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); - - // search for something that does exists - Query query2 = new TermQuery(new Term("body", "sunny")); - - ScoreDoc[] result; - - // ensure that queries return expected results without DateFilter first - result = searcher.search(query1, 1000).scoreDocs; - assertEquals(0, result.length); - - result = searcher.search(query2, 1000).scoreDocs; - assertEquals(1, result.length); - - // run queries with DateFilter - Query filtered = new BooleanQuery.Builder() - .add(query1, Occur.MUST) - .add(df1, Occur.FILTER) - .build(); - result = searcher.search(filtered, 1000).scoreDocs; - assertEquals(0, result.length); - - filtered = new BooleanQuery.Builder() - .add(query1, Occur.MUST) - .add(df2, Occur.FILTER) - .build(); - result = searcher.search(filtered, 1000).scoreDocs; - assertEquals(0, result.length); - - filtered = new BooleanQuery.Builder() - .add(query2, Occur.MUST) - .add(df1, Occur.FILTER) - .build(); - result = searcher.search(filtered, 1000).scoreDocs; - assertEquals(1, result.length); - - filtered = new BooleanQuery.Builder() - .add(query2, Occur.MUST) - .add(df2, Occur.FILTER) - .build(); - result = searcher.search(filtered, 1000).scoreDocs; - assertEquals(0, result.length); - reader.close(); - indexStore.close(); - } -} diff --git a/lucene/core/src/test/org/apache/lucene/search/TestNeedsScores.java b/lucene/core/src/test/org/apache/lucene/search/TestNeedsScores.java index 514ff64d8b0..6e753591e52 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestNeedsScores.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestNeedsScores.java @@ -74,13 +74,6 @@ public class TestNeedsScores extends LuceneTestCase { assertEquals(5, searcher.search(constantScore, 5).totalHits); } - /** when converted to a filter */ - public void testQueryWrapperFilter() throws Exception { - Query term = new TermQuery(new Term("field", "this")); - Filter filter = new QueryWrapperFilter(new AssertNeedsScores(term, false)); - assertEquals(5, searcher.search(filter, 5).totalHits); - } - /** when not sorting by score */ public void testSortByField() throws Exception { Query query = new AssertNeedsScores(new MatchAllDocsQuery(), false); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java b/lucene/core/src/test/org/apache/lucene/search/TestQueryCachingPolicy.java similarity index 86% rename from lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java rename to lucene/core/src/test/org/apache/lucene/search/TestQueryCachingPolicy.java index b487f77fd73..5fb5e97c26d 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestQueryCachingPolicy.java @@ -27,7 +27,7 @@ import org.apache.lucene.index.Term; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; -public class TestFilterCachingPolicy extends LuceneTestCase { +public class TestQueryCachingPolicy extends LuceneTestCase { public void testLargeSegmentDetection() throws IOException { Directory dir = newDirectory(); @@ -40,12 +40,12 @@ public class TestFilterCachingPolicy extends LuceneTestCase { for (float minSizeRatio : new float[] {Float.MIN_VALUE, 0.01f, 0.1f, 0.9f}) { final QueryCachingPolicy policy = new QueryCachingPolicy.CacheOnLargeSegments(0, minSizeRatio); for (LeafReaderContext ctx : reader.leaves()) { - final Filter filter = new QueryWrapperFilter(new TermQuery(new Term("field", "value"))); - final boolean shouldCache = policy.shouldCache(filter, ctx); + final Query query = new TermQuery(new Term("field", "value")); + final boolean shouldCache = policy.shouldCache(query, ctx); final float sizeRatio = (float) ctx.reader().maxDoc() / reader.maxDoc(); assertEquals(sizeRatio >= minSizeRatio, shouldCache); - assertTrue(new QueryCachingPolicy.CacheOnLargeSegments(numDocs, Float.MIN_VALUE).shouldCache(filter, ctx)); - assertFalse(new QueryCachingPolicy.CacheOnLargeSegments(numDocs + 1, Float.MIN_VALUE).shouldCache(filter, ctx)); + assertTrue(new QueryCachingPolicy.CacheOnLargeSegments(numDocs, Float.MIN_VALUE).shouldCache(query, ctx)); + assertFalse(new QueryCachingPolicy.CacheOnLargeSegments(numDocs + 1, Float.MIN_VALUE).shouldCache(query, ctx)); } } reader.close(); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestScorerPerf.java b/lucene/core/src/test/org/apache/lucene/search/TestScorerPerf.java index ff583d5057f..73ebd40407b 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestScorerPerf.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestScorerPerf.java @@ -14,6 +14,7 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.Term; import org.apache.lucene.store.Directory; import org.apache.lucene.util.BitDocIdSet; +import org.apache.lucene.util.BitSetIterator; import org.apache.lucene.util.Bits; import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LuceneTestCase; @@ -140,30 +141,35 @@ public class TestScorerPerf extends LuceneTestCase { } } - private static class BitSetFilter extends Filter { + private static class BitSetQuery extends Query { + private final FixedBitSet docs; - BitSetFilter(FixedBitSet docs) { + BitSetQuery(FixedBitSet docs) { this.docs = docs; } @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - assertNull("acceptDocs should be null, as we have an index without deletions", acceptDocs); - return new BitDocIdSet(docs); + public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { + return new ConstantScoreWeight(this) { + @Override + public Scorer scorer(LeafReaderContext context) throws IOException { + return new ConstantScoreScorer(this, score(), new BitSetIterator(docs, docs.approximateCardinality())); + } + }; } - + @Override public String toString(String field) { return "randomBitSetFilter"; } - + @Override public boolean equals(Object obj) { if (super.equals(obj) == false) { return false; } - return docs == ((BitSetFilter) obj).docs; + return docs == ((BitSetQuery) obj).docs; } @Override @@ -174,7 +180,7 @@ public class TestScorerPerf extends LuceneTestCase { FixedBitSet addClause(BooleanQuery.Builder bq, FixedBitSet result) { final FixedBitSet rnd = sets[random().nextInt(sets.length)]; - Query q = new ConstantScoreQuery(new BitSetFilter(rnd)); + Query q = new BitSetQuery(rnd); bq.add(q, BooleanClause.Occur.MUST); if (validate) { if (result==null) result = rnd.clone(); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSortRandom.java b/lucene/core/src/test/org/apache/lucene/search/TestSortRandom.java index 9ec83ebe655..d6b93d0f234 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestSortRandom.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestSortRandom.java @@ -36,10 +36,8 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.RandomIndexWriter; -import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.store.Directory; -import org.apache.lucene.util.BitDocIdSet; -import org.apache.lucene.util.Bits; +import org.apache.lucene.util.BitSetIterator; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LuceneTestCase; @@ -147,9 +145,8 @@ public class TestSortRandom extends LuceneTestCase { sort = new Sort(sf, SortField.FIELD_DOC); } final int hitCount = TestUtil.nextInt(random, 1, r.maxDoc() + 20); - final RandomFilter f = new RandomFilter(random.nextLong(), random.nextFloat(), docValues); - hits = s.search(new ConstantScoreQuery(f), - hitCount, sort, random.nextBoolean(), random.nextBoolean()); + final RandomQuery f = new RandomQuery(random.nextLong(), random.nextFloat(), docValues); + hits = s.search(f, hitCount, sort, random.nextBoolean(), random.nextBoolean()); if (VERBOSE) { System.out.println("\nTEST: iter=" + iter + " " + hits.totalHits + " hits; topN=" + hitCount + "; reverse=" + reverse + "; sortMissingLast=" + sortMissingLast + " sort=" + sort); @@ -218,35 +215,40 @@ public class TestSortRandom extends LuceneTestCase { dir.close(); } - private static class RandomFilter extends Filter { + private static class RandomQuery extends Query { private final long seed; private float density; private final List docValues; public final List matchValues = Collections.synchronizedList(new ArrayList()); // density should be 0.0 ... 1.0 - public RandomFilter(long seed, float density, List docValues) { + public RandomQuery(long seed, float density, List docValues) { this.seed = seed; this.density = density; this.docValues = docValues; } @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - Random random = new Random(context.docBase ^ seed); - final int maxDoc = context.reader().maxDoc(); - final NumericDocValues idSource = DocValues.getNumeric(context.reader(), "id"); - assertNotNull(idSource); - final FixedBitSet bits = new FixedBitSet(maxDoc); - for(int docID=0;docID docs, String contentToken, String[][] drillDowns, - String[][] dimValues, Filter onlyEven) throws Exception { + String[][] dimValues, Query onlyEven) throws Exception { int numDims = dimValues.length; List hits = new ArrayList<>(); diff --git a/lucene/grouping/src/test/org/apache/lucene/search/grouping/GroupingSearchTest.java b/lucene/grouping/src/test/org/apache/lucene/search/grouping/GroupingSearchTest.java index 06bcd2afca1..05f84533f3a 100644 --- a/lucene/grouping/src/test/org/apache/lucene/search/grouping/GroupingSearchTest.java +++ b/lucene/grouping/src/test/org/apache/lucene/search/grouping/GroupingSearchTest.java @@ -28,10 +28,8 @@ import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.queries.function.ValueSource; import org.apache.lucene.queries.function.valuesource.BytesRefFieldSource; -import org.apache.lucene.search.CachingWrapperQuery; -import org.apache.lucene.search.Filter; import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.search.QueryWrapperFilter; +import org.apache.lucene.search.Query; import org.apache.lucene.search.Sort; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; @@ -159,7 +157,7 @@ public class GroupingSearchTest extends LuceneTestCase { assertEquals(1, group.scoreDocs.length); assertEquals(6, group.scoreDocs[0].doc); - Filter lastDocInBlock = new QueryWrapperFilter(new TermQuery(new Term("groupend", "x"))); + Query lastDocInBlock = new TermQuery(new Term("groupend", "x")); groupingSearch = new GroupingSearch(lastDocInBlock); groups = groupingSearch.search(indexSearcher, new TermQuery(new Term("content", "random")), 0, 10); diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java b/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java index 3689a81b3d3..ce05eed3312 100644 --- a/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java +++ b/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java @@ -71,7 +71,6 @@ import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.PhraseQuery.Builder; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; -import org.apache.lucene.search.QueryWrapperFilter; import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermRangeQuery; @@ -585,8 +584,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte public void testToParentBlockJoinQuery() throws Exception { BitSetProducer parentFilter = new QueryBitSetProducer( - new QueryWrapperFilter( - new TermQuery(new Term(FIELD_NAME, "parent")))); + new TermQuery(new Term(FIELD_NAME, "parent"))); query = new ToParentBlockJoinQuery(new TermQuery(new Term(FIELD_NAME, "child")), parentFilter, ScoreMode.None); @@ -611,8 +609,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte public void testToChildBlockJoinQuery() throws Exception { BitSetProducer parentFilter = new QueryBitSetProducer( - new QueryWrapperFilter( - new TermQuery(new Term(FIELD_NAME, "parent")))); + new TermQuery(new Term(FIELD_NAME, "parent"))); BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); booleanQuery.add(new ToChildBlockJoinQuery(new TermQuery( @@ -910,8 +907,8 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte numHighlights = 0; if (random().nextBoolean()) { BooleanQuery.Builder bq = new BooleanQuery.Builder(); - bq.add(new ConstantScoreQuery(new QueryWrapperFilter(new TermQuery( - new Term(FIELD_NAME, "kennedy")))), Occur.MUST); + bq.add(new ConstantScoreQuery(new TermQuery( + new Term(FIELD_NAME, "kennedy"))), Occur.MUST); bq.add(new ConstantScoreQuery(new TermQuery(new Term(FIELD_NAME, "kennedy"))), Occur.MUST); doSearching(bq.build()); } else { diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FieldQueryTest.java b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FieldQueryTest.java index 32d881f1104..de7a9d5e71f 100644 --- a/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FieldQueryTest.java +++ b/lucene/highlighter/src/test/org/apache/lucene/search/vectorhighlight/FieldQueryTest.java @@ -23,14 +23,11 @@ import java.util.List; import java.util.Map; import java.util.Set; -import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.ConstantScoreQuery; -import org.apache.lucene.search.DocIdSet; -import org.apache.lucene.search.Filter; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.RegexpQuery; @@ -39,7 +36,6 @@ import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.search.vectorhighlight.FieldQuery.QueryPhraseMap; import org.apache.lucene.search.vectorhighlight.FieldTermStack.TermInfo; -import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; public class FieldQueryTest extends AbstractTestCase { @@ -938,30 +934,6 @@ public class FieldQueryTest extends AbstractTestCase { new FieldQuery(q, reader, true, true ); } - public void testFlattenFilteredQuery() throws Exception { - initBoost(); - Filter filter = new Filter() { - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) - throws IOException { - return null; - } - @Override - public String toString(String field) { - return "filterToBeFlattened"; - } - }; - Query query = new BooleanQuery.Builder() - .add(pqF( "A" ), Occur.MUST) - .add(filter, Occur.FILTER) - .build(); - query = new BoostQuery(query, boost); - FieldQuery fq = new FieldQuery( query, true, true ); - Set flatQueries = new HashSet<>(); - fq.flatten( query, reader, flatQueries, 1f ); - assertCollectionQueries( flatQueries, tq( boost, "A" ) ); - } - public void testFlattenConstantScoreQuery() throws Exception { initBoost(); Query query = new ConstantScoreQuery(pqF( "A" )); diff --git a/lucene/join/src/java/org/apache/lucene/search/join/package-info.java b/lucene/join/src/java/org/apache/lucene/search/join/package-info.java index 1356ba6d500..6133f99748f 100644 --- a/lucene/join/src/java/org/apache/lucene/search/join/package-info.java +++ b/lucene/join/src/java/org/apache/lucene/search/join/package-info.java @@ -29,7 +29,7 @@ * *

    When you index in this way, the documents in your index are divided * into parent documents (the last document of each block) and child - * documents (all others). You provide a {@link org.apache.lucene.search.Filter} that identifies the + * documents (all others). You provide a {@link org.apache.lucene.search.join.BitSetProducer} that identifies the * parent documents, as Lucene does not currently record any information * about doc blocks.

    * diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestCheckJoinIndex.java b/lucene/join/src/test/org/apache/lucene/search/join/TestCheckJoinIndex.java index 2d764826ab1..214c103b8f7 100644 --- a/lucene/join/src/test/org/apache/lucene/search/join/TestCheckJoinIndex.java +++ b/lucene/join/src/test/org/apache/lucene/search/join/TestCheckJoinIndex.java @@ -30,7 +30,6 @@ import org.apache.lucene.index.NoMergePolicy; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.MatchNoDocsQuery; -import org.apache.lucene.search.QueryWrapperFilter; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; @@ -88,7 +87,7 @@ public class TestCheckJoinIndex extends LuceneTestCase { final IndexReader reader = w.getReader(); w.close(); - BitSetProducer parentsFilter = new QueryBitSetProducer(new QueryWrapperFilter(new TermQuery(new Term("parent", "true")))); + BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("parent", "true"))); try { CheckJoinIndex.check(reader, parentsFilter); fail("Invalid index"); @@ -128,7 +127,7 @@ public class TestCheckJoinIndex extends LuceneTestCase { final IndexReader reader = w.getReader(); w.close(); - BitSetProducer parentsFilter = new QueryBitSetProducer(new QueryWrapperFilter(new TermQuery(new Term("parent", "true")))); + BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("parent", "true"))); try { CheckJoinIndex.check(reader, parentsFilter); fail("Invalid index"); diff --git a/lucene/misc/src/java/org/apache/lucene/search/BlockJoinComparatorSource.java b/lucene/misc/src/java/org/apache/lucene/search/BlockJoinComparatorSource.java index 6463bfac12f..e0b9f1962b0 100644 --- a/lucene/misc/src/java/org/apache/lucene/search/BlockJoinComparatorSource.java +++ b/lucene/misc/src/java/org/apache/lucene/search/BlockJoinComparatorSource.java @@ -21,7 +21,6 @@ import java.io.IOException; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.SortingMergePolicy; -import org.apache.lucene.util.BitDocIdSet; import org.apache.lucene.util.BitSet; /** @@ -38,7 +37,7 @@ import org.apache.lucene.util.BitSet; // TODO: can/should we clean this thing up (e.g. return a proper sort value) // and move to the join/ module? public class BlockJoinComparatorSource extends FieldComparatorSource { - final Filter parentsFilter; + final Query parentsFilter; final Sort parentSort; final Sort childSort; @@ -49,7 +48,7 @@ public class BlockJoinComparatorSource extends FieldComparatorSource { * @param parentsFilter Filter identifying parent documents * @param parentSort Sort for parent documents */ - public BlockJoinComparatorSource(Filter parentsFilter, Sort parentSort) { + public BlockJoinComparatorSource(Query parentsFilter, Sort parentSort) { this(parentsFilter, parentSort, new Sort(SortField.FIELD_DOC)); } @@ -61,7 +60,7 @@ public class BlockJoinComparatorSource extends FieldComparatorSource { * @param parentSort Sort for parent documents * @param childSort Sort for child documents in the same block */ - public BlockJoinComparatorSource(Filter parentsFilter, Sort parentSort, Sort childSort) { + public BlockJoinComparatorSource(Query parentsFilter, Sort parentSort, Sort childSort) { this.parentsFilter = parentsFilter; this.parentSort = parentSort; this.childSort = childSort; @@ -119,14 +118,14 @@ public class BlockJoinComparatorSource extends FieldComparatorSource { if (parentBits != null) { throw new IllegalStateException("This comparator can only be used on a single segment"); } - final DocIdSet parents = parentsFilter.getDocIdSet(context, null); + IndexSearcher searcher = new IndexSearcher(context.reader()); + searcher.setQueryCache(null); + final Weight weight = searcher.createNormalizedWeight(parentsFilter, false); + final DocIdSetIterator parents = weight.scorer(context); if (parents == null) { throw new IllegalStateException("LeafReader " + context.reader() + " contains no parents!"); } - if (parents instanceof BitDocIdSet == false) { - throw new IllegalStateException("parentFilter must return BitSet; got " + parents); - } - parentBits = (BitSet) parents.bits(); + parentBits = BitSet.of(parents, context.reader().maxDoc()); parentLeafComparators = new LeafFieldComparator[parentComparators.length]; for (int i = 0; i < parentComparators.length; i++) { parentLeafComparators[i] = parentComparators[i].getLeafComparator(context); diff --git a/lucene/misc/src/test/org/apache/lucene/index/TestBlockJoinSorter.java b/lucene/misc/src/test/org/apache/lucene/index/TestBlockJoinSorter.java index 006f2ad2856..5a83d07539d 100644 --- a/lucene/misc/src/test/org/apache/lucene/index/TestBlockJoinSorter.java +++ b/lucene/misc/src/test/org/apache/lucene/index/TestBlockJoinSorter.java @@ -17,94 +17,29 @@ package org.apache.lucene.index; * limitations under the License. */ -import static org.apache.lucene.search.DocIdSet.EMPTY; - import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.Map; -import java.util.WeakHashMap; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field.Store; import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.StringField; -import org.apache.lucene.index.LeafReader; -import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexWriterConfig; -import org.apache.lucene.index.NumericDocValues; -import org.apache.lucene.index.RandomIndexWriter; -import org.apache.lucene.index.Term; -import org.apache.lucene.search.BitsFilteredDocIdSet; import org.apache.lucene.search.BlockJoinComparatorSource; -import org.apache.lucene.search.DocIdSet; import org.apache.lucene.search.DocIdSetIterator; -import org.apache.lucene.search.Filter; -import org.apache.lucene.search.QueryWrapperFilter; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.Weight; import org.apache.lucene.util.ArrayUtil; -import org.apache.lucene.util.BitDocIdSet; import org.apache.lucene.util.BitSet; -import org.apache.lucene.util.Bits; -import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LuceneTestCase; -import org.apache.lucene.util.SparseFixedBitSet; public class TestBlockJoinSorter extends LuceneTestCase { - private static class BitSetCachingWrapperFilter extends Filter { - - private final Filter filter; - private final Map cache = Collections.synchronizedMap(new WeakHashMap()); - - public BitSetCachingWrapperFilter(Filter filter) { - this.filter = filter; - } - - @Override - public DocIdSet getDocIdSet(LeafReaderContext context, final Bits acceptDocs) throws IOException { - final LeafReader reader = context.reader(); - final Object key = reader.getCoreCacheKey(); - - BitDocIdSet docIdSet = cache.get(key); - if (docIdSet == null) { - final DocIdSet uncached = filter.getDocIdSet(context, null); - final DocIdSetIterator it = uncached == null ? null : uncached.iterator(); - if (it != null) { - docIdSet = new BitDocIdSet(BitSet.of(it, context.reader().maxDoc())); - } - if (docIdSet == null) { - docIdSet = new BitDocIdSet(new SparseFixedBitSet(context.reader().maxDoc())); - } - cache.put(key, docIdSet); - } - - return docIdSet == EMPTY ? null : BitsFilteredDocIdSet.wrap(docIdSet, acceptDocs); - } - - @Override - public boolean equals(Object obj) { - if (super.equals(obj) == false) { - return false; - } - return filter.equals(((BitSetCachingWrapperFilter) obj).filter); - } - - @Override - public int hashCode() { - return 31 * super.hashCode() + filter.hashCode(); - } - - @Override - public String toString(String field) { - return getClass().getName() + "(" + filter.toString(field) + ")"; - } - } - public void test() throws IOException { final int numParents = atLeast(200); IndexWriterConfig cfg = newIndexWriterConfig(new MockAnalyzer(random())); @@ -132,8 +67,11 @@ public class TestBlockJoinSorter extends LuceneTestCase { writer.close(); final LeafReader reader = getOnlySegmentReader(indexReader); - final Filter parentsFilter = new BitSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("parent", "true")))); - final FixedBitSet parentBits = (FixedBitSet) parentsFilter.getDocIdSet(reader.getContext(), null).bits(); + final Query parentsFilter = new TermQuery(new Term("parent", "true")); + IndexSearcher searcher = newSearcher(reader); + final Weight weight = searcher.createNormalizedWeight(parentsFilter, false); + final DocIdSetIterator parents = weight.scorer(reader.getContext()); + final BitSet parentBits = BitSet.of(parents, reader.maxDoc()); final NumericDocValues parentValues = reader.getNumericDocValues("parent_val"); final NumericDocValues childValues = reader.getNumericDocValues("child_val"); diff --git a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSortRandom.java b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSortRandom.java index 085b44057ce..1c9b7d7b24a 100644 --- a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSortRandom.java +++ b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSortRandom.java @@ -39,17 +39,19 @@ import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.ConstantScoreQuery; -import org.apache.lucene.search.DocIdSet; +import org.apache.lucene.search.ConstantScoreScorer; +import org.apache.lucene.search.ConstantScoreWeight; import org.apache.lucene.search.FieldDoc; -import org.apache.lucene.search.Filter; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; import org.apache.lucene.search.TopFieldDocs; +import org.apache.lucene.search.Weight; import org.apache.lucene.store.Directory; import org.apache.lucene.uninverting.UninvertingReader.Type; -import org.apache.lucene.util.BitDocIdSet; -import org.apache.lucene.util.Bits; +import org.apache.lucene.util.BitSetIterator; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.FixedBitSet; import org.apache.lucene.util.LuceneTestCase; @@ -160,7 +162,7 @@ public class TestFieldCacheSortRandom extends LuceneTestCase { sort = new Sort(sf, SortField.FIELD_DOC); } final int hitCount = TestUtil.nextInt(random, 1, r.maxDoc() + 20); - final RandomFilter f = new RandomFilter(random.nextLong(), random.nextFloat(), docValues); + final RandomQuery f = new RandomQuery(random.nextLong(), random.nextFloat(), docValues); int queryType = random.nextInt(2); if (queryType == 0) { hits = s.search(new ConstantScoreQuery(f), @@ -251,35 +253,40 @@ public class TestFieldCacheSortRandom extends LuceneTestCase { dir.close(); } - private static class RandomFilter extends Filter { + private static class RandomQuery extends Query { private final long seed; private float density; private final List docValues; public final List matchValues = Collections.synchronizedList(new ArrayList()); // density should be 0.0 ... 1.0 - public RandomFilter(long seed, float density, List docValues) { + public RandomQuery(long seed, float density, List docValues) { this.seed = seed; this.density = density; this.docValues = docValues; } @Override - public DocIdSet getDocIdSet(LeafReaderContext context, Bits acceptDocs) throws IOException { - Random random = new Random(seed ^ context.docBase); - final int maxDoc = context.reader().maxDoc(); - final NumericDocValues idSource = DocValues.getNumeric(context.reader(), "id"); - assertNotNull(idSource); - final FixedBitSet bits = new FixedBitSet(maxDoc); - for(int docID=0;docID> callables(List blocks) {