diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 5c64fea7726..87c6d149e18 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -741,6 +741,9 @@ API Changes * LUCENE-3574: Deprecate outdated constants in org.apache.lucene.util.Constants and add new ones for Java 6 and Java 7. (Uwe Schindler) +* LUCENE-3571: Deprecate IndexSearcher(Directory). Use the constructors + that take IndexReader instead. (Robert Muir) + New Features * LUCENE-3448: Added FixedBitSet.and(other/DISI), andNot(other/DISI). diff --git a/lucene/contrib/demo/src/java/org/apache/lucene/demo/SearchFiles.java b/lucene/contrib/demo/src/java/org/apache/lucene/demo/SearchFiles.java index 7653a1e2f7f..2384c99f685 100644 --- a/lucene/contrib/demo/src/java/org/apache/lucene/demo/SearchFiles.java +++ b/lucene/contrib/demo/src/java/org/apache/lucene/demo/SearchFiles.java @@ -27,6 +27,7 @@ import java.util.Date; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; @@ -85,7 +86,8 @@ public class SearchFiles { } } - IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File(index))); + IndexReader reader = IndexReader.open(FSDirectory.open(new File(index))); + IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); BufferedReader in = null; @@ -130,6 +132,7 @@ public class SearchFiles { } } searcher.close(); + reader.close(); } /** diff --git a/lucene/contrib/demo/src/java/org/apache/lucene/demo/xmlparser/FormBasedXmlQueryDemo.java b/lucene/contrib/demo/src/java/org/apache/lucene/demo/xmlparser/FormBasedXmlQueryDemo.java index 734f8df7197..56021c49b21 100644 --- a/lucene/contrib/demo/src/java/org/apache/lucene/demo/xmlparser/FormBasedXmlQueryDemo.java +++ b/lucene/contrib/demo/src/java/org/apache/lucene/demo/xmlparser/FormBasedXmlQueryDemo.java @@ -38,6 +38,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.FieldType; import org.apache.lucene.document.TextField; import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryparser.xml.CorePlusExtensionsParser; @@ -145,6 +146,8 @@ public class FormBasedXmlQueryDemo extends HttpServlet { writer.close(); //open searcher - searcher = new IndexSearcher(rd, true); + // this example never closes it reader! + IndexReader reader = IndexReader.open(rd); + searcher = new IndexSearcher(reader); } } diff --git a/lucene/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java b/lucene/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java index 3d237f6142c..6eda921926b 100644 --- a/lucene/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java +++ b/lucene/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java @@ -87,7 +87,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte phraseQuery.add(new Term(FIELD_NAME, "long")); query = phraseQuery; - searcher = new IndexSearcher(ramDir, true); + searcher = new IndexSearcher(reader); TopDocs hits = searcher.search(query, 10); QueryScorer scorer = new QueryScorer(query, FIELD_NAME); @@ -330,7 +330,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte public void testSpanRegexQuery() throws Exception { query = new SpanOrQuery(new SpanMultiTermQueryWrapper(new RegexpQuery(new Term(FIELD_NAME, "ken.*")))); - searcher = new IndexSearcher(ramDir, true); + searcher = new IndexSearcher(reader); hits = searcher.search(query, 100); int maxNumFragmentsRequired = 2; @@ -354,7 +354,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte public void testRegexQuery() throws Exception { query = new RegexpQuery(new Term(FIELD_NAME, "ken.*")); - searcher = new IndexSearcher(ramDir, true); + searcher = new IndexSearcher(reader); hits = searcher.search(query, 100); int maxNumFragmentsRequired = 2; @@ -379,7 +379,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte public void testNumericRangeQuery() throws Exception { // doesn't currently highlight, but make sure it doesn't cause exception either query = NumericRangeQuery.newIntRange(NUMERIC_FIELD_NAME, 2, 6, true, true); - searcher = new IndexSearcher(ramDir, true); + searcher = new IndexSearcher(reader); hits = searcher.search(query, 100); int maxNumFragmentsRequired = 2; @@ -754,7 +754,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte query = new WildcardQuery(new Term(FIELD_NAME, "ken*")); ((WildcardQuery)query).setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); - searcher = new IndexSearcher(ramDir, true); + searcher = new IndexSearcher(reader); // can't rewrite ConstantScore if you want to highlight it - // it rewrites to ConstantScoreQuery which cannot be highlighted // query = unReWrittenQuery.rewrite(reader); @@ -1272,7 +1272,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte numHighlights = 0; // test to show how rewritten query can still be used if (searcher != null) searcher.close(); - searcher = new IndexSearcher(ramDir, true); + searcher = new IndexSearcher(reader); Analyzer analyzer = new MockAnalyzer(random, MockTokenizer.SIMPLE, true, MockTokenFilter.ENGLISH_STOPSET, true); BooleanQuery query = new BooleanQuery(); @@ -1649,7 +1649,8 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte private void searchIndex() throws IOException, InvalidTokenOffsetsException { Query query = new TermQuery(new Term("t_text1", "random")); - IndexSearcher searcher = new IndexSearcher( dir, true ); + IndexReader reader = IndexReader.open(dir); + IndexSearcher searcher = new IndexSearcher(reader); // This scorer can return negative idf -> null fragment Scorer scorer = new QueryTermScorer( query, searcher.getIndexReader(), "t_text1" ); // This scorer doesn't use idf (patch version) @@ -1664,6 +1665,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte assertEquals("more random words for second field", result); } searcher.close(); + reader.close(); } /* @@ -1702,7 +1704,7 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte public void doSearching(Query unReWrittenQuery) throws Exception { if (searcher != null) searcher.close(); - searcher = new IndexSearcher(ramDir, true); + searcher = new IndexSearcher(reader); // for any multi-term queries to work (prefix, wildcard, range,fuzzy etc) // you must use a rewritten query! query = unReWrittenQuery.rewrite(reader); diff --git a/lucene/contrib/memory/src/test/org/apache/lucene/index/memory/MemoryIndexTest.java b/lucene/contrib/memory/src/test/org/apache/lucene/index/memory/MemoryIndexTest.java index 57a96e11636..068b84fb488 100644 --- a/lucene/contrib/memory/src/test/org/apache/lucene/index/memory/MemoryIndexTest.java +++ b/lucene/contrib/memory/src/test/org/apache/lucene/index/memory/MemoryIndexTest.java @@ -32,6 +32,7 @@ import org.apache.lucene.analysis.MockTokenizer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.TextField; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.codecs.lucene40.Lucene40PostingsFormat; @@ -128,7 +129,8 @@ public class MemoryIndexTest extends BaseTokenStreamTestCase { * Run all queries against both the RAMDirectory and MemoryIndex, ensuring they are the same. */ public void assertAllQueries(MemoryIndex memory, Directory ramdir, Analyzer analyzer) throws Exception { - IndexSearcher ram = new IndexSearcher(ramdir); + IndexReader reader = IndexReader.open(ramdir); + IndexSearcher ram = new IndexSearcher(reader); IndexSearcher mem = memory.createSearcher(); QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "foo", analyzer); for (String query : queries) { @@ -137,6 +139,7 @@ public class MemoryIndexTest extends BaseTokenStreamTestCase { assertEquals(ramDocs.totalHits, memDocs.totalHits); } ram.close(); + reader.close(); mem.close(); } diff --git a/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java b/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java index 080e2fecd13..b7051db4363 100644 --- a/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java +++ b/lucene/contrib/misc/src/test/org/apache/lucene/index/TestFieldNormModifier.java @@ -127,7 +127,8 @@ public class TestFieldNormModifier extends LuceneTestCase { public void testGoodCases() throws Exception { - IndexSearcher searcher = new IndexSearcher(store, true); + IndexReader reader = IndexReader.open(store); + IndexSearcher searcher = new IndexSearcher(reader); final float[] scores = new float[NUM_DOCS]; float lastScore = 0.0f; @@ -154,6 +155,7 @@ public class TestFieldNormModifier extends LuceneTestCase { } }); searcher.close(); + reader.close(); lastScore = Float.MAX_VALUE; for (int i = 0; i < NUM_DOCS; i++) { @@ -167,7 +169,8 @@ public class TestFieldNormModifier extends LuceneTestCase { fnm.reSetNorms("field"); // new norm (with default similarity) should put longer docs first - searcher = new IndexSearcher(store, true); + reader = IndexReader.open(store); + searcher = new IndexSearcher(reader); searcher.search(new TermQuery(new Term("field", "word")), new Collector() { private int docBase = 0; private Scorer scorer; @@ -189,6 +192,7 @@ public class TestFieldNormModifier extends LuceneTestCase { } }); searcher.close(); + reader.close(); lastScore = 0.0f; for (int i = 0; i < NUM_DOCS; i++) { @@ -215,7 +219,8 @@ public class TestFieldNormModifier extends LuceneTestCase { // verify that we still get documents in the same order as originally - IndexSearcher searcher = new IndexSearcher(store, true); + IndexReader reader = IndexReader.open(store); + IndexSearcher searcher = new IndexSearcher(reader); final float[] scores = new float[NUM_DOCS]; float lastScore = 0.0f; @@ -241,6 +246,7 @@ public class TestFieldNormModifier extends LuceneTestCase { } }); searcher.close(); + reader.close(); lastScore = scores[0]; for (int i = 0; i < NUM_DOCS; i++) { diff --git a/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java b/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java index da4e704e905..184e0dccbc6 100644 --- a/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java +++ b/lucene/contrib/misc/src/test/org/apache/lucene/misc/TestLengthNormModifier.java @@ -135,7 +135,8 @@ public class TestLengthNormModifier extends LuceneTestCase { float lastScore = 0.0f; // default similarity should put docs with shorter length first - searcher = new IndexSearcher(store, false); + IndexReader reader = IndexReader.open(store, false); + searcher = new IndexSearcher(reader); searcher.search(new TermQuery(new Term("field", "word")), new Collector() { private int docBase = 0; private Scorer scorer; @@ -157,6 +158,7 @@ public class TestLengthNormModifier extends LuceneTestCase { } }); searcher.close(); + reader.close(); lastScore = Float.MAX_VALUE; for (int i = 0; i < NUM_DOCS; i++) { @@ -183,7 +185,8 @@ public class TestLengthNormModifier extends LuceneTestCase { fnm.reSetNorms("field"); // new norm (with default similarity) should put longer docs first - searcher = new IndexSearcher(store, false); + reader = IndexReader.open(store, false); + searcher = new IndexSearcher(reader); searcher.search(new TermQuery(new Term("field", "word")), new Collector() { private int docBase = 0; private Scorer scorer; @@ -205,6 +208,7 @@ public class TestLengthNormModifier extends LuceneTestCase { } }); searcher.close(); + reader.close(); lastScore = 0.0f; for (int i = 0; i < NUM_DOCS; i++) { diff --git a/lucene/contrib/sandbox/src/test/org/apache/lucene/sandbox/queries/regex/TestSpanRegexQuery.java b/lucene/contrib/sandbox/src/test/org/apache/lucene/sandbox/queries/regex/TestSpanRegexQuery.java index 761b620e0fd..1057098bec3 100644 --- a/lucene/contrib/sandbox/src/test/org/apache/lucene/sandbox/queries/regex/TestSpanRegexQuery.java +++ b/lucene/contrib/sandbox/src/test/org/apache/lucene/sandbox/queries/regex/TestSpanRegexQuery.java @@ -24,6 +24,7 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.FieldType; import org.apache.lucene.document.TextField; import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.index.IndexWriterConfig.OpenMode; @@ -71,7 +72,8 @@ public class TestSpanRegexQuery extends LuceneTestCase { writer.forceMerge(1); writer.close(); - IndexSearcher searcher = new IndexSearcher(directory, true); + IndexReader reader = IndexReader.open(directory); + IndexSearcher searcher = new IndexSearcher(reader); SpanQuery srq = new SpanMultiTermQueryWrapper(new RegexQuery(new Term("field", "aut.*"))); SpanFirstQuery sfq = new SpanFirstQuery(srq, 1); // SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6, @@ -79,6 +81,7 @@ public class TestSpanRegexQuery extends LuceneTestCase { int numHits = searcher.search(sfq, null, 1000).totalHits; assertEquals(1, numHits); searcher.close(); + reader.close(); directory.close(); } diff --git a/lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java b/lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java index 64a8b464dec..33f5d4574ab 100644 --- a/lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java +++ b/lucene/contrib/spatial/src/test/org/apache/lucene/spatial/tier/TestCartesian.java @@ -26,6 +26,7 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.NumericField; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.IndexSearcher; @@ -191,7 +192,8 @@ public class TestCartesian extends LuceneTestCase { public void testAntiM() throws IOException, InvalidGeoException { - searcher = new IndexSearcher(directory, true); + IndexReader reader = IndexReader.open(directory); + searcher = new IndexSearcher(reader); final double miles = 2800.0; // Hawaii @@ -259,10 +261,12 @@ public class TestCartesian extends LuceneTestCase { lastDistance = geo_distance; } searcher.close(); + reader.close(); } public void testPoleFlipping() throws IOException, InvalidGeoException { - searcher = new IndexSearcher(directory, true); + IndexReader reader = IndexReader.open(directory); + searcher = new IndexSearcher(reader); final double miles = 3500.0; lat = 41.6032207; @@ -329,10 +333,12 @@ public class TestCartesian extends LuceneTestCase { lastDistance = geo_distance; } searcher.close(); + reader.close(); } public void testRange() throws IOException, InvalidGeoException { - searcher = new IndexSearcher(directory, true); + IndexReader reader = IndexReader.open(directory); + searcher = new IndexSearcher(reader); final double[] milesToTest = new double[] {6.0, 0.5, 0.001, 0.0}; final int[] expected = new int[] {7, 1, 0, 0}; @@ -399,12 +405,14 @@ public class TestCartesian extends LuceneTestCase { } } searcher.close(); + reader.close(); } public void testGeoHashRange() throws IOException, InvalidGeoException { - searcher = new IndexSearcher(directory, true); + IndexReader reader = IndexReader.open(directory); + searcher = new IndexSearcher(reader); final double[] milesToTest = new double[] {6.0, 0.5, 0.001, 0.0}; final int[] expected = new int[] {7, 1, 0, 0}; @@ -469,5 +477,6 @@ public class TestCartesian extends LuceneTestCase { } } searcher.close(); + reader.close(); } } diff --git a/lucene/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/src/java/org/apache/lucene/search/IndexSearcher.java index 1fe4be9949e..3e8112307ee 100644 --- a/lucene/src/java/org/apache/lucene/search/IndexSearcher.java +++ b/lucene/src/java/org/apache/lucene/search/IndexSearcher.java @@ -41,7 +41,6 @@ import org.apache.lucene.index.Term; import org.apache.lucene.index.Terms; import org.apache.lucene.search.similarities.DefaultSimilarityProvider; import org.apache.lucene.search.similarities.SimilarityProvider; -import org.apache.lucene.store.Directory; import org.apache.lucene.store.NIOFSDirectory; // javadoc import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; @@ -76,7 +75,6 @@ import org.apache.lucene.util.ThreadInterruptedException; */ public class IndexSearcher implements Closeable { final IndexReader reader; // package private for testing! - private boolean closeReader; // NOTE: these members might change in incompatible ways // in the next release @@ -105,34 +103,9 @@ public class IndexSearcher implements Closeable { /** The SimilarityProvider implementation used by this searcher. */ private SimilarityProvider similarityProvider = defaultProvider; - /** Creates a searcher searching the index in the named - * directory, with readOnly=true - * @param path directory where IndexReader will be opened - * @throws CorruptIndexException if the index is corrupt - * @throws IOException if there is a low-level IO error - */ - public IndexSearcher(Directory path) throws CorruptIndexException, IOException { - this(IndexReader.open(path, true), true, null); - } - - /** Creates a searcher searching the index in the named - * directory. You should pass readOnly=true, since it - * gives much better concurrent performance, unless you - * intend to do write operations (delete documents or - * change norms) with the underlying IndexReader. - * @param path directory where IndexReader will be opened - * @param readOnly if true, the underlying IndexReader - * will be opened readOnly - * @throws CorruptIndexException if the index is corrupt - * @throws IOException if there is a low-level IO error - */ - public IndexSearcher(Directory path, boolean readOnly) throws CorruptIndexException, IOException { - this(IndexReader.open(path, readOnly), true, null); - } - /** Creates a searcher searching the provided index. */ public IndexSearcher(IndexReader r) { - this(r, false, null); + this(r, null); } /** Runs searches for each segment separately, using the @@ -147,7 +120,7 @@ public class IndexSearcher implements Closeable { * * @lucene.experimental */ public IndexSearcher(IndexReader r, ExecutorService executor) { - this(r, false, executor); + this(r.getTopReaderContext(), executor); } /** @@ -167,7 +140,12 @@ public class IndexSearcher implements Closeable { * @lucene.experimental */ public IndexSearcher(ReaderContext context, ExecutorService executor) { - this(context, false, executor); + assert context.isTopLevel: "IndexSearcher's ReaderContext must be topLevel for reader" + context.reader; + reader = context.reader; + this.executor = executor; + this.readerContext = context; + leafContexts = ReaderUtil.leaves(context); + this.leafSlices = executor == null ? null : slices(leafContexts); } /** @@ -178,22 +156,7 @@ public class IndexSearcher implements Closeable { * @lucene.experimental */ public IndexSearcher(ReaderContext context) { - this(context, (ExecutorService) null); - } - - // convenience ctor for other IR based ctors - private IndexSearcher(IndexReader reader, boolean closeReader, ExecutorService executor) { - this(reader.getTopReaderContext(), closeReader, executor); - } - - private IndexSearcher(ReaderContext context, boolean closeReader, ExecutorService executor) { - assert context.isTopLevel: "IndexSearcher's ReaderContext must be topLevel for reader" + context.reader; - reader = context.reader; - this.executor = executor; - this.closeReader = closeReader; - this.readerContext = context; - leafContexts = ReaderUtil.leaves(context); - this.leafSlices = executor == null ? null : slices(leafContexts); + this(context, null); } /** @@ -236,17 +199,8 @@ public class IndexSearcher implements Closeable { return similarityProvider; } - /** - * Note that the underlying IndexReader is not closed, if - * IndexSearcher was constructed with IndexSearcher(IndexReader r). - * If the IndexReader was supplied implicitly by specifying a directory, then - * the IndexReader is closed. - */ @Override public void close() throws IOException { - if (closeReader) { - reader.close(); - } } /** @lucene.internal */ diff --git a/lucene/src/java/overview.html b/lucene/src/java/overview.html index e98df85a9af..0934e319f34 100644 --- a/lucene/src/java/overview.html +++ b/lucene/src/java/overview.html @@ -61,7 +61,8 @@ to check if the results are what we expect):

iwriter.close(); // Now search the index: - IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true + IndexReader ireader = IndexReader.open(directory); // read-only=true + IndexSearcher isearcher = new IndexSearcher(ireader); // Parse a simple query that searches for "text": QueryParser parser = new QueryParser("fieldname", analyzer); Query query = parser.parse("text"); @@ -73,6 +74,7 @@ to check if the results are what we expect):

assertEquals("This is the text to be indexed.", hitDoc.get("fieldname")); } isearcher.close(); + ireader.close(); directory.close(); diff --git a/lucene/src/test-framework/java/org/apache/lucene/analysis/CollationTestBase.java b/lucene/src/test-framework/java/org/apache/lucene/analysis/CollationTestBase.java index a16e1b37b8f..c1c7f79d289 100644 --- a/lucene/src/test-framework/java/org/apache/lucene/analysis/CollationTestBase.java +++ b/lucene/src/test-framework/java/org/apache/lucene/analysis/CollationTestBase.java @@ -85,7 +85,8 @@ public abstract class CollationTestBase extends LuceneTestCase { doc.add(new Field("body", "body", StringField.TYPE_STORED)); writer.addDocument(doc); writer.close(); - IndexSearcher searcher = new IndexSearcher(ramDir, true); + IndexReader reader = IndexReader.open(ramDir); + IndexSearcher searcher = new IndexSearcher(reader); Query query = new TermQuery(new Term("body","body")); // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi @@ -102,6 +103,7 @@ public abstract class CollationTestBase extends LuceneTestCase { assertEquals("The index Term should be included.", 1, result.length); searcher.close(); + reader.close(); } public void testFarsiRangeQueryCollating(Analyzer analyzer, BytesRef firstBeg, @@ -119,7 +121,8 @@ public abstract class CollationTestBase extends LuceneTestCase { doc.add(new Field("content", "\u0633\u0627\u0628", TextField.TYPE_STORED)); writer.addDocument(doc); writer.close(); - IndexSearcher searcher = new IndexSearcher(ramDir, true); + IndexReader reader = IndexReader.open(ramDir); + IndexSearcher searcher = new IndexSearcher(reader); Query query = new TermRangeQuery("content", firstBeg, firstEnd, true, true); ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs; @@ -129,6 +132,7 @@ public abstract class CollationTestBase extends LuceneTestCase { hits = searcher.search(query, null, 1000).scoreDocs; assertEquals("The index Term should be included.", 1, hits.length); searcher.close(); + reader.close(); } public void testFarsiTermRangeQuery(Analyzer analyzer, BytesRef firstBeg, @@ -218,7 +222,8 @@ public abstract class CollationTestBase extends LuceneTestCase { } writer.forceMerge(1); writer.close(); - IndexSearcher searcher = new IndexSearcher(indexStore, true); + IndexReader reader = IndexReader.open(indexStore); + IndexSearcher searcher = new IndexSearcher(reader); Sort sort = new Sort(); Query queryX = new TermQuery(new Term ("contents", "x")); @@ -235,6 +240,8 @@ public abstract class CollationTestBase extends LuceneTestCase { sort.setSort(new SortField("Denmark", SortField.Type.STRING)); assertMatches(searcher, queryY, sort, dkResult); + searcher.close(); + reader.close(); } // Make sure the documents returned by the search match the expected list diff --git a/lucene/src/test-framework/java/org/apache/lucene/search/CheckHits.java b/lucene/src/test-framework/java/org/apache/lucene/search/CheckHits.java index ce4660441ed..4f8729aea4a 100644 --- a/lucene/src/test-framework/java/org/apache/lucene/search/CheckHits.java +++ b/lucene/src/test-framework/java/org/apache/lucene/search/CheckHits.java @@ -419,9 +419,6 @@ public class CheckHits { * @see ExplanationAsserter */ public static class ExplanationAssertingSearcher extends IndexSearcher { - public ExplanationAssertingSearcher(Directory d) throws IOException { - super(d, true); - } public ExplanationAssertingSearcher(IndexReader r) throws IOException { super(r); } diff --git a/lucene/src/test/org/apache/lucene/TestDemo.java b/lucene/src/test/org/apache/lucene/TestDemo.java index c3de6e26e2d..a27b23844e3 100644 --- a/lucene/src/test/org/apache/lucene/TestDemo.java +++ b/lucene/src/test/org/apache/lucene/TestDemo.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.TextField; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.*; @@ -53,7 +54,8 @@ public class TestDemo extends LuceneTestCase { iwriter.close(); // Now search the index: - IndexSearcher isearcher = new IndexSearcher(directory, true); // read-only=true + IndexReader ireader = IndexReader.open(directory); // read-only=true + IndexSearcher isearcher = new IndexSearcher(ireader); assertEquals(1, isearcher.search(new TermQuery(new Term("fieldname", longTerm)), 1).totalHits); Query query = new TermQuery(new Term("fieldname", "text")); @@ -72,6 +74,7 @@ public class TestDemo extends LuceneTestCase { assertEquals(1, isearcher.search(phraseQuery, null, 1).totalHits); isearcher.close(); + ireader.close(); directory.close(); } } diff --git a/lucene/src/test/org/apache/lucene/TestSearch.java b/lucene/src/test/org/apache/lucene/TestSearch.java index 1e664a48b1b..9f5f670cbc9 100644 --- a/lucene/src/test/org/apache/lucene/TestSearch.java +++ b/lucene/src/test/org/apache/lucene/TestSearch.java @@ -99,7 +99,8 @@ public class TestSearch extends LuceneTestCase { } writer.close(); - IndexSearcher searcher = new IndexSearcher(directory, true); + IndexReader reader = IndexReader.open(directory); + IndexSearcher searcher = new IndexSearcher(reader); ScoreDoc[] hits = null; @@ -122,6 +123,7 @@ public class TestSearch extends LuceneTestCase { } } searcher.close(); + reader.close(); directory.close(); } diff --git a/lucene/src/test/org/apache/lucene/TestSearchForDuplicates.java b/lucene/src/test/org/apache/lucene/TestSearchForDuplicates.java index 442c4888877..e57b053584f 100644 --- a/lucene/src/test/org/apache/lucene/TestSearchForDuplicates.java +++ b/lucene/src/test/org/apache/lucene/TestSearchForDuplicates.java @@ -98,7 +98,8 @@ public class TestSearchForDuplicates extends LuceneTestCase { writer.close(); // try a search without OR - IndexSearcher searcher = new IndexSearcher(directory, true); + IndexReader reader = IndexReader.open(directory); + IndexSearcher searcher = new IndexSearcher(reader); Query query = new TermQuery(new Term(PRIORITY_FIELD, HIGH_PRIORITY)); out.println("Query: " + query.toString(PRIORITY_FIELD)); @@ -117,7 +118,7 @@ public class TestSearchForDuplicates extends LuceneTestCase { searcher.close(); // try a new search with OR - searcher = new IndexSearcher(directory, true); + searcher = new IndexSearcher(reader); hits = null; BooleanQuery booleanQuery = new BooleanQuery(); @@ -130,6 +131,7 @@ public class TestSearchForDuplicates extends LuceneTestCase { checkHits(hits, MAX_DOCS, searcher); searcher.close(); + reader.close(); directory.close(); } diff --git a/lucene/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/lucene/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java index e4b5ebe8e13..53d234c4a4b 100644 --- a/lucene/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java +++ b/lucene/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java @@ -281,8 +281,8 @@ public class TestBackwardsCompatibility extends LuceneTestCase { //Query query = parser.parse("handle:1"); Directory dir = newFSDirectory(indexDir); - IndexSearcher searcher = new IndexSearcher(dir, true); - IndexReader reader = searcher.getIndexReader(); + IndexReader reader = IndexReader.open(dir); + IndexSearcher searcher = new IndexSearcher(reader); _TestUtil.checkIndex(dir); @@ -336,6 +336,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase { assertEquals(34, hits.length); searcher.close(); + reader.close(); dir.close(); } @@ -366,15 +367,17 @@ public class TestBackwardsCompatibility extends LuceneTestCase { writer.close(); // make sure searching sees right # hits - IndexSearcher searcher = new IndexSearcher(dir, true); + IndexReader reader = IndexReader.open(dir); + IndexSearcher searcher = new IndexSearcher(reader); ScoreDoc[] hits = searcher.search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs; Document d = searcher.getIndexReader().document(hits[0].doc); assertEquals("wrong first document", "21", d.get("id")); doTestHits(hits, 44, searcher.getIndexReader()); searcher.close(); + reader.close(); // make sure we can do delete & setNorm against this segment: - IndexReader reader = IndexReader.open(dir, false); + reader = IndexReader.open(dir, false); searcher = newSearcher(reader); Term searchTerm = new Term("id", "6"); int delCount = reader.deleteDocuments(searchTerm); @@ -385,26 +388,30 @@ public class TestBackwardsCompatibility extends LuceneTestCase { searcher.close(); // make sure they "took": - searcher = new IndexSearcher(dir, true); + reader = IndexReader.open(dir, true); + searcher = new IndexSearcher(reader); hits = searcher.search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs; assertEquals("wrong number of hits", 43, hits.length); d = searcher.doc(hits[0].doc); assertEquals("wrong first document", "22", d.get("id")); doTestHits(hits, 43, searcher.getIndexReader()); searcher.close(); + reader.close(); // fully merge writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND)); writer.forceMerge(1); writer.close(); - searcher = new IndexSearcher(dir, true); + reader = IndexReader.open(dir); + searcher = new IndexSearcher(reader); hits = searcher.search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs; assertEquals("wrong number of hits", 43, hits.length); d = searcher.doc(hits[0].doc); doTestHits(hits, 43, searcher.getIndexReader()); assertEquals("wrong first document", "22", d.get("id")); searcher.close(); + reader.close(); dir.close(); } @@ -414,15 +421,17 @@ public class TestBackwardsCompatibility extends LuceneTestCase { Directory dir = newFSDirectory(oldIndexDir); // make sure searching sees right # hits - IndexSearcher searcher = new IndexSearcher(dir, true); + IndexReader reader = IndexReader.open(dir); + IndexSearcher searcher = new IndexSearcher(reader); ScoreDoc[] hits = searcher.search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs; assertEquals("wrong number of hits", 34, hits.length); Document d = searcher.doc(hits[0].doc); assertEquals("wrong first document", "21", d.get("id")); searcher.close(); + reader.close(); // make sure we can do a delete & setNorm against this segment: - IndexReader reader = IndexReader.open(dir, false); + reader = IndexReader.open(dir, false); Term searchTerm = new Term("id", "6"); int delCount = reader.deleteDocuments(searchTerm); assertEquals("wrong delete count", 1, delCount); @@ -431,26 +440,30 @@ public class TestBackwardsCompatibility extends LuceneTestCase { reader.close(); // make sure they "took": - searcher = new IndexSearcher(dir, true); + reader = IndexReader.open(dir); + searcher = new IndexSearcher(reader); hits = searcher.search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs; assertEquals("wrong number of hits", 33, hits.length); d = searcher.doc(hits[0].doc); assertEquals("wrong first document", "22", d.get("id")); doTestHits(hits, 33, searcher.getIndexReader()); searcher.close(); + reader.close(); // fully merge IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND)); writer.forceMerge(1); writer.close(); - searcher = new IndexSearcher(dir, true); + reader = IndexReader.open(dir); + searcher = new IndexSearcher(reader); hits = searcher.search(new TermQuery(new Term("content", "aaa")), null, 1000).scoreDocs; assertEquals("wrong number of hits", 33, hits.length); d = searcher.doc(hits[0].doc); assertEquals("wrong first document", "22", d.get("id")); doTestHits(hits, 33, searcher.getIndexReader()); searcher.close(); + reader.close(); dir.close(); } @@ -686,7 +699,8 @@ public class TestBackwardsCompatibility extends LuceneTestCase { File oldIndexDir = _TestUtil.getTempDir(oldNames[i]); _TestUtil.unzip(getDataFile("index." + oldNames[i] + ".zip"), oldIndexDir); Directory dir = newFSDirectory(oldIndexDir); - IndexSearcher searcher = new IndexSearcher(dir, true); + IndexReader reader = IndexReader.open(dir); + IndexSearcher searcher = new IndexSearcher(reader); for (int id=10; id<15; id++) { ScoreDoc[] hits = searcher.search(NumericRangeQuery.newIntRange("trieInt", 4, Integer.valueOf(id), Integer.valueOf(id), true, true), 100).scoreDocs; @@ -719,6 +733,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase { } searcher.close(); + reader.close(); dir.close(); _TestUtil.rmDir(oldIndexDir); } diff --git a/lucene/src/test/org/apache/lucene/index/TestDeletionPolicy.java b/lucene/src/test/org/apache/lucene/index/TestDeletionPolicy.java index 61bcfd6af2c..8a9992e6816 100644 --- a/lucene/src/test/org/apache/lucene/index/TestDeletionPolicy.java +++ b/lucene/src/test/org/apache/lucene/index/TestDeletionPolicy.java @@ -683,7 +683,8 @@ public class TestDeletionPolicy extends LuceneTestCase { assertEquals(2*(N+1)+1, policy.numOnInit); assertEquals(2*(N+2) - (wasFullyMerged ? 1:0), policy.numOnCommit); - IndexSearcher searcher = new IndexSearcher(dir, false); + IndexReader rwReader = IndexReader.open(dir, false); + IndexSearcher searcher = new IndexSearcher(rwReader); ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs; assertEquals(176, hits.length); @@ -694,6 +695,7 @@ public class TestDeletionPolicy extends LuceneTestCase { dir.deleteFile(IndexFileNames.SEGMENTS_GEN); int expectedCount = 176; searcher.close(); + rwReader.close(); for(int i=0;i,C,D => A, B & are in range", 3, numHits); @@ -275,22 +293,27 @@ public class TestTermRangeQuery extends LuceneTestCase { //assertEquals("A,B,,C,D => A, B & are in range", 2, hits.length()); searcher.close(); + reader.close(); initializeIndex(new String[] {"A", "B", "", "D"}, analyzer); - searcher = new IndexSearcher(dir, true); + reader = IndexReader.open(dir); + searcher = new IndexSearcher(reader); numHits = searcher.search(query, null, 1000).totalHits; // When Lucene-38 is fixed, use the assert on the next line: assertEquals("A,B,,D => A, B & are in range", 3, numHits); // until Lucene-38 is fixed, use this assert: //assertEquals("A,B,,D => A, B & are in range", 2, hits.length()); searcher.close(); + reader.close(); addDoc("C"); - searcher = new IndexSearcher(dir, true); + reader = IndexReader.open(dir); + searcher = new IndexSearcher(reader); numHits = searcher.search(query, null, 1000).totalHits; // When Lucene-38 is fixed, use the assert on the next line: assertEquals("C added, still A, B & are in range", 3, numHits); // until Lucene-38 is fixed, use this assert //assertEquals("C added, still A, B & are in range", 2, hits.length()); searcher.close(); + reader.close(); } // LUCENE-38 @@ -299,28 +322,34 @@ public class TestTermRangeQuery extends LuceneTestCase { Analyzer analyzer = new SingleCharAnalyzer(); Query query = TermRangeQuery.newStringRange("content", null, "C", true, true); initializeIndex(new String[]{"A", "B", "","C", "D"}, analyzer); - IndexSearcher searcher = new IndexSearcher(dir, true); + IndexReader reader = IndexReader.open(dir); + IndexSearcher searcher = new IndexSearcher(reader); int numHits = searcher.search(query, null, 1000).totalHits; // When Lucene-38 is fixed, use the assert on the next line: assertEquals("A,B,,C,D => A,B,,C in range", 4, numHits); // until Lucene-38 is fixed, use this assert //assertEquals("A,B,,C,D => A,B,,C in range", 3, hits.length()); searcher.close(); + reader.close(); initializeIndex(new String[]{"A", "B", "", "D"}, analyzer); - searcher = new IndexSearcher(dir, true); + reader = IndexReader.open(dir); + searcher = new IndexSearcher(reader); numHits = searcher.search(query, null, 1000).totalHits; // When Lucene-38 is fixed, use the assert on the next line: assertEquals("A,B,,D - A, B and in range", 3, numHits); // until Lucene-38 is fixed, use this assert //assertEquals("A,B,,D => A, B and in range", 2, hits.length()); searcher.close(); + reader.close(); addDoc("C"); - searcher = new IndexSearcher(dir, true); + reader = IndexReader.open(dir); + searcher = new IndexSearcher(reader); numHits = searcher.search(query, null, 1000).totalHits; // When Lucene-38 is fixed, use the assert on the next line: assertEquals("C added => A,B,,C in range", 4, numHits); // until Lucene-38 is fixed, use this assert //assertEquals("C added => A,B,,C in range", 3, hits.length()); searcher.close(); + reader.close(); } } diff --git a/lucene/src/test/org/apache/lucene/search/TestWildcard.java b/lucene/src/test/org/apache/lucene/search/TestWildcard.java index 83a0c916b2e..635e72aac02 100644 --- a/lucene/src/test/org/apache/lucene/search/TestWildcard.java +++ b/lucene/src/test/org/apache/lucene/search/TestWildcard.java @@ -22,6 +22,7 @@ import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.TextField; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.MultiFields; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; @@ -67,7 +68,8 @@ public class TestWildcard */ public void testTermWithoutWildcard() throws IOException { Directory indexStore = getIndexStore("field", new String[]{"nowildcard", "nowildcardx"}); - IndexSearcher searcher = new IndexSearcher(indexStore, true); + IndexReader reader = IndexReader.open(indexStore); + IndexSearcher searcher = new IndexSearcher(reader); MultiTermQuery wq = new WildcardQuery(new Term("field", "nowildcard")); assertMatches(searcher, wq, 1); @@ -96,6 +98,7 @@ public class TestWildcard assertTrue(q instanceof ConstantScoreQuery); assertEquals(q.getBoost(), wq.getBoost(), 0.1); searcher.close(); + reader.close(); indexStore.close(); } @@ -104,7 +107,8 @@ public class TestWildcard */ public void testEmptyTerm() throws IOException { Directory indexStore = getIndexStore("field", new String[]{"nowildcard", "nowildcardx"}); - IndexSearcher searcher = new IndexSearcher(indexStore, true); + IndexReader reader = IndexReader.open(indexStore); + IndexSearcher searcher = new IndexSearcher(reader); MultiTermQuery wq = new WildcardQuery(new Term("field", "")); wq.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE); @@ -113,6 +117,7 @@ public class TestWildcard assertTrue(q instanceof BooleanQuery); assertEquals(0, ((BooleanQuery) q).clauses().size()); searcher.close(); + reader.close(); indexStore.close(); } @@ -123,7 +128,8 @@ public class TestWildcard */ public void testPrefixTerm() throws IOException { Directory indexStore = getIndexStore("field", new String[]{"prefix", "prefixx"}); - IndexSearcher searcher = new IndexSearcher(indexStore, true); + IndexReader reader = IndexReader.open(indexStore); + IndexSearcher searcher = new IndexSearcher(reader); MultiTermQuery wq = new WildcardQuery(new Term("field", "prefix*")); assertMatches(searcher, wq, 2); @@ -135,6 +141,7 @@ public class TestWildcard assertFalse(wq.getTermsEnum(terms) instanceof PrefixTermsEnum); assertFalse(wq.getTermsEnum(terms).getClass().getSimpleName().contains("AutomatonTermsEnum")); searcher.close(); + reader.close(); indexStore.close(); } @@ -145,7 +152,8 @@ public class TestWildcard throws IOException { Directory indexStore = getIndexStore("body", new String[] {"metal", "metals"}); - IndexSearcher searcher = new IndexSearcher(indexStore, true); + IndexReader reader = IndexReader.open(indexStore); + IndexSearcher searcher = new IndexSearcher(reader); Query query1 = new TermQuery(new Term("body", "metal")); Query query2 = new WildcardQuery(new Term("body", "metal*")); Query query3 = new WildcardQuery(new Term("body", "m*tal")); @@ -174,6 +182,7 @@ public class TestWildcard assertMatches(searcher, new WildcardQuery(new Term("body", "*tal")), 1); assertMatches(searcher, new WildcardQuery(new Term("body", "*tal*")), 2); searcher.close(); + reader.close(); indexStore.close(); } @@ -186,7 +195,8 @@ public class TestWildcard throws IOException { Directory indexStore = getIndexStore("body", new String[] {"metal", "metals", "mXtals", "mXtXls"}); - IndexSearcher searcher = new IndexSearcher(indexStore, true); + IndexReader reader = IndexReader.open(indexStore); + IndexSearcher searcher = new IndexSearcher(reader); Query query1 = new WildcardQuery(new Term("body", "m?tal")); Query query2 = new WildcardQuery(new Term("body", "metal?")); Query query3 = new WildcardQuery(new Term("body", "metals?")); @@ -201,6 +211,7 @@ public class TestWildcard assertMatches(searcher, query5, 0); assertMatches(searcher, query6, 1); // Query: 'meta??' matches 'metals' not 'metal' searcher.close(); + reader.close(); indexStore.close(); } @@ -210,7 +221,8 @@ public class TestWildcard public void testEscapes() throws Exception { Directory indexStore = getIndexStore("field", new String[]{"foo*bar", "foo??bar", "fooCDbar", "fooSOMETHINGbar", "foo\\"}); - IndexSearcher searcher = new IndexSearcher(indexStore, true); + IndexReader reader = IndexReader.open(indexStore); + IndexSearcher searcher = new IndexSearcher(reader); // without escape: matches foo??bar, fooCDbar, foo*bar, and fooSOMETHINGbar WildcardQuery unescaped = new WildcardQuery(new Term("field", "foo*bar")); @@ -233,6 +245,7 @@ public class TestWildcard assertMatches(searcher, atEnd, 1); searcher.close(); + reader.close(); indexStore.close(); } @@ -347,7 +360,8 @@ public class TestWildcard } iw.close(); - IndexSearcher searcher = new IndexSearcher(dir, true); + IndexReader reader = IndexReader.open(dir); + IndexSearcher searcher = new IndexSearcher(reader); // test queries that must find all for (Query q : matchAll) { @@ -386,6 +400,7 @@ public class TestWildcard } searcher.close(); + reader.close(); dir.close(); } } diff --git a/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java b/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java index 943fd4676ae..331f82f9952 100644 --- a/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java +++ b/lucene/src/test/org/apache/lucene/search/payloads/TestPayloadTermQuery.java @@ -232,7 +232,8 @@ public class TestPayloadTermQuery extends LuceneTestCase { PayloadTermQuery query = new PayloadTermQuery(new Term(PayloadHelper.MULTI_FIELD, "seventy"), new MaxPayloadFunction(), false); - IndexSearcher theSearcher = new IndexSearcher(directory, true); + IndexReader reader = IndexReader.open(directory); + IndexSearcher theSearcher = new IndexSearcher(reader); theSearcher.setSimilarityProvider(new DefaultSimilarityProvider() { @Override public Similarity get(String field) { @@ -271,6 +272,7 @@ public class TestPayloadTermQuery extends LuceneTestCase { count++; } theSearcher.close(); + reader.close(); } public void testNoMatch() throws Exception { diff --git a/lucene/src/test/org/apache/lucene/store/TestLockFactory.java b/lucene/src/test/org/apache/lucene/store/TestLockFactory.java index ec8db9d5922..fadcb75908c 100755 --- a/lucene/src/test/org/apache/lucene/store/TestLockFactory.java +++ b/lucene/src/test/org/apache/lucene/store/TestLockFactory.java @@ -26,6 +26,7 @@ import java.util.Map; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.TextField; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Term; @@ -339,11 +340,13 @@ public class TestLockFactory extends LuceneTestCase { } @Override public void run() { + IndexReader reader = null; IndexSearcher searcher = null; Query query = new TermQuery(new Term("content", "aaa")); for(int i=0;i