Skip to content

Commit c3a8674

Browse files
author
Christoph Büscher
authored
Completely disallow setting negative size in search (#70209)
We used to treat setting size to -1 in search request bodies or as a rest parameter as a no-op, using the default search size of 10 in this case. This lenient behaviour was deprecated in #69548 and is removed with this PR in 8.0. Relates to #69548
1 parent 3167910 commit c3a8674

File tree

5 files changed

+7
-60
lines changed

5 files changed

+7
-60
lines changed

rest-api-spec/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ tasks.named("yamlRestCompatTest").configure {
340340
'search/160_exists_query/Test exists query on _type field',
341341
'search/171_terms_query_with_types/Terms Query with No.of terms exceeding index.max_terms_count should FAIL',
342342
'search/20_default_values/Basic search',
343+
'search/260_parameter_validation/test size=-1 is deprecated',
343344
'search/310_match_bool_prefix/multi_match multiple fields with cutoff_frequency throws exception',
344345
'search/340_type_query/type query',
345346
'search/40_indices_boost/Indices boost using object',

rest-api-spec/src/main/resources/rest-api-spec/test/search/260_parameter_validation.yml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ setup:
1818
- '{ "index" : { "_index" : "index1", "_id" : "1" } }'
1919
- '{ "foo": "bar"}'
2020

21-
---
22-
"test size=-1 is deprecated":
23-
- skip:
24-
version: " - 7.99.99"
25-
reason: deprecation so far only in 8.0, lower when backported
26-
features: allowed_warnings
27-
28-
- do:
29-
allowed_warnings:
30-
- "Using search size of -1 is deprecated and will be removed in future versions. Instead, don't use the `size` parameter if you don't want to set it explicitely."
31-
search:
32-
rest_total_hits_as_int: true
33-
index: index1
34-
size: -1
35-
36-
- length: { hits.hits: 1 }
37-
3821
---
3922
"test negative size throws IAE":
4023

server/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import org.elasticsearch.common.Booleans;
2020
import org.elasticsearch.common.Strings;
2121
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
22-
import org.elasticsearch.common.logging.DeprecationCategory;
23-
import org.elasticsearch.common.logging.DeprecationLogger;
2422
import org.elasticsearch.common.xcontent.XContentParser;
2523
import org.elasticsearch.index.query.QueryBuilder;
2624
import org.elasticsearch.rest.BaseRestHandler;
@@ -29,6 +27,7 @@
2927
import org.elasticsearch.rest.action.RestCancellableNodeClient;
3028
import org.elasticsearch.rest.action.RestStatusToXContentListener;
3129
import org.elasticsearch.search.Scroll;
30+
import org.elasticsearch.search.SearchService;
3231
import org.elasticsearch.search.builder.SearchSourceBuilder;
3332
import org.elasticsearch.search.fetch.StoredFieldsContext;
3433
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
@@ -60,8 +59,6 @@ public class RestSearchAction extends BaseRestHandler {
6059
public static final String TYPED_KEYS_PARAM = "typed_keys";
6160
private static final Set<String> RESPONSE_PARAMS;
6261

63-
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(RestSearchAction.class);
64-
6562
static {
6663
final Set<String> responseParams = new HashSet<>(Arrays.asList(TYPED_KEYS_PARAM, TOTAL_HITS_AS_INT_PARAM));
6764
RESPONSE_PARAMS = Collections.unmodifiableSet(responseParams);
@@ -195,17 +192,8 @@ private static void parseSearchSource(final SearchSourceBuilder searchSourceBuil
195192
searchSourceBuilder.from(request.paramAsInt("from", 0));
196193
}
197194
if (request.hasParam("size")) {
198-
int size = request.paramAsInt("size", -1);
199-
if (size != -1) {
200-
setSize.accept(size);
201-
} else {
202-
deprecationLogger.deprecate(
203-
DeprecationCategory.API,
204-
"search-api-size-1",
205-
"Using search size of -1 is deprecated and will be removed in future versions. Instead, don't use the `size` parameter "
206-
+ "if you don't want to set it explicitely."
207-
);
208-
}
195+
int size = request.paramAsInt("size", SearchService.DEFAULT_SIZE);
196+
setSize.accept(size);
209197
}
210198

211199
if (request.hasParam("explain")) {

server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import org.elasticsearch.common.io.stream.StreamInput;
1919
import org.elasticsearch.common.io.stream.StreamOutput;
2020
import org.elasticsearch.common.io.stream.Writeable;
21-
import org.elasticsearch.common.logging.DeprecationCategory;
22-
import org.elasticsearch.common.logging.DeprecationLogger;
2321
import org.elasticsearch.common.unit.TimeValue;
2422
import org.elasticsearch.common.xcontent.ToXContentFragment;
2523
import org.elasticsearch.common.xcontent.ToXContentObject;
@@ -105,8 +103,6 @@ public final class SearchSourceBuilder implements Writeable, ToXContentObject, R
105103
public static final ParseField POINT_IN_TIME = new ParseField("pit");
106104
public static final ParseField RUNTIME_MAPPINGS_FIELD = new ParseField("runtime_mappings");
107105

108-
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(SearchSourceBuilder.class);
109-
110106
public static SearchSourceBuilder fromXContent(XContentParser parser) throws IOException {
111107
return fromXContent(parser, true);
112108
}
@@ -1121,19 +1117,7 @@ public void parseXContent(XContentParser parser, boolean checkTrailingTokens) th
11211117
if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
11221118
from(parser.intValue());
11231119
} else if (SIZE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
1124-
int parsedSize = parser.intValue();
1125-
// we treat -1 as not-set, but deprecate it to be able to later remove this funny extra treatment
1126-
if (parsedSize != -1) {
1127-
size(parsedSize);
1128-
} else {
1129-
deprecationLogger.deprecate(
1130-
DeprecationCategory.API,
1131-
"search-api-size-1",
1132-
"Using search size of -1 is deprecated and will be removed in future versions. "
1133-
+ "Instead, don't use the `size` parameter if you don't want to set it explicitely."
1134-
);
1135-
}
1136-
1120+
size(parser.intValue());
11371121
} else if (TIMEOUT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
11381122
timeout = TimeValue.parseTimeValue(parser.text(), null, TIMEOUT_FIELD.getPreferredName());
11391123
} else if (TERMINATE_AFTER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {

server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -460,23 +460,14 @@ public void testNegativeFromErrors() {
460460
}
461461

462462
public void testNegativeSizeErrors() throws IOException {
463-
int randomSize = randomIntBetween(-100000, -2);
463+
int randomSize = randomIntBetween(-100000, -1);
464464
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class,
465465
() -> new SearchSourceBuilder().size(randomSize));
466466
assertEquals("[size] parameter cannot be negative, found [" + randomSize + "]", expected.getMessage());
467467
expected = expectThrows(IllegalArgumentException.class, () -> new SearchSourceBuilder().size(-1));
468468
assertEquals("[size] parameter cannot be negative, found [-1]", expected.getMessage());
469469

470-
// we don't want to error on -1 for bwc reasons, this is treated as if the value is unset later
471-
String restContent = "{\"size\" : -1}";
472-
try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
473-
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser);
474-
assertEquals(-1, searchSourceBuilder.size());
475-
}
476-
assertWarnings("Using search size of -1 is deprecated and will be removed in future versions. Instead, don't use the `size` "
477-
+ "parameter if you don't want to set it explicitely.");
478-
479-
restContent = "{\"size\" : " + randomSize + "}";
470+
String restContent = "{\"size\" : " + randomSize + "}";
480471
try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) {
481472
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> SearchSourceBuilder.fromXContent(parser));
482473
assertThat(ex.getMessage(), containsString(Integer.toString(randomSize)));

0 commit comments

Comments
 (0)