Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions;
Expand All @@ -29,16 +30,16 @@
import org.elasticsearch.common.io.stream.StreamOutput;

import java.io.IOException;
import java.util.Objects;

/**
*/
public class ClusterSearchShardsRequest extends MasterNodeReadRequest<ClusterSearchShardsRequest> implements IndicesRequest.Replaceable {
private String[] indices;
private String[] indices = Strings.EMPTY_ARRAY;
@Nullable
private String routing;
@Nullable
private String preference;
private String[] types = Strings.EMPTY_ARRAY;
private IndicesOptions indicesOptions = IndicesOptions.lenientExpandOpen();


Expand All @@ -59,14 +60,9 @@ public ActionRequestValidationException validate() {
*/
@Override
public ClusterSearchShardsRequest indices(String... indices) {
if (indices == null) {
throw new IllegalArgumentException("indices must not be null");
} else {
for (int i = 0; i < indices.length; i++) {
if (indices[i] == null) {
throw new IllegalArgumentException("indices[" + i + "] must not be null");
}
}
Objects.requireNonNull(indices, "indices must not be null");
for (int i = 0; i < indices.length; i++) {
Objects.requireNonNull(indices[i], "indices[" + i + "] must not be null");
}
this.indices = indices;
return this;
Expand All @@ -90,23 +86,6 @@ public ClusterSearchShardsRequest indicesOptions(IndicesOptions indicesOptions)
return this;
}

/**
* The document types to execute the search against. Defaults to be executed against
* all types.
*/
public String[] types() {
return types;
}

/**
* The document types to execute the search against. Defaults to be executed against
* all types.
*/
public ClusterSearchShardsRequest types(String... types) {
this.types = types;
return this;
}

/**
* A comma separated list of routing values to control the shards the search will be executed on.
*/
Expand Down Expand Up @@ -156,7 +135,10 @@ public void readFrom(StreamInput in) throws IOException {
routing = in.readOptionalString();
preference = in.readOptionalString();

types = in.readStringArray();
if (in.getVersion().onOrBefore(Version.V_5_1_0)) {
//types
in.readStringArray();
}
indicesOptions = IndicesOptions.readIndicesOptions(in);
}

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

out.writeStringArray(types);
if (out.getVersion().onOrBefore(Version.V_5_1_0)) {
//types
out.writeStringArray(Strings.EMPTY_ARRAY);
}
indicesOptions.writeIndicesOptions(out);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ public ClusterSearchShardsRequestBuilder setIndices(String... indices) {
return this;
}

/**
* The document types to execute the search against. Defaults to be executed against
* all types.
*/
public ClusterSearchShardsRequestBuilder setTypes(String... types) {
request.types(types);
return this;
}

/**
* A comma separated list of routing values to control the shards the search will be executed on.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
final ClusterSearchShardsRequest clusterSearchShardsRequest = Requests.clusterSearchShardsRequest(indices);
clusterSearchShardsRequest.local(request.paramAsBoolean("local", clusterSearchShardsRequest.local()));

clusterSearchShardsRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
if (request.hasParam("type")) {
String type = request.param("type");
deprecationLogger.deprecated("type [" + type + "] doesn't have any effect in the search shards api, " +
"it should be rather omitted");
}
clusterSearchShardsRequest.routing(request.param("routing"));
clusterSearchShardsRequest.preference(request.param("preference"));
clusterSearchShardsRequest.indicesOptions(IndicesOptions.fromRequest(request, clusterSearchShardsRequest.indicesOptions()));

return channel -> client.admin().cluster().searchShards(clusterSearchShardsRequest, new RestToXContentListener<>(channel));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import org.elasticsearch.Version;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.VersionUtils;

public class ClusterSearchShardsRequestTests extends ESTestCase {

public void testSerialization() throws Exception {
ClusterSearchShardsRequest request = new ClusterSearchShardsRequest();
if (randomBoolean()) {
int numIndices = randomIntBetween(1, 5);
String[] indices = new String[numIndices];
for (int i = 0; i < numIndices; i++) {
indices[i] = randomAsciiOfLengthBetween(3, 10);
}
request.indices(indices);
}
if (randomBoolean()) {
request.indicesOptions(
IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()));
}
if (randomBoolean()) {
request.preference(randomAsciiOfLengthBetween(3, 10));
}
if (randomBoolean()) {
int numRoutings = randomIntBetween(1, 3);
String[] routings = new String[numRoutings];
for (int i = 0; i < numRoutings; i++) {
routings[i] = randomAsciiOfLengthBetween(3, 10);
}
request.routing(routings);
}

Version version = VersionUtils.randomVersion(random());
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.setVersion(version);
request.writeTo(out);
try (StreamInput in = out.bytes().streamInput()) {
in.setVersion(version);
ClusterSearchShardsRequest deserialized = new ClusterSearchShardsRequest();
deserialized.readFrom(in);
assertArrayEquals(request.indices(), deserialized.indices());
assertSame(request.indicesOptions(), deserialized.indicesOptions());
assertEquals(request.routing(), deserialized.routing());
assertEquals(request.preference(), deserialized.preference());
}
}
}

public void testIndicesMustNotBeNull() {
ClusterSearchShardsRequest request = new ClusterSearchShardsRequest();
assertNotNull(request.indices());
expectThrows(NullPointerException.class, () -> request.indices((String[])null));
expectThrows(NullPointerException.class, () -> request.indices((String)null));
expectThrows(NullPointerException.class, () -> request.indices(new String[]{"index1", null, "index3"}));
}
}
2 changes: 2 additions & 0 deletions docs/reference/search/search-shards.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ planning optimizations with routing and shard preferences.

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

The `type` parameter is deprecated deprecated[5.1.0,was ignored in previous versions].

[float]
=== Usage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,23 @@
routing: foo

- match: { shards.0.0.index: test_1 }

---
"Search shards with type":
- skip:
features: "warnings"
version: " - 5.0.99"
reason: type was deprecated in 5.1.0

- do:
indices.create:
index: test_1

- do:
warnings:
- type [whatever] doesn't have any effect in the search shards api, it should be rather omitted
search_shards:
index: test_1
type: whatever

- match: { shards.0.0.index: test_1 }