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 87cb645a8559d..07d2229a5366d 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java @@ -161,17 +161,21 @@ public void writeTo(StreamOutput out) throws IOException { @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; - if (source != null && source.trackTotalHits() == false && scroll() != null) { + final Scroll scroll = scroll(); + if (source != null && source.trackTotalHits() == false && scroll != null) { validationException = addValidationError("disabling [track_total_hits] is not allowed in a scroll context", validationException); } - if (source != null && source.from() > 0 && scroll() != null) { + if (source != null && source.from() > 0 && scroll != null) { validationException = addValidationError("using [from] is not allowed in a scroll context", validationException); } - if (requestCache != null && requestCache && scroll() != null) { + if (requestCache != null && requestCache && scroll != null) { validationException = - addValidationError("[request_cache] cannot be used in a a scroll context", validationException); + addValidationError("[request_cache] cannot be used in a scroll context", validationException); + } + if (source != null && source.size() == 0 && scroll != null) { + validationException = addValidationError("[size] cannot be [0] in a scroll context", validationException); } return validationException; } diff --git a/core/src/test/java/org/elasticsearch/search/SearchRequestTests.java b/core/src/test/java/org/elasticsearch/search/SearchRequestTests.java index eb643885e830a..da8a31d23e1c0 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchRequestTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchRequestTests.java @@ -82,7 +82,6 @@ public void testIllegalArguments() { } public void testValidate() throws IOException { - { // if scroll isn't set, validate should never add errors SearchRequest searchRequest = createSearchRequest().source(new SearchSourceBuilder()); @@ -114,6 +113,16 @@ public void testValidate() throws IOException { assertEquals(1, validationErrors.validationErrors().size()); assertEquals("using [from] is not allowed in a scroll context", validationErrors.validationErrors().get(0)); } + { + // scroll and `size` is `0` + SearchRequest searchRequest = createSearchRequest().source(new SearchSourceBuilder().size(0)); + searchRequest.requestCache(false); + searchRequest.scroll(new TimeValue(1000)); + ActionRequestValidationException validationErrors = searchRequest.validate(); + assertNotNull(validationErrors); + assertEquals(1, validationErrors.validationErrors().size()); + assertEquals("[size] cannot be [0] in a scroll context", validationErrors.validationErrors().get(0)); + } } public void testEqualsAndHashcode() throws IOException { 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 b7fd64770d3cd..0ea2779fa92fa 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 @@ -206,7 +206,7 @@ indices.create: index: test_scroll - do: - catch: /\[request_cache\] cannot be used in a a scroll context/ + catch: /\[request_cache\] cannot be used in a scroll context/ search: index: test_scroll scroll: 1m @@ -214,3 +214,22 @@ body: query: match_all: {} + +--- +"Scroll with size 0": + - skip: + version: " - 6.99.99" + reason: the error message has been added in v7.0.0 + - do: + indices.create: + index: test_scroll + - do: + catch: /\[size\] cannot be \[0\] in a scroll context/ + search: + index: test_scroll + scroll: 1m + request_cache: true + body: + query: + match_all: {} + size: 0