make painless the default scripting language for ScriptProcessor (#20981)

- fixes a bug in the docs that mentions `lang` as optional
- now `lang` defaults to "painless"
This commit is contained in:
Tal Levy 2016-10-18 16:22:01 -07:00 committed by GitHub
parent 31de1fa914
commit 38c650f376
4 changed files with 44 additions and 11 deletions

View File

@ -1432,14 +1432,16 @@ caching see <<modules-scripting-using-caching, Script Caching>>.
.Script Options
[options="header"]
|======
| Name | Required | Default | Description
| `lang` | no | - | The scripting language
| `file` | no | - | The script file to refer to
| `id` | no | - | The stored script id to refer to
| `inline` | no | - | An inline script to be executed
| `params` | no | - | Script Parameters
| Name | Required | Default | Description
| `lang` | no | "painless" | The scripting language
| `file` | no | - | The script file to refer to
| `id` | no | - | The stored script id to refer to
| `inline` | no | - | An inline script to be executed
| `params` | no | - | Script Parameters
|======
One of `file`, `id`, `inline` options must be provided in order to properly reference a script to execute.
You can access the current ingest document from within the script context by using the `ctx` variable.
The following example sets a new field called `field_a_plus_b_times_c` to be the sum of two existing

View File

@ -69,6 +69,10 @@ public final class ScriptProcessor extends AbstractProcessor {
return TYPE;
}
Script getScript() {
return script;
}
public static final class Factory implements Processor.Factory {
private final ScriptService scriptService;
@ -80,7 +84,7 @@ public final class ScriptProcessor extends AbstractProcessor {
@Override
public ScriptProcessor create(Map<String, Processor.Factory> registry, String processorTag,
Map<String, Object> config) throws Exception {
String lang = readStringProperty(TYPE, processorTag, config, "lang");
String lang = readOptionalStringProperty(TYPE, processorTag, config, "lang");
String inline = readOptionalStringProperty(TYPE, processorTag, config, "inline");
String file = readOptionalStringProperty(TYPE, processorTag, config, "file");
String id = readOptionalStringProperty(TYPE, processorTag, config, "id");

View File

@ -20,7 +20,7 @@
package org.elasticsearch.ingest.common;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
@ -29,18 +29,48 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
public class ScriptProcessorFactoryTests extends ESTestCase {
private ScriptProcessor.Factory factory;
private static final Map<String, String> ingestScriptParamToType;
static {
Map<String, String> map = new HashMap<>();
map.put("id", "stored");
map.put("inline", "inline");
map.put("file", "file");
ingestScriptParamToType = Collections.unmodifiableMap(map);
}
@Before
public void init() {
factory = new ScriptProcessor.Factory(mock(ScriptService.class));
}
public void testFactoryValidationWithDefaultLang() throws Exception {
Map<String, Object> configMap = new HashMap<>();
String randomType = randomFrom("id", "inline", "file");
configMap.put(randomType, "foo");
ScriptProcessor processor = factory.create(null, randomAsciiOfLength(10), configMap);
assertThat(processor.getScript().getLang(), equalTo(Script.DEFAULT_SCRIPT_LANG));
assertThat(processor.getScript().getType().toString(), equalTo(ingestScriptParamToType.get(randomType)));
assertThat(processor.getScript().getParams(), equalTo(Collections.emptyMap()));
}
public void testFactoryValidationWithParams() throws Exception {
Map<String, Object> configMap = new HashMap<>();
String randomType = randomFrom("id", "inline", "file");
Map<String, Object> randomParams = Collections.singletonMap(randomAsciiOfLength(10), randomAsciiOfLength(10));
configMap.put(randomType, "foo");
configMap.put("params", randomParams);
ScriptProcessor processor = factory.create(null, randomAsciiOfLength(10), configMap);
assertThat(processor.getScript().getLang(), equalTo(Script.DEFAULT_SCRIPT_LANG));
assertThat(processor.getScript().getType().toString(), equalTo(ingestScriptParamToType.get(randomType)));
assertThat(processor.getScript().getParams(), equalTo(randomParams));
}
public void testFactoryValidationForMultipleScriptingTypes() throws Exception {
Map<String, Object> configMap = new HashMap<>();

View File

@ -9,7 +9,6 @@
"processors": [
{
"script" : {
"lang" : "painless",
"inline": "ctx.bytes_total = (ctx.bytes_in + ctx.bytes_out) * params.factor",
"params": {
"factor": 10
@ -48,7 +47,6 @@
"processors": [
{
"script" : {
"lang" : "painless",
"file": "master"
}
}
@ -94,7 +92,6 @@
"processors": [
{
"script" : {
"lang" : "painless",
"id" : "sum_bytes"
}
}