Java api: change base class for GetIndexedScriptRequest and improve its javadocs

`GetIndexedScriptRequest` now extends `ActionRequest` instead of `SingleShardOperationRequest`, as the index field that was provided with the previous base class is not needed (hardcoded).

Closes #7553
This commit is contained in:
javanna 2014-09-03 12:31:16 +02:00 committed by Luca Cavanna
parent 851cb3ae8a
commit 19418749e4
3 changed files with 38 additions and 48 deletions

View File

@ -19,9 +19,12 @@
package org.elasticsearch.action.indexedscripts.get; package org.elasticsearch.action.indexedscripts.get;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.ValidateActions; import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest; import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
@ -33,15 +36,12 @@ import org.elasticsearch.search.fetch.source.FetchSourceContext;
import java.io.IOException; import java.io.IOException;
/** /**
* A request to get a document (its source) from an index based on its type/language (optional) and id. Best created using * A request to get an indexed script (its source) based on its language (optional) and id.
* {@link org.elasticsearch.client.Requests#getRequest(String)}. * The operation requires the {@link #scriptLang(String)} and {@link #id(String)} to be set.
* <p/>
* <p>The operation requires the {@link #index()}, {@link #scriptLang(String)} and {@link #id(String)}
* to be set.
* *
* @see GetIndexedScriptResponse * @see GetIndexedScriptResponse
*/ */
public class GetIndexedScriptRequest extends SingleShardOperationRequest<GetIndexedScriptRequest> { public class GetIndexedScriptRequest extends ActionRequest<GetIndexedScriptRequest> implements IndicesRequest {
protected String scriptLang; protected String scriptLang;
protected String id; protected String id;
@ -56,32 +56,28 @@ public class GetIndexedScriptRequest extends SingleShardOperationRequest<GetInde
private VersionType versionType = VersionType.INTERNAL; private VersionType versionType = VersionType.INTERNAL;
private long version = Versions.MATCH_ANY; private long version = Versions.MATCH_ANY;
/** /**
* Constructs a new get request against the script index. The {@link #scriptLang(String)} and {@link #id(String)} * Constructs a new get request against the script index. The {@link #scriptLang(String)} and {@link #id(String)}
* must be set. * must be set.
*/ */
public GetIndexedScriptRequest() { public GetIndexedScriptRequest() {
super(ScriptService.SCRIPT_INDEX);
} }
/** /**
* Constructs a new get request against the script index with the type and id. * Constructs a new get request against the script index with the type and id.
* *
* @param index The index to get the document from * @param scriptLang The language of the script
* @param scriptLang The type of the document * @param id The id of the script
* @param id The id of the document
*/ */
public GetIndexedScriptRequest(String index, String scriptLang, String id) { public GetIndexedScriptRequest(String scriptLang, String id) {
super(index);
this.scriptLang = scriptLang; this.scriptLang = scriptLang;
this.id = id; this.id = id;
} }
@Override @Override
public ActionRequestValidationException validate() { public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = super.validate(); ActionRequestValidationException validationException = null;
if (scriptLang == null) { if (scriptLang == null) {
validationException = ValidateActions.addValidationError("type is missing", validationException); validationException = ValidateActions.addValidationError("type is missing", validationException);
} }
@ -100,8 +96,14 @@ public class GetIndexedScriptRequest extends SingleShardOperationRequest<GetInde
return new String[]{ScriptService.SCRIPT_INDEX}; return new String[]{ScriptService.SCRIPT_INDEX};
} }
@Override
public IndicesOptions indicesOptions() {
return IndicesOptions.strictSingleIndexNoExpandForbidClosed();
}
/** /**
* Sets the type of the document to fetch. * Sets the language of the script to fetch.
*/ */
public GetIndexedScriptRequest scriptLang(@Nullable String type) { public GetIndexedScriptRequest scriptLang(@Nullable String type) {
this.scriptLang = type; this.scriptLang = type;
@ -109,7 +111,7 @@ public class GetIndexedScriptRequest extends SingleShardOperationRequest<GetInde
} }
/** /**
* Sets the id of the document to fetch. * Sets the id of the script to fetch.
*/ */
public GetIndexedScriptRequest id(String id) { public GetIndexedScriptRequest id(String id) {
this.id = id; this.id = id;
@ -125,17 +127,8 @@ public class GetIndexedScriptRequest extends SingleShardOperationRequest<GetInde
return this; return this;
} }
/** /**
* Explicitly specify the fields that will be returned. By default, the <tt>_source</tt> * Sets the preference to execute the get. Defaults to randomize across shards. Can be set to
* field will be returned.
*/
public String[] fields() {
return null;
}
/**
* Sets the preference to execute the search. Defaults to randomize across shards. Can be set to
* <tt>_local</tt> to prefer local shards, <tt>_primary</tt> to execute only on primary shards, or * <tt>_local</tt> to prefer local shards, <tt>_primary</tt> to execute only on primary shards, or
* a custom value, which guarantees that the same order will be used across different requests. * a custom value, which guarantees that the same order will be used across different requests.
*/ */
@ -211,6 +204,10 @@ public class GetIndexedScriptRequest extends SingleShardOperationRequest<GetInde
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
if (in.getVersion().before(Version.V_1_4_0)) {
//the index was previously serialized although not needed
in.readString();
}
scriptLang = in.readString(); scriptLang = in.readString();
id = in.readString(); id = in.readString();
preference = in.readOptionalString(); preference = in.readOptionalString();
@ -231,13 +228,17 @@ public class GetIndexedScriptRequest extends SingleShardOperationRequest<GetInde
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
if (out.getVersion().before(Version.V_1_4_0)) {
//the index was previously serialized although not needed
out.writeString(ScriptService.SCRIPT_INDEX);
}
out.writeString(scriptLang); out.writeString(scriptLang);
out.writeString(id); out.writeString(id);
out.writeOptionalString(preference); out.writeOptionalString(preference);
out.writeBoolean(refresh); out.writeBoolean(refresh);
if (realtime == null) { if (realtime == null) {
out.writeByte((byte) -1); out.writeByte((byte) -1);
} else if (realtime == false) { } else if (!realtime) {
out.writeByte((byte) 0); out.writeByte((byte) 0);
} else { } else {
out.writeByte((byte) 1); out.writeByte((byte) 1);
@ -251,7 +252,6 @@ public class GetIndexedScriptRequest extends SingleShardOperationRequest<GetInde
@Override @Override
public String toString() { public String toString() {
return "[" + index + "][" + scriptLang + "][" + id + "]: routing [" + routing + "]"; return "[" + ScriptService.SCRIPT_INDEX + "][" + scriptLang + "][" + id + "]: routing [" + routing + "]";
} }
} }

View File

@ -19,8 +19,6 @@
package org.elasticsearch.rest.action.script; package org.elasticsearch.rest.action.script;
import org.elasticsearch.ElasticsearchIllegalStateException; import org.elasticsearch.ElasticsearchIllegalStateException;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptRequest; import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptRequest;
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptResponse; import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
@ -28,14 +26,11 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestResponseListener; import org.elasticsearch.rest.action.support.RestResponseListener;
import org.elasticsearch.script.ScriptService;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND; import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
@ -47,8 +42,7 @@ import static org.elasticsearch.rest.RestStatus.OK;
public class RestGetIndexedScriptAction extends BaseRestHandler { public class RestGetIndexedScriptAction extends BaseRestHandler {
@Inject @Inject
public RestGetIndexedScriptAction(Settings settings, Client client, public RestGetIndexedScriptAction(Settings settings, Client client, RestController controller) {
ScriptService scriptService, RestController controller) {
super(settings, client); super(settings, client);
controller.registerHandler(GET, "/_scripts/{lang}/{id}", this); controller.registerHandler(GET, "/_scripts/{lang}/{id}", this);
} }
@ -56,7 +50,7 @@ public class RestGetIndexedScriptAction extends BaseRestHandler {
@Override @Override
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) { public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
final GetIndexedScriptRequest getRequest = new GetIndexedScriptRequest(ScriptService.SCRIPT_INDEX, request.param("lang"), request.param("id")); final GetIndexedScriptRequest getRequest = new GetIndexedScriptRequest(request.param("lang"), request.param("id"));
RestResponseListener<GetIndexedScriptResponse> responseListener = new RestResponseListener<GetIndexedScriptResponse>(channel) { RestResponseListener<GetIndexedScriptResponse> responseListener = new RestResponseListener<GetIndexedScriptResponse>(channel) {
@Override @Override
public RestResponse buildResponse(GetIndexedScriptResponse response) throws Exception { public RestResponse buildResponse(GetIndexedScriptResponse response) throws Exception {

View File

@ -19,21 +19,18 @@
package org.elasticsearch.rest.action.template; package org.elasticsearch.rest.action.template;
import org.elasticsearch.ElasticsearchIllegalStateException; import org.elasticsearch.ElasticsearchIllegalStateException;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptRequest; import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptRequest;
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptResponse; import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.*; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.*; import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestResponseListener; import org.elasticsearch.rest.action.support.RestResponseListener;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND; import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
@ -45,15 +42,14 @@ import static org.elasticsearch.rest.RestStatus.OK;
public class RestGetSearchTemplateAction extends BaseRestHandler { public class RestGetSearchTemplateAction extends BaseRestHandler {
@Inject @Inject
public RestGetSearchTemplateAction(Settings settings, Client client, public RestGetSearchTemplateAction(Settings settings, Client client, RestController controller) {
RestController controller, ScriptService scriptService) {
super(settings, client); super(settings, client);
controller.registerHandler(GET, "/_search/template/{id}", this); controller.registerHandler(GET, "/_search/template/{id}", this);
} }
@Override @Override
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) { public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
final GetIndexedScriptRequest getRequest = new GetIndexedScriptRequest(ScriptService.SCRIPT_INDEX, "mustache", request.param("id")); final GetIndexedScriptRequest getRequest = new GetIndexedScriptRequest("mustache", request.param("id"));
RestResponseListener<GetIndexedScriptResponse> responseListener = new RestResponseListener<GetIndexedScriptResponse>(channel) { RestResponseListener<GetIndexedScriptResponse> responseListener = new RestResponseListener<GetIndexedScriptResponse>(channel) {
@Override @Override
public RestResponse buildResponse(GetIndexedScriptResponse response) throws Exception { public RestResponse buildResponse(GetIndexedScriptResponse response) throws Exception {