Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up various issues #7787

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions rest-api-spec/test/template/20_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@
body: { "id" : "1", "params" : { "my_value" : "value1_foo", "my_size" : 1 } }
- match: { hits.total: 1 }

- do:
catch: /ElasticsearchIllegalArgumentException.Unable.to.find.on.disk.script.simple1/
search_template:
body: { "template" : "simple1" }

Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ public class GetIndexedScriptRequest extends ActionRequest<GetIndexedScriptReque

protected String scriptLang;
protected String id;
protected String preference;
protected String routing;
private FetchSourceContext fetchSourceContext;

private boolean refresh = false;

Boolean realtime;

private VersionType versionType = VersionType.INTERNAL;
private long version = Versions.MATCH_ANY;
Expand Down Expand Up @@ -117,24 +110,6 @@ public GetIndexedScriptRequest id(String id) {
return this;
}

/**
* Controls the shard routing of the request. Using this value to hash the shard
* and not the id.
*/
public GetIndexedScriptRequest routing(String routing) {
this.routing = routing;
return this;
}

/**
* Sets the preference to execute the get. 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
* a custom value, which guarantees that the same order will be used across different requests.
*/
public GetIndexedScriptRequest preference(String preference) {
this.preference = preference;
return this;
}

public String scriptLang() {
return scriptLang;
Expand All @@ -144,37 +119,6 @@ public String id() {
return id;
}

public String routing() {
return routing;
}

public String preference() {
return this.preference;
}

/**
* Should a refresh be executed before this get operation causing the operation to
* return the latest value. Note, heavy get should not set this to <tt>true</tt>. Defaults
* to <tt>false</tt>.
*/
public GetIndexedScriptRequest refresh(boolean refresh) {
this.refresh = refresh;
return this;
}

public boolean refresh() {
return this.refresh;
}

public boolean realtime() {
return this.realtime == null ? true : this.realtime;
}

public GetIndexedScriptRequest realtime(Boolean realtime) {
this.realtime = realtime;
return this;
}

/**
* Sets the version, which will cause the get operation to only be performed if a matching
* version exists and no changes happened on the doc since then.
Expand Down Expand Up @@ -209,19 +153,17 @@ public void readFrom(StreamInput in) throws IOException {
}
scriptLang = in.readString();
id = in.readString();
preference = in.readOptionalString();
refresh = in.readBoolean();
byte realtime = in.readByte();
if (realtime == 0) {
this.realtime = false;
} else if (realtime == 1) {
this.realtime = true;
if (in.getVersion().before(Version.V_1_5_0)) {
in.readOptionalString(); //Preference
in.readBoolean(); //Refresh
in.readByte(); //Realtime
}

this.versionType = VersionType.fromValue(in.readByte());
this.version = Versions.readVersionWithVLongForBW(in);

fetchSourceContext = FetchSourceContext.optionalReadFromStream(in);
if (in.getVersion().before(Version.V_1_5_0)) {
FetchSourceContext.optionalReadFromStream(in);
}
}

@Override
Expand All @@ -233,24 +175,23 @@ public void writeTo(StreamOutput out) throws IOException {
}
out.writeString(scriptLang);
out.writeString(id);
out.writeOptionalString(preference);
out.writeBoolean(refresh);
if (realtime == null) {
out.writeByte((byte) -1);
} else if (!realtime) {
out.writeByte((byte) 0);
} else {
out.writeByte((byte) 1);
if (out.getVersion().before(Version.V_1_5_0)) {
out.writeOptionalString("_local"); //Preference
out.writeBoolean(true); //Refresh
out.writeByte((byte) -1); //Realtime
}

out.writeByte(versionType.getValue());
Versions.writeVersionWithVLongForBW(version, out);

FetchSourceContext.optionalWriteToStream(fetchSourceContext, out);
if (out.getVersion().before(Version.V_1_5_0)) {
FetchSourceContext.optionalWriteToStream(null, out);
}

}

@Override
public String toString() {
return "[" + ScriptService.SCRIPT_INDEX + "][" + scriptLang + "][" + id + "]: routing [" + routing + "]";
return "[" + ScriptService.SCRIPT_INDEX + "][" + scriptLang + "][" + id + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,6 @@ public GetIndexedScriptRequestBuilder setId(String id) {
return this;
}

/**
* 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
* a custom value, which guarantees that the same order will be used across different requests.
*/
public GetIndexedScriptRequestBuilder setPreference(String preference) {
request.preference(preference);
return this;
}

/**
* Should a refresh be executed before this get operation causing the operation to
* return the latest value. Note, heavy get should not set this to <tt>true</tt>. Defaults
* to <tt>false</tt>.
*/
public GetIndexedScriptRequestBuilder setRefresh(boolean refresh) {
request.refresh(refresh);
return this;
}

public GetIndexedScriptRequestBuilder setRealtime(Boolean realtime) {
request.realtime(realtime);
return this;
}

/**
* Sets the version, which will cause the get operation to only be performed if a matching
* version exists and no changes happened on the doc since then.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ public static TemplateContext parse(XContentParser parser, String paramsFieldnam
currentFieldName = parser.currentName();
} else if (parameterMap.containsKey(currentFieldName)) {
type = parameterMap.get(currentFieldName);



if (token == XContentParser.Token.START_OBJECT && !parser.hasTextCharacters()) {
XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent());
builder.copyCurrentStructure(parser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
*/
public class RestGetIndexedScriptAction extends BaseRestHandler {

private final static String LANG_FIELD = "lang";
private final static String ID_FIELD = "_id";
private final static String VERSION_FIELD = "_version";
private final static String SCRIPT_FIELD = "script";

@Inject
public RestGetIndexedScriptAction(Settings settings, RestController controller, Client client) {
this(settings, controller, true, client);
Expand All @@ -54,14 +59,15 @@ protected RestGetIndexedScriptAction(Settings settings, RestController controlle
}
}

protected String getScriptLang(RestRequest request) {
return request.param("lang");
protected String getScriptFieldName() {
return SCRIPT_FIELD;
}

protected String getScriptFieldName() {
return "script";
protected String getScriptLang(RestRequest request) {
return request.param(LANG_FIELD);
}


@Override
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
final GetIndexedScriptRequest getRequest = new GetIndexedScriptRequest(getScriptLang(request), request.param("id"));
Expand All @@ -78,6 +84,9 @@ public RestResponse buildResponse(GetIndexedScriptResponse response) throws Exce
String script = response.getScript();
builder.startObject();
builder.field(getScriptFieldName(), script);
builder.field(VERSION_FIELD, response.getVersion());
builder.field(LANG_FIELD, response.getScriptLang());
builder.field(ID_FIELD, response.getId());
builder.endObject();
return new BytesRestResponse(OK, builder);
} catch( IOException|ClassCastException e ){
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/org/elasticsearch/search/SearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.carrotsearch.hppc.ObjectOpenHashSet;
import com.carrotsearch.hppc.ObjectSet;
import com.carrotsearch.hppc.cursors.ObjectCursor;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.NumericDocValues;
Expand Down Expand Up @@ -77,7 +78,6 @@
import org.elasticsearch.threadpool.ThreadPool;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -614,11 +614,21 @@ private void parseTemplate(ShardSearchRequest request) {

if (templateContext.scriptType().equals(ScriptService.ScriptType.INLINE)) {
//Try to double parse for nested template id/file
parser = XContentFactory.xContent(templateContext.template().getBytes(Charset.defaultCharset())).createParser(templateContext.template().getBytes(Charset.defaultCharset()));
TemplateQueryParser.TemplateContext innerContext = TemplateQueryParser.parse(parser, "params");
if (hasLength(innerContext.template()) && !innerContext.scriptType().equals(ScriptService.ScriptType.INLINE)) {
//An inner template referring to a filename or id
templateContext = new TemplateQueryParser.TemplateContext(innerContext.scriptType(), innerContext.template(), templateContext.params());
parser = null;
try {
byte[] templateBytes = templateContext.template().getBytes(Charsets.UTF_8);
parser = XContentFactory.xContent(templateBytes).createParser(templateBytes);
} catch (ElasticsearchParseException epe) {
//This was an non-nested template, the parse failure was due to this, it is safe to assume this refers to a file
//for backwards compatibility and keep going
templateContext = new TemplateQueryParser.TemplateContext(ScriptService.ScriptType.FILE, templateContext.template(), templateContext.params());
}
if (parser != null) {
TemplateQueryParser.TemplateContext innerContext = TemplateQueryParser.parse(parser, "params");
if (hasLength(innerContext.template()) && !innerContext.scriptType().equals(ScriptService.ScriptType.INLINE)) {
//An inner template referring to a filename or id
templateContext = new TemplateQueryParser.TemplateContext(innerContext.scriptType(), innerContext.template(), templateContext.params());
}
}
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,10 @@ public class GetIndexedScriptRequestTests extends ElasticsearchTestCase {
@Test
public void testGetIndexedScriptRequestSerialization() throws IOException {
GetIndexedScriptRequest request = new GetIndexedScriptRequest("lang", "id");
if (randomBoolean()) {
request.realtime(false);
}
if (randomBoolean()) {
request.refresh(true);
}
if (randomBoolean()) {
request.version(randomIntBetween(1, Integer.MAX_VALUE));
request.versionType(randomFrom(VersionType.values()));
}
if (randomBoolean()) {
request.routing(randomAsciiOfLength(randomIntBetween(1, 10)));
}

BytesStreamOutput out = new BytesStreamOutput();
out.setVersion(randomVersion());
Expand All @@ -59,8 +50,6 @@ public void testGetIndexedScriptRequestSerialization() throws IOException {

assertThat(request2.id(), equalTo(request.id()));
assertThat(request2.scriptLang(), equalTo(request.scriptLang()));
assertThat(request2.realtime(), equalTo(request.realtime()));
assertThat(request2.refresh(), equalTo(request.refresh()));
assertThat(request2.version(), equalTo(request.version()));
assertThat(request2.versionType(), equalTo(request.versionType()));
}
Expand Down