Skip to content

Apache Lucene open-source search software

LUCENE-6455: Require a minimum index size to enable query caching.

Adrien Grand6e4c1cc699d3
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c9204be9469..19236148371 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -104,6 +104,9 @@ Optimizations
* LUCENE-6456: Queries that generate doc id sets that are too large for the
query cache are not cached instead of evicting everything. (Adrien Grand)
+* LUCENE-6455: Require a minimum index size to enable query caching in order
+ not to cache eg. on MemoryIndex. (Adrien Grand)
+
Bug Fixes
* LUCENE-6378: Fix all RuntimeExceptions to throw the underlying root cause.
diff --git a/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java b/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java
index 59f9fe5fd58..f9118aaf97f 100644
--- a/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java
+++ b/lucene/core/src/java/org/apache/lucene/search/QueryCachingPolicy.java
@@ -62,20 +62,23 @@ public interface QueryCachingPolicy {
* cached while ensuring that at most <tt>33</tt> segments can make it to
* the cache (given that some implementations such as {@link LRUQueryCache}
* perform better when the number of cached segments is low). */
- public static final CacheOnLargeSegments DEFAULT = new CacheOnLargeSegments(0.03f);
+ public static final CacheOnLargeSegments DEFAULT = new CacheOnLargeSegments(10000, 0.03f);
+ private final int minIndexSize;
private final float minSizeRatio;
/**
* Create a {@link CacheOnLargeSegments} instance that only caches on a
- * given segment if its number of documents divided by the total number of
- * documents in the index is greater than or equal to
- * <code>minSizeRatio</code>.
+ * given segment if the total number of documents in the index is greater
+ * than {@code minIndexSize} and the number of documents in the segment
+ * divided by the total number of documents in the index is greater than
+ * or equal to {@code minSizeRatio}.
*/
- public CacheOnLargeSegments(float minSizeRatio) {
+ public CacheOnLargeSegments(int minIndexSize, float minSizeRatio) {
if (minSizeRatio <= 0 || minSizeRatio >= 1) {
throw new IllegalArgumentException("minSizeRatio must be in ]0, 1[, got " + minSizeRatio);
}
+ this.minIndexSize = minIndexSize;
this.minSizeRatio = minSizeRatio;
}
@@ -85,6 +88,9 @@ public interface QueryCachingPolicy {
@Override
public boolean shouldCache(Query query, LeafReaderContext context) throws IOException {
final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
+ if (topLevelContext.reader().maxDoc() < minIndexSize) {
+ return false;
+ }
final float sizeRatio = (float) context.reader().maxDoc() / topLevelContext.reader().maxDoc();
return sizeRatio >= minSizeRatio;
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java b/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java
index c007bb00fbd..4af138909e7 100644
--- a/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java
+++ b/lucene/core/src/java/org/apache/lucene/search/UsageTrackingQueryCachingPolicy.java
@@ -58,13 +58,15 @@ public final class UsageTrackingQueryCachingPolicy implements QueryCachingPolicy
/**
* Create a new instance.
*
+ * @param minIndexSize the minimum size of the top-level index
* @param minSizeRatio the minimum size ratio for segments to be cached, see {@link QueryCachingPolicy.CacheOnLargeSegments}
* @param historySize the number of recently used filters to track
*/
public UsageTrackingQueryCachingPolicy(
+ int minIndexSize,
float minSizeRatio,
int historySize) {
- this(new QueryCachingPolicy.CacheOnLargeSegments(minSizeRatio), historySize);
+ this(new QueryCachingPolicy.CacheOnLargeSegments(minIndexSize, minSizeRatio), historySize);
}
/** Create a new instance with an history size of 256. */
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java b/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java
index 6465797bda5..b487f77fd73 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestFilterCachingPolicy.java
@@ -38,13 +38,14 @@ public class TestFilterCachingPolicy extends LuceneTestCase {
}
final IndexReader reader = w.getReader();
for (float minSizeRatio : new float[] {Float.MIN_VALUE, 0.01f, 0.1f, 0.9f}) {
- final QueryCachingPolicy policy = new QueryCachingPolicy.CacheOnLargeSegments(minSizeRatio);
+ 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 DocIdSet set = null;
final boolean shouldCache = policy.shouldCache(filter, 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));
}
}
reader.close();