Skip to content

Commit 059b23e

Browse files
committed
Merge branch 'master' into feature/client_aggs_parsing
2 parents 5fb04fa + 8c6b5a9 commit 059b23e

File tree

38 files changed

+474
-165
lines changed

38 files changed

+474
-165
lines changed

core/src/main/java/org/elasticsearch/common/lucene/search/MoreLikeThisQuery.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,6 @@ public String[] getLikeTexts() {
224224
return likeText;
225225
}
226226

227-
public void setLikeText(String likeText) {
228-
setLikeText(new String[]{likeText});
229-
}
230-
231227
public void setLikeText(String... likeText) {
232228
this.likeText = likeText;
233229
}
@@ -236,15 +232,15 @@ public Fields[] getLikeFields() {
236232
return likeFields;
237233
}
238234

239-
public void setLikeText(Fields... likeFields) {
235+
public void setLikeFields(Fields... likeFields) {
240236
this.likeFields = likeFields;
241237
}
242238

243239
public void setLikeText(List<String> likeText) {
244240
setLikeText(likeText.toArray(Strings.EMPTY_ARRAY));
245241
}
246242

247-
public void setUnlikeText(Fields... unlikeFields) {
243+
public void setUnlikeFields(Fields... unlikeFields) {
248244
this.unlikeFields = unlikeFields;
249245
}
250246

core/src/main/java/org/elasticsearch/common/xcontent/ObjectParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public enum ValueType {
399399
LONG(VALUE_NUMBER, VALUE_STRING),
400400
LONG_OR_NULL(VALUE_NUMBER, VALUE_STRING, VALUE_NULL),
401401
INT(VALUE_NUMBER, VALUE_STRING),
402-
BOOLEAN(VALUE_BOOLEAN),
402+
BOOLEAN(VALUE_BOOLEAN, VALUE_STRING),
403403
STRING_ARRAY(START_ARRAY, VALUE_STRING),
404404
FLOAT_ARRAY(START_ARRAY, VALUE_NUMBER, VALUE_STRING),
405405
DOUBLE_ARRAY(START_ARRAY, VALUE_NUMBER, VALUE_STRING),

core/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ public Item() {
178178
this.index = copy.index;
179179
this.type = copy.type;
180180
this.id = copy.id;
181+
this.routing = copy.routing;
181182
this.doc = copy.doc;
182183
this.xContentType = copy.xContentType;
183184
this.fields = copy.fields;
@@ -343,7 +344,7 @@ XContentType xContentType() {
343344
/**
344345
* Convert this to a {@link TermVectorsRequest} for fetching the terms of the document.
345346
*/
346-
public TermVectorsRequest toTermVectorsRequest() {
347+
TermVectorsRequest toTermVectorsRequest() {
347348
TermVectorsRequest termVectorsRequest = new TermVectorsRequest(index, type, id)
348349
.selectedFields(fields)
349350
.routing(routing)
@@ -1085,14 +1086,14 @@ private Query handleItems(QueryShardContext context, MoreLikeThisQuery mltQuery,
10851086
// fetching the items with multi-termvectors API
10861087
MultiTermVectorsResponse likeItemsResponse = fetchResponse(context.getClient(), likeItems);
10871088
// getting the Fields for liked items
1088-
mltQuery.setLikeText(getFieldsFor(likeItemsResponse));
1089+
mltQuery.setLikeFields(getFieldsFor(likeItemsResponse));
10891090

10901091
// getting the Fields for unliked items
10911092
if (unlikeItems.length > 0) {
10921093
MultiTermVectorsResponse unlikeItemsResponse = fetchResponse(context.getClient(), unlikeItems);
10931094
org.apache.lucene.index.Fields[] unlikeFields = getFieldsFor(unlikeItemsResponse);
10941095
if (unlikeFields.length > 0) {
1095-
mltQuery.setUnlikeText(unlikeFields);
1096+
mltQuery.setUnlikeFields(unlikeFields);
10961097
}
10971098
}
10981099

core/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public long nowInMillis() {
105105
return nowInMillis.getAsLong();
106106
}
107107

108-
public BytesReference getTemplateBytes(Script template) {
108+
public String getTemplateBytes(Script template) {
109109
CompiledTemplate compiledTemplate = scriptService.compileTemplate(template, ScriptContext.Standard.SEARCH);
110110
return compiledTemplate.run(template.getParams());
111111
}

core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ protected final void failIfFrozen() {
392392
}
393393

394394
@Override
395-
public final BytesReference getTemplateBytes(Script template) {
395+
public final String getTemplateBytes(Script template) {
396396
failIfFrozen();
397397
return super.getTemplateBytes(template);
398398
}

core/src/main/java/org/elasticsearch/ingest/InternalTemplateService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Template compile(String template) {
4848
return new Template() {
4949
@Override
5050
public String execute(Map<String, Object> model) {
51-
return compiledTemplate.run(model).utf8ToString();
51+
return compiledTemplate.run(model);
5252
}
5353

5454
@Override

core/src/main/java/org/elasticsearch/script/ScriptContextRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ Collection<ScriptContext> scriptContexts() {
6464
/**
6565
* @return <tt>true</tt> if the provided {@link ScriptContext} is supported, <tt>false</tt> otherwise
6666
*/
67-
boolean isSupportedContext(ScriptContext scriptContext) {
68-
return scriptContexts.containsKey(scriptContext.getKey());
67+
boolean isSupportedContext(String scriptContext) {
68+
return scriptContexts.containsKey(scriptContext);
6969
}
7070

7171
//script contexts can be used in fine-grained settings, we need to be careful with what we allow here

core/src/main/java/org/elasticsearch/script/ScriptModes.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@
1919

2020
package org.elasticsearch.script;
2121

22+
import org.apache.lucene.util.SetOnce;
2223
import org.elasticsearch.common.settings.Setting;
2324
import org.elasticsearch.common.settings.Settings;
2425

26+
import java.util.ArrayList;
2527
import java.util.Collections;
2628
import java.util.HashMap;
29+
import java.util.HashSet;
30+
import java.util.List;
2731
import java.util.Map;
32+
import java.util.Set;
2833
import java.util.TreeMap;
34+
import java.util.function.Function;
2935

3036
/**
3137
* Holds the boolean indicating the enabled mode for each of the different scripting languages available, each script source and each
@@ -38,12 +44,55 @@ public class ScriptModes {
3844

3945
final Map<String, Boolean> scriptEnabled;
4046

41-
ScriptModes(ScriptSettings scriptSettings, Settings settings) {
47+
private static final Setting<List<String>> TYPES_ALLOWED_SETTING =
48+
Setting.listSetting("script.types_allowed", Collections.emptyList(), Function.identity(), Setting.Property.NodeScope);
49+
private static final Setting<List<String>> CONTEXTS_ALLOWED_SETTING =
50+
Setting.listSetting("script.contexts_allowed", Collections.emptyList(), Function.identity(), Setting.Property.NodeScope);
51+
52+
private final Set<String> typesAllowed;
53+
private final Set<String> contextsAllowed;
54+
55+
ScriptModes(ScriptContextRegistry scriptContextRegistry, ScriptSettings scriptSettings, Settings settings) {
4256
HashMap<String, Boolean> scriptModes = new HashMap<>();
4357
for (Setting<Boolean> scriptModeSetting : scriptSettings.getScriptLanguageSettings()) {
4458
scriptModes.put(scriptModeSetting.getKey(), scriptModeSetting.get(settings));
4559
}
4660
this.scriptEnabled = Collections.unmodifiableMap(scriptModes);
61+
62+
typesAllowed = TYPES_ALLOWED_SETTING.exists(settings) ? new HashSet<>() : null;
63+
64+
if (typesAllowed != null) {
65+
for (String settingType : TYPES_ALLOWED_SETTING.get(settings)) {
66+
boolean found = false;
67+
68+
for (ScriptType scriptType : ScriptType.values()) {
69+
if (scriptType.getName().equals(settingType)) {
70+
found = true;
71+
typesAllowed.add(settingType);
72+
73+
break;
74+
}
75+
}
76+
77+
if (!found) {
78+
throw new IllegalArgumentException(
79+
"unknown script type [" + settingType + "] found in setting [" + TYPES_ALLOWED_SETTING.getKey() + "].");
80+
}
81+
}
82+
}
83+
84+
contextsAllowed = CONTEXTS_ALLOWED_SETTING.exists(settings) ? new HashSet<>() : null;
85+
86+
if (contextsAllowed != null) {
87+
for (String settingContext : CONTEXTS_ALLOWED_SETTING.get(settings)) {
88+
if (scriptContextRegistry.isSupportedContext(settingContext)) {
89+
contextsAllowed.add(settingContext);
90+
} else {
91+
throw new IllegalArgumentException(
92+
"unknown script context [" + settingContext + "] found in setting [" + CONTEXTS_ALLOWED_SETTING.getKey() + "].");
93+
}
94+
}
95+
}
4796
}
4897

4998
/**
@@ -60,6 +109,15 @@ public boolean getScriptEnabled(String lang, ScriptType scriptType, ScriptContex
60109
if (NativeScriptEngine.NAME.equals(lang)) {
61110
return true;
62111
}
112+
113+
if (typesAllowed != null && typesAllowed.contains(scriptType.getName()) == false) {
114+
throw new IllegalArgumentException("[" + scriptType.getName() + "] scripts cannot be executed");
115+
}
116+
117+
if (contextsAllowed != null && contextsAllowed.contains(scriptContext.getKey()) == false) {
118+
throw new IllegalArgumentException("[" + scriptContext.getKey() + "] scripts cannot be executed");
119+
}
120+
63121
Boolean scriptMode = scriptEnabled.get(getKey(lang, scriptType, scriptContext));
64122
if (scriptMode == null) {
65123
throw new IllegalArgumentException("script mode not found for lang [" + lang + "], script_type [" + scriptType + "], operation [" + scriptContext.getKey() + "]");

core/src/main/java/org/elasticsearch/script/ScriptService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public ScriptService(Settings settings, Environment env,
152152
this.scriptEnginesByLang = unmodifiableMap(enginesByLangBuilder);
153153
this.scriptEnginesByExt = unmodifiableMap(enginesByExtBuilder);
154154

155-
this.scriptModes = new ScriptModes(scriptSettings, settings);
155+
this.scriptModes = new ScriptModes(scriptContextRegistry, scriptSettings, settings);
156156

157157
// add file watcher for static scripts
158158
scriptsDirectory = env.scriptsFile();
@@ -325,7 +325,7 @@ public CompiledScript compile(Script script, ScriptContext scriptContext) {
325325
/** Compiles a template. Note this will be moved to a separate TemplateService in the future. */
326326
public CompiledTemplate compileTemplate(Script script, ScriptContext scriptContext) {
327327
CompiledScript compiledScript = compile(script, scriptContext);
328-
return params -> (BytesReference)executable(compiledScript, params).run();
328+
return params -> (String)executable(compiledScript, params).run();
329329
}
330330

331331
/**
@@ -511,7 +511,7 @@ private boolean isAnyScriptContextEnabled(String lang, ScriptType scriptType) {
511511

512512
private boolean canExecuteScript(String lang, ScriptType scriptType, ScriptContext scriptContext) {
513513
assert lang != null;
514-
if (scriptContextRegistry.isSupportedContext(scriptContext) == false) {
514+
if (scriptContextRegistry.isSupportedContext(scriptContext.getKey()) == false) {
515515
throw new IllegalArgumentException("script context [" + scriptContext.getKey() + "] not supported");
516516
}
517517
return scriptModes.getScriptEnabled(lang, scriptType, scriptContext);

0 commit comments

Comments
 (0)