Reject scroll query if size is 0 (#22552) (#27842)

This commit is contained in:
kel 2017-12-18 17:38:41 +08:00 committed by Jim Ferenczi
parent 55b71a871b
commit 7a27a2770b
3 changed files with 38 additions and 6 deletions

View File

@ -161,17 +161,21 @@ public final class SearchRequest extends ActionRequest implements IndicesRequest
@Override @Override
public ActionRequestValidationException validate() { public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null; ActionRequestValidationException validationException = null;
if (source != null && source.trackTotalHits() == false && scroll() != null) { final Scroll scroll = scroll();
if (source != null && source.trackTotalHits() == false && scroll != null) {
validationException = validationException =
addValidationError("disabling [track_total_hits] is not allowed in a scroll context", 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 = validationException =
addValidationError("using [from] is not allowed in a scroll context", 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 = 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; return validationException;
} }

View File

@ -82,7 +82,6 @@ public class SearchRequestTests extends AbstractSearchTestCase {
} }
public void testValidate() throws IOException { public void testValidate() throws IOException {
{ {
// if scroll isn't set, validate should never add errors // if scroll isn't set, validate should never add errors
SearchRequest searchRequest = createSearchRequest().source(new SearchSourceBuilder()); SearchRequest searchRequest = createSearchRequest().source(new SearchSourceBuilder());
@ -114,6 +113,16 @@ public class SearchRequestTests extends AbstractSearchTestCase {
assertEquals(1, validationErrors.validationErrors().size()); assertEquals(1, validationErrors.validationErrors().size());
assertEquals("using [from] is not allowed in a scroll context", validationErrors.validationErrors().get(0)); 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 { public void testEqualsAndHashcode() throws IOException {

View File

@ -206,7 +206,7 @@
indices.create: indices.create:
index: test_scroll index: test_scroll
- do: - do:
catch: /\[request_cache\] cannot be used in a a scroll context/ catch: /\[request_cache\] cannot be used in a scroll context/
search: search:
index: test_scroll index: test_scroll
scroll: 1m scroll: 1m
@ -214,3 +214,22 @@
body: body:
query: query:
match_all: {} 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