Skip to content

Commit 7a27a27

Browse files
keljimczi
authored andcommitted
Reject scroll query if size is 0 (#22552) (#27842)
1 parent 55b71a8 commit 7a27a27

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

core/src/main/java/org/elasticsearch/action/search/SearchRequest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,21 @@ public void writeTo(StreamOutput out) throws IOException {
161161
@Override
162162
public ActionRequestValidationException validate() {
163163
ActionRequestValidationException validationException = null;
164-
if (source != null && source.trackTotalHits() == false && scroll() != null) {
164+
final Scroll scroll = scroll();
165+
if (source != null && source.trackTotalHits() == false && scroll != null) {
165166
validationException =
166167
addValidationError("disabling [track_total_hits] is not allowed in a scroll context", validationException);
167168
}
168-
if (source != null && source.from() > 0 && scroll() != null) {
169+
if (source != null && source.from() > 0 && scroll != null) {
169170
validationException =
170171
addValidationError("using [from] is not allowed in a scroll context", validationException);
171172
}
172-
if (requestCache != null && requestCache && scroll() != null) {
173+
if (requestCache != null && requestCache && scroll != null) {
173174
validationException =
174-
addValidationError("[request_cache] cannot be used in a a scroll context", validationException);
175+
addValidationError("[request_cache] cannot be used in a scroll context", validationException);
176+
}
177+
if (source != null && source.size() == 0 && scroll != null) {
178+
validationException = addValidationError("[size] cannot be [0] in a scroll context", validationException);
175179
}
176180
return validationException;
177181
}

core/src/test/java/org/elasticsearch/search/SearchRequestTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ public void testIllegalArguments() {
8282
}
8383

8484
public void testValidate() throws IOException {
85-
8685
{
8786
// if scroll isn't set, validate should never add errors
8887
SearchRequest searchRequest = createSearchRequest().source(new SearchSourceBuilder());
@@ -114,6 +113,16 @@ public void testValidate() throws IOException {
114113
assertEquals(1, validationErrors.validationErrors().size());
115114
assertEquals("using [from] is not allowed in a scroll context", validationErrors.validationErrors().get(0));
116115
}
116+
{
117+
// scroll and `size` is `0`
118+
SearchRequest searchRequest = createSearchRequest().source(new SearchSourceBuilder().size(0));
119+
searchRequest.requestCache(false);
120+
searchRequest.scroll(new TimeValue(1000));
121+
ActionRequestValidationException validationErrors = searchRequest.validate();
122+
assertNotNull(validationErrors);
123+
assertEquals(1, validationErrors.validationErrors().size());
124+
assertEquals("[size] cannot be [0] in a scroll context", validationErrors.validationErrors().get(0));
125+
}
117126
}
118127

119128
public void testEqualsAndHashcode() throws IOException {

rest-api-spec/src/main/resources/rest-api-spec/test/scroll/10_basic.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,30 @@
206206
indices.create:
207207
index: test_scroll
208208
- do:
209-
catch: /\[request_cache\] cannot be used in a a scroll context/
209+
catch: /\[request_cache\] cannot be used in a scroll context/
210210
search:
211211
index: test_scroll
212212
scroll: 1m
213213
request_cache: true
214214
body:
215215
query:
216216
match_all: {}
217+
218+
---
219+
"Scroll with size 0":
220+
- skip:
221+
version: " - 6.99.99"
222+
reason: the error message has been added in v7.0.0
223+
- do:
224+
indices.create:
225+
index: test_scroll
226+
- do:
227+
catch: /\[size\] cannot be \[0\] in a scroll context/
228+
search:
229+
index: test_scroll
230+
scroll: 1m
231+
request_cache: true
232+
body:
233+
query:
234+
match_all: {}
235+
size: 0

0 commit comments

Comments
 (0)