diff --git a/server/src/main/java/org/elasticsearch/indices/IndicesService.java b/server/src/main/java/org/elasticsearch/indices/IndicesService.java index 2000467f5c1e2..8ac5682405568 100644 --- a/server/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/server/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -1246,6 +1246,11 @@ public boolean canCache(ShardSearchRequest request, SearchContext context) { return false; } + // Profiled queries should not use the cache + if (request.source() != null && request.source().profile()) { + return false; + } + IndexSettings settings = context.indexShard().indexSettings(); // if not explicitly set in the request, use the index setting, if not, use the request if (request.requestCache() == null) { diff --git a/server/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java b/server/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java index e2b8e8ab7afd6..4bebdb0123672 100644 --- a/server/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java +++ b/server/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java @@ -120,7 +120,7 @@ public Weight createWeight(Query query, ScoreMode scoreMode, float boost) throws timer.start(); final Weight weight; try { - weight = super.createWeight(query, scoreMode, boost); + weight = query.createWeight(this, scoreMode, boost); } finally { timer.stop(); profiler.pollLastElement(); diff --git a/server/src/main/java/org/elasticsearch/search/profile/query/ProfileWeight.java b/server/src/main/java/org/elasticsearch/search/profile/query/ProfileWeight.java index ae4481949ffd1..8e1d4ed133b45 100644 --- a/server/src/main/java/org/elasticsearch/search/profile/query/ProfileWeight.java +++ b/server/src/main/java/org/elasticsearch/search/profile/query/ProfileWeight.java @@ -120,7 +120,7 @@ public void extractTerms(Set set) { @Override public boolean isCacheable(LeafReaderContext ctx) { - return subQueryWeight.isCacheable(ctx); + return false; } } diff --git a/server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java b/server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java index 0164ac051d76a..85e2cf9f2c891 100644 --- a/server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java +++ b/server/src/test/java/org/elasticsearch/indices/IndicesRequestCacheIT.java @@ -414,9 +414,49 @@ public void testCacheWithFilteredAlias() { assertCacheState(client, "index", 2, 2); } + public void testProfileDisableCache() throws Exception { + Client client = client(); + assertAcked( + client.admin().indices().prepareCreate("index") + .addMapping("_doc", "k", "type=keyword") + .setSettings( + Settings.builder() + .put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) + ) + .get() + ); + indexRandom(true, client.prepareIndex("index").setSource("k", "hello")); + ensureSearchable("index"); + + int expectedHits = 0; + int expectedMisses = 0; + for (int i = 0; i < 5; i++) { + boolean profile = i % 2 == 0; + SearchResponse resp = client.prepareSearch("index") + .setRequestCache(true) + .setProfile(profile) + .setQuery(QueryBuilders.termQuery("k", "hello")) + .get(); + assertSearchResponse(resp); + ElasticsearchAssertions.assertAllSuccessful(resp); + assertThat(resp.getHits().getTotalHits().value, equalTo(1L)); + if (profile == false) { + if (i == 1) { + expectedMisses ++; + } else { + expectedHits ++; + } + } + assertCacheState(client, "index", expectedHits, expectedMisses); + } + } + private static void assertCacheState(Client client, String index, long expectedHits, long expectedMisses) { - RequestCacheStats requestCacheStats = client.admin().indices().prepareStats(index).setRequestCache(true).get().getTotal() - .getRequestCache(); + RequestCacheStats requestCacheStats = client.admin().indices().prepareStats(index) + .setRequestCache(true) + .get().getTotal().getRequestCache(); // Check the hit count and miss count together so if they are not // correct we can see both values assertEquals(Arrays.asList(expectedHits, expectedMisses, 0L), diff --git a/server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerIT.java b/server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerIT.java index 5ff2f3865d9e9..d272e0603240a 100644 --- a/server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerIT.java +++ b/server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerIT.java @@ -54,7 +54,6 @@ public class QueryProfilerIT extends ESIntegTestCase { * This test simply checks to make sure nothing crashes. Test indexes 100-150 documents, * constructs 20-100 random queries and tries to profile them */ - @AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/LUCENE-8658") public void testProfileQuery() throws Exception { createIndex("test"); ensureGreen(); diff --git a/server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerTests.java b/server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerTests.java index b29d3ba3b7dd4..f05684930a9c5 100644 --- a/server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerTests.java +++ b/server/src/test/java/org/elasticsearch/search/profile/query/QueryProfilerTests.java @@ -30,6 +30,7 @@ import org.apache.lucene.index.Term; import org.apache.lucene.search.Explanation; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.LRUQueryCache; import org.apache.lucene.search.LeafCollector; import org.apache.lucene.search.Query; import org.apache.lucene.search.QueryCachingPolicy; @@ -47,6 +48,7 @@ import org.elasticsearch.search.internal.ContextIndexSearcher; import org.elasticsearch.search.profile.ProfileResult; import org.elasticsearch.test.ESTestCase; +import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -81,7 +83,16 @@ public static void setup() throws IOException { reader = w.getReader(); w.close(); searcher = new ContextIndexSearcher(reader, IndexSearcher.getDefaultSimilarity(), - IndexSearcher.getDefaultQueryCache(), MAYBE_CACHE_POLICY); + IndexSearcher.getDefaultQueryCache(), ALWAYS_CACHE_POLICY); + } + + @After + public void checkNoCache() { + LRUQueryCache cache = (LRUQueryCache) searcher.getQueryCache(); + assertThat(cache.getHitCount(), equalTo(0L)); + assertThat(cache.getCacheCount(), equalTo(0L)); + assertThat(cache.getTotalCount(), equalTo(cache.getMissCount())); + assertThat(cache.getCacheSize(), equalTo(0L)); } @AfterClass @@ -158,10 +169,6 @@ public void testUseIndexStats() throws IOException { public void testApproximations() throws IOException { QueryProfiler profiler = new QueryProfiler(); - // disable query caching since we want to test approximations, which won't - // be exposed on a cached entry - ContextIndexSearcher searcher = new ContextIndexSearcher(reader, IndexSearcher.getDefaultSimilarity(), - null, MAYBE_CACHE_POLICY); searcher.setProfiler(profiler); Query query = new RandomApproximationQuery(new TermQuery(new Term("foo", "bar")), random()); searcher.count(query); @@ -184,7 +191,6 @@ public void testApproximations() throws IOException { long rewriteTime = profiler.getRewriteTime(); assertThat(rewriteTime, greaterThan(0L)); - } public void testCollector() throws IOException { @@ -288,17 +294,4 @@ public boolean shouldCache(Query query) throws IOException { } }; - - private static final QueryCachingPolicy NEVER_CACHE_POLICY = new QueryCachingPolicy() { - - @Override - public void onUse(Query query) {} - - @Override - public boolean shouldCache(Query query) throws IOException { - return false; - } - - }; - }