Skip to content

Commit 5c0201b

Browse files
authored
Deprecate ignored type parameter in search_shards api (#21730)
The `type` parameter has always been accepted by the search_shards api, probably to make the api and its urls the same as search. Truth is that the type never had any effect, it's been ignored from day one while accepting it may make users think that we actually do something with it. This commit deprecate support for the type parameter from the REST layer and removes it from the Java API. Backwards compatibility is maintained on the transport layer though. The new added serialization test also uncovered a bug in the java API where the `ClusterSearchShardsRequest` could be created with no arguments, but the indices were required to be not null otherwise the request couldn't be serialized as `writeTo` would throw NPE. Fixed by setting a default value (empty array) for indices. Relates to #21688
1 parent ba87035 commit 5c0201b

File tree

6 files changed

+121
-40
lines changed

6 files changed

+121
-40
lines changed

core/src/main/java/org/elasticsearch/action/admin/cluster/shards/ClusterSearchShardsRequest.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action.admin.cluster.shards;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.action.ActionRequestValidationException;
2324
import org.elasticsearch.action.IndicesRequest;
2425
import org.elasticsearch.action.support.IndicesOptions;
@@ -29,16 +30,16 @@
2930
import org.elasticsearch.common.io.stream.StreamOutput;
3031

3132
import java.io.IOException;
33+
import java.util.Objects;
3234

3335
/**
3436
*/
3537
public class ClusterSearchShardsRequest extends MasterNodeReadRequest<ClusterSearchShardsRequest> implements IndicesRequest.Replaceable {
36-
private String[] indices;
38+
private String[] indices = Strings.EMPTY_ARRAY;
3739
@Nullable
3840
private String routing;
3941
@Nullable
4042
private String preference;
41-
private String[] types = Strings.EMPTY_ARRAY;
4243
private IndicesOptions indicesOptions = IndicesOptions.lenientExpandOpen();
4344

4445

@@ -59,14 +60,9 @@ public ActionRequestValidationException validate() {
5960
*/
6061
@Override
6162
public ClusterSearchShardsRequest indices(String... indices) {
62-
if (indices == null) {
63-
throw new IllegalArgumentException("indices must not be null");
64-
} else {
65-
for (int i = 0; i < indices.length; i++) {
66-
if (indices[i] == null) {
67-
throw new IllegalArgumentException("indices[" + i + "] must not be null");
68-
}
69-
}
63+
Objects.requireNonNull(indices, "indices must not be null");
64+
for (int i = 0; i < indices.length; i++) {
65+
Objects.requireNonNull(indices[i], "indices[" + i + "] must not be null");
7066
}
7167
this.indices = indices;
7268
return this;
@@ -90,23 +86,6 @@ public ClusterSearchShardsRequest indicesOptions(IndicesOptions indicesOptions)
9086
return this;
9187
}
9288

93-
/**
94-
* The document types to execute the search against. Defaults to be executed against
95-
* all types.
96-
*/
97-
public String[] types() {
98-
return types;
99-
}
100-
101-
/**
102-
* The document types to execute the search against. Defaults to be executed against
103-
* all types.
104-
*/
105-
public ClusterSearchShardsRequest types(String... types) {
106-
this.types = types;
107-
return this;
108-
}
109-
11089
/**
11190
* A comma separated list of routing values to control the shards the search will be executed on.
11291
*/
@@ -156,7 +135,10 @@ public void readFrom(StreamInput in) throws IOException {
156135
routing = in.readOptionalString();
157136
preference = in.readOptionalString();
158137

159-
types = in.readStringArray();
138+
if (in.getVersion().onOrBefore(Version.V_5_1_0)) {
139+
//types
140+
in.readStringArray();
141+
}
160142
indicesOptions = IndicesOptions.readIndicesOptions(in);
161143
}
162144

@@ -172,7 +154,10 @@ public void writeTo(StreamOutput out) throws IOException {
172154
out.writeOptionalString(routing);
173155
out.writeOptionalString(preference);
174156

175-
out.writeStringArray(types);
157+
if (out.getVersion().onOrBefore(Version.V_5_1_0)) {
158+
//types
159+
out.writeStringArray(Strings.EMPTY_ARRAY);
160+
}
176161
indicesOptions.writeIndicesOptions(out);
177162
}
178163

core/src/main/java/org/elasticsearch/action/admin/cluster/shards/ClusterSearchShardsRequestBuilder.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ public ClusterSearchShardsRequestBuilder setIndices(String... indices) {
3939
return this;
4040
}
4141

42-
/**
43-
* The document types to execute the search against. Defaults to be executed against
44-
* all types.
45-
*/
46-
public ClusterSearchShardsRequestBuilder setTypes(String... types) {
47-
request.types(types);
48-
return this;
49-
}
50-
5142
/**
5243
* A comma separated list of routing values to control the shards the search will be executed on.
5344
*/

core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterSearchShardsAction.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
5555
final ClusterSearchShardsRequest clusterSearchShardsRequest = Requests.clusterSearchShardsRequest(indices);
5656
clusterSearchShardsRequest.local(request.paramAsBoolean("local", clusterSearchShardsRequest.local()));
5757

58-
clusterSearchShardsRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
58+
if (request.hasParam("type")) {
59+
String type = request.param("type");
60+
deprecationLogger.deprecated("type [" + type + "] doesn't have any effect in the search shards api, " +
61+
"it should be rather omitted");
62+
}
5963
clusterSearchShardsRequest.routing(request.param("routing"));
6064
clusterSearchShardsRequest.preference(request.param("preference"));
6165
clusterSearchShardsRequest.indicesOptions(IndicesOptions.fromRequest(request, clusterSearchShardsRequest.indicesOptions()));
62-
6366
return channel -> client.admin().cluster().searchShards(clusterSearchShardsRequest, new RestToXContentListener<>(channel));
6467
}
6568
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action.admin.cluster.shards;
21+
22+
import org.elasticsearch.Version;
23+
import org.elasticsearch.action.support.IndicesOptions;
24+
import org.elasticsearch.common.io.stream.BytesStreamOutput;
25+
import org.elasticsearch.common.io.stream.StreamInput;
26+
import org.elasticsearch.test.ESTestCase;
27+
import org.elasticsearch.test.VersionUtils;
28+
29+
public class ClusterSearchShardsRequestTests extends ESTestCase {
30+
31+
public void testSerialization() throws Exception {
32+
ClusterSearchShardsRequest request = new ClusterSearchShardsRequest();
33+
if (randomBoolean()) {
34+
int numIndices = randomIntBetween(1, 5);
35+
String[] indices = new String[numIndices];
36+
for (int i = 0; i < numIndices; i++) {
37+
indices[i] = randomAsciiOfLengthBetween(3, 10);
38+
}
39+
request.indices(indices);
40+
}
41+
if (randomBoolean()) {
42+
request.indicesOptions(
43+
IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()));
44+
}
45+
if (randomBoolean()) {
46+
request.preference(randomAsciiOfLengthBetween(3, 10));
47+
}
48+
if (randomBoolean()) {
49+
int numRoutings = randomIntBetween(1, 3);
50+
String[] routings = new String[numRoutings];
51+
for (int i = 0; i < numRoutings; i++) {
52+
routings[i] = randomAsciiOfLengthBetween(3, 10);
53+
}
54+
request.routing(routings);
55+
}
56+
57+
Version version = VersionUtils.randomVersion(random());
58+
try (BytesStreamOutput out = new BytesStreamOutput()) {
59+
out.setVersion(version);
60+
request.writeTo(out);
61+
try (StreamInput in = out.bytes().streamInput()) {
62+
in.setVersion(version);
63+
ClusterSearchShardsRequest deserialized = new ClusterSearchShardsRequest();
64+
deserialized.readFrom(in);
65+
assertArrayEquals(request.indices(), deserialized.indices());
66+
assertSame(request.indicesOptions(), deserialized.indicesOptions());
67+
assertEquals(request.routing(), deserialized.routing());
68+
assertEquals(request.preference(), deserialized.preference());
69+
}
70+
}
71+
}
72+
73+
public void testIndicesMustNotBeNull() {
74+
ClusterSearchShardsRequest request = new ClusterSearchShardsRequest();
75+
assertNotNull(request.indices());
76+
expectThrows(NullPointerException.class, () -> request.indices((String[])null));
77+
expectThrows(NullPointerException.class, () -> request.indices((String)null));
78+
expectThrows(NullPointerException.class, () -> request.indices(new String[]{"index1", null, "index3"}));
79+
}
80+
}

docs/reference/search/search-shards.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ planning optimizations with routing and shard preferences.
77

88
The `index` and `type` parameters may be single values, or comma-separated.
99

10+
The `type` parameter is deprecated deprecated[5.1.0,was ignored in previous versions].
11+
1012
[float]
1113
=== Usage
1214

rest-api-spec/src/main/resources/rest-api-spec/test/search_shards/10_basic.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,23 @@
1010
routing: foo
1111

1212
- match: { shards.0.0.index: test_1 }
13+
14+
---
15+
"Search shards with type":
16+
- skip:
17+
features: "warnings"
18+
version: " - 5.0.99"
19+
reason: type was deprecated in 5.1.0
20+
21+
- do:
22+
indices.create:
23+
index: test_1
24+
25+
- do:
26+
warnings:
27+
- type [whatever] doesn't have any effect in the search shards api, it should be rather omitted
28+
search_shards:
29+
index: test_1
30+
type: whatever
31+
32+
- match: { shards.0.0.index: test_1 }

0 commit comments

Comments
 (0)