diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java index 39c88ed6759..a165dfd9340 100644 --- a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java +++ b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java @@ -75,8 +75,8 @@ import org.apache.lucene.util.ThreadInterruptedException; public class IndexSearcher { // 32MB and at most 10,000 queries - private static final QueryCache DEFAULT_QUERY_CACHE = new LRUQueryCache(10000, 1 << 25); - private static final QueryCachingPolicy DEFAULT_CACHING_POLICY = new UsageTrackingQueryCachingPolicy(); + private static QueryCache DEFAULT_QUERY_CACHE = new LRUQueryCache(10000, 1 << 25); + private static QueryCachingPolicy DEFAULT_CACHING_POLICY = new UsageTrackingQueryCachingPolicy(); final IndexReader reader; // package private for testing! @@ -106,7 +106,23 @@ public class IndexSearcher { public static Similarity getDefaultSimilarity() { return defaultSimilarity; } - + + /** + * Expert: set the default {@link QueryCache} instance. + * @lucene.internal + */ + public static void setDefaultQueryCache(QueryCache defaultQueryCache) { + DEFAULT_QUERY_CACHE = defaultQueryCache; + } + + /** + * Expert: set the default {@link QueryCachingPolicy} instance. + * @lucene.internal + */ + public static void setDefaultQueryCachingPolicy(QueryCachingPolicy defaultQueryCachingPolicy) { + DEFAULT_CACHING_POLICY = defaultQueryCachingPolicy; + } + /** The Similarity implementation used by this searcher. */ private Similarity similarity = defaultSimilarity; diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java index 75d4cc69c96..a226a99bed6 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java @@ -602,7 +602,8 @@ public class TestBooleanQuery extends LuceneTestCase { w.commit(); DirectoryReader reader = w.getReader(); - final IndexSearcher searcher = new IndexSearcher(reader); + final IndexSearcher searcher = newSearcher(reader); + searcher.setQueryCache(null); // to still have approximations PhraseQuery pq = new PhraseQuery(); pq.add(new Term("field", "a")); @@ -633,6 +634,7 @@ public class TestBooleanQuery extends LuceneTestCase { DirectoryReader reader = w.getReader(); final IndexSearcher searcher = new IndexSearcher(reader); + searcher.setQueryCache(null); // to still have approximations PhraseQuery pq = new PhraseQuery(); pq.add(new Term("field", "a")); @@ -662,7 +664,8 @@ public class TestBooleanQuery extends LuceneTestCase { w.commit(); DirectoryReader reader = w.getReader(); - final IndexSearcher searcher = new IndexSearcher(reader); + final IndexSearcher searcher = newSearcher(reader); + searcher.setQueryCache(null); // to still have approximations PhraseQuery pq = new PhraseQuery(); pq.add(new Term("field", "a")); @@ -693,6 +696,7 @@ public class TestBooleanQuery extends LuceneTestCase { DirectoryReader reader = w.getReader(); final IndexSearcher searcher = new IndexSearcher(reader); + searcher.setQueryCache(null); // to still have approximations PhraseQuery pq = new PhraseQuery(); pq.add(new Term("field", "a")); @@ -723,6 +727,7 @@ public class TestBooleanQuery extends LuceneTestCase { DirectoryReader reader = w.getReader(); final IndexSearcher searcher = new IndexSearcher(reader); + searcher.setQueryCache(null); // to still have approximations PhraseQuery pq = new PhraseQuery(); pq.add(new Term("field", "a")); 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 5bea31c5f86..1142d9bf629 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java @@ -214,7 +214,8 @@ public class TestConstantScoreQuery extends LuceneTestCase { w.commit(); DirectoryReader reader = w.getReader(); - final IndexSearcher searcher = new IndexSearcher(reader); + final IndexSearcher searcher = newSearcher(reader); + searcher.setQueryCache(null); // to still have approximations PhraseQuery pq = new PhraseQuery(); pq.add(new Term("field", "a")); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestQueryWrapperFilter.java b/lucene/core/src/test/org/apache/lucene/search/TestQueryWrapperFilter.java index 6af1f607393..76b4def1fcb 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestQueryWrapperFilter.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestQueryWrapperFilter.java @@ -203,6 +203,7 @@ public class TestQueryWrapperFilter extends LuceneTestCase { final IndexReader reader = writer.getReader(); writer.close(); final IndexSearcher searcher = new IndexSearcher(reader); + searcher.setQueryCache(null); // to still have approximations final Query query = new QueryWrapperFilter(new RandomApproximationQuery(new TermQuery(new Term("foo", "bar")), random())); final Weight weight = searcher.createNormalizedWeight(query, random().nextBoolean()); final Scorer scorer = weight.scorer(reader.leaves().get(0), null); diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java index 7c931e9431d..2464c5f01eb 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java @@ -95,6 +95,7 @@ import org.apache.lucene.index.IndexReader.ReaderClosedListener; import org.apache.lucene.index.TermsEnum.SeekStatus; import org.apache.lucene.search.AssertingIndexSearcher; import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.search.LRUQueryCache; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.search.IndexSearcher; @@ -1649,6 +1650,12 @@ public abstract class LuceneTestCase extends Assert { } } + @Before + public void resetDefaultQueryCache() { + IndexSearcher.setDefaultQueryCache(new LRUQueryCache(10000, 1 << 25)); + IndexSearcher.setDefaultQueryCachingPolicy(MAYBE_CACHE_POLICY); + } + /** * Create a new searcher over the reader. This searcher might randomly use * threads. @@ -1700,7 +1707,6 @@ public abstract class LuceneTestCase extends Assert { ret = random.nextBoolean() ? new IndexSearcher(r) : new IndexSearcher(r.getContext()); } ret.setSimilarity(classEnvRule.similarity); - ret.setQueryCachingPolicy(MAYBE_CACHE_POLICY); return ret; } else { int threads = 0;