LUCENE-6838: Added getters for IndexSearcher's query cache and caching policy.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1708832 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2015-10-15 15:03:46 +00:00
parent 15016f17e8
commit 70a4825d96
3 changed files with 68 additions and 0 deletions

View File

@ -88,6 +88,9 @@ New Features
fast, very accurate query to find all indexed points within an
earth-surface shape (Karl Wright, Mike McCandless)
* LUCENE-6838: Added IndexSearcher#getQueryCache and #getQueryCachingPolicy.
(Adrien Grand)
API Changes
* LUCENE-6590: Query.setBoost(), Query.getBoost() and Query.clone() are gone.

View File

@ -268,6 +268,17 @@ public class IndexSearcher {
this.queryCache = queryCache;
}
/**
* Return the query cache of this {@link IndexSearcher}. This will be either
* the {@link #getDefaultQueryCache() default query cache} or the query cache
* that was last set through {@link #setQueryCache(QueryCache)}. A return
* value of {@code null} indicates that caching is disabled.
* @lucene.experimental
*/
public QueryCache getQueryCache() {
return queryCache;
}
/**
* Set the {@link QueryCachingPolicy} to use for query caching.
* This method should be called <b>before</b> starting using this
@ -279,6 +290,16 @@ public class IndexSearcher {
this.queryCachingPolicy = Objects.requireNonNull(queryCachingPolicy);
}
/**
* Return the query cache of this {@link IndexSearcher}. This will be either
* the {@link #getDefaultQueryCachingPolicy() default policy} or the policy
* that was last set through {@link #setQueryCachingPolicy(QueryCachingPolicy)}.
* @lucene.experimental
*/
public QueryCachingPolicy getQueryCachingPolicy() {
return queryCachingPolicy;
}
/**
* Expert: Creates an array of leaf slices each holding a subset of the given leaves.
* Each {@link LeafSlice} is executed in a single thread. By default there

View File

@ -30,6 +30,8 @@ import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.StringField;
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;
@ -179,4 +181,46 @@ public class TestIndexSearcher extends LuceneTestCase {
dir.close();
}
public void testGetQueryCache() throws IOException {
IndexSearcher searcher = new IndexSearcher(new MultiReader());
assertEquals(IndexSearcher.getDefaultQueryCache(), searcher.getQueryCache());
QueryCache dummyCache = new QueryCache() {
@Override
public Weight doCache(Weight weight, QueryCachingPolicy policy) {
return weight;
}
};
searcher.setQueryCache(dummyCache);
assertEquals(dummyCache, searcher.getQueryCache());
IndexSearcher.setDefaultQueryCache(dummyCache);
searcher = new IndexSearcher(new MultiReader());
assertEquals(dummyCache, searcher.getQueryCache());
searcher.setQueryCache(null);
assertNull(searcher.getQueryCache());
IndexSearcher.setDefaultQueryCache(null);
searcher = new IndexSearcher(new MultiReader());
assertNull(searcher.getQueryCache());
}
public void testGetQueryCachingPolicy() throws IOException {
IndexSearcher searcher = new IndexSearcher(new MultiReader());
assertEquals(IndexSearcher.getDefaultQueryCachingPolicy(), searcher.getQueryCachingPolicy());
QueryCachingPolicy dummyPolicy = new QueryCachingPolicy() {
@Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException {
return false;
}
@Override
public void onUse(Query query) {}
};
searcher.setQueryCachingPolicy(dummyPolicy);
assertEquals(dummyPolicy, searcher.getQueryCachingPolicy());
IndexSearcher.setDefaultQueryCachingPolicy(dummyPolicy);
searcher = new IndexSearcher(new MultiReader());
assertEquals(dummyPolicy, searcher.getQueryCachingPolicy());
}
}