diff --git a/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java b/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java index 7bfa317c72c70..87cb645a8559d 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java @@ -169,6 +169,10 @@ public ActionRequestValidationException validate() { validationException = addValidationError("using [from] is not allowed in a scroll context", validationException); } + if (requestCache != null && requestCache && scroll() != null) { + validationException = + addValidationError("[request_cache] cannot be used in a a scroll context", validationException); + } return validationException; } diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesService.java b/core/src/main/java/org/elasticsearch/indices/IndicesService.java index caffa1b7befda..e2c66260a39b8 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -1072,6 +1072,12 @@ public void close() { * Can the shard request be cached at all? */ public boolean canCache(ShardSearchRequest request, SearchContext context) { + // Queries that create a scroll context cannot use the cache. + // They modify the search context during their execution so using the cache + // may invalidate the scroll for the next query. + if (request.scroll() != null) { + return false; + } // We cannot cache with DFS because results depend not only on the content of the index but also // on the overridden statistics. So if you ran two queries on the same index with different stats @@ -1080,6 +1086,7 @@ public boolean canCache(ShardSearchRequest request, SearchContext context) { if (SearchType.QUERY_THEN_FETCH != context.searchType()) { 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/docs/reference/migration/migrate_7_0/search.asciidoc b/docs/reference/migration/migrate_7_0/search.asciidoc index a2e5d1ccf8563..12847354cf895 100644 --- a/docs/reference/migration/migrate_7_0/search.asciidoc +++ b/docs/reference/migration/migrate_7_0/search.asciidoc @@ -33,3 +33,10 @@ The Search API returns `400 - Bad request` while it would previously return * the number of slices is too large * keep alive for scroll is too large * number of filters in the adjacency matrix aggregation is too large + + +==== Scroll queries cannot use the request_cache anymore + +Setting `request_cache:true` on a query that creates a scroll ('scroll=1m`) +has been deprecated in 6 and will now return a `400 - Bad request`. +Scroll queries are not meant to be cached. diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/scroll/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/scroll/10_basic.yml index 0f037b890fc4b..b7fd64770d3cd 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/scroll/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/scroll/10_basic.yml @@ -197,3 +197,20 @@ clear_scroll: scroll_id: $scroll_id +--- +"Scroll cannot used the request cache": + - skip: + version: " - 6.99.99" + reason: the error message has been added in v7.0.0 + - do: + indices.create: + index: test_scroll + - do: + catch: /\[request_cache\] cannot be used in a a scroll context/ + search: + index: test_scroll + scroll: 1m + request_cache: true + body: + query: + match_all: {}