Prevent negative from parameter in SearchSourceBuilder ()

This prevents later errors like the one reported in  and throws an
IllegalArgumentException early instead.
This commit is contained in:
Christoph Büscher 2017-02-27 09:45:10 +01:00 committed by GitHub
parent 396b8b371c
commit 641c88dc29
3 changed files with 17 additions and 1 deletions
core/src
main/java/org/elasticsearch/search/builder
test/java/org/elasticsearch/search/builder
rest-api-spec/src/main/resources/rest-api-spec/test/search

@ -38,11 +38,11 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.search.SearchExtBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.PipelineAggregationBuilder;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.search.fetch.StoredFieldsContext;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
@ -314,6 +314,9 @@ public final class SearchSourceBuilder extends ToXContentToBytes implements Writ
* From index to start the search from. Defaults to <tt>0</tt>.
*/
public SearchSourceBuilder from(int from) {
if (from < 0) {
throw new IllegalArgumentException("[from] parameter cannot be negative");
}
this.from = from;
return this;
}

@ -360,6 +360,11 @@ public class SearchSourceBuilderTests extends AbstractSearchTestCase {
}
}
public void testNegativeFromErrors() {
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> new SearchSourceBuilder().from(-2));
assertEquals("[from] parameter cannot be negative", expected.getMessage());
}
private void assertIndicesBoostParseErrorMessage(String restContent, String expectedErrorMessage) throws IOException {
try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
ParsingException e = expectThrows(ParsingException.class, () -> SearchSourceBuilder.fromXContent(createParseContext(parser)));

@ -17,6 +17,14 @@ setup:
index: test_1
from: 10000
---
"Request with negative from value":
- do:
catch: /\[from\] parameter cannot be negative/
search:
index: test_1
from: -2
---
"Request window limits with scroll":
- do: