Browse Source

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"
Tal Levy 9 năm trước cách đây
mục cha
commit
38c650f376

+ 8 - 6
docs/reference/ingest/ingest-node.asciidoc

@@ -1432,14 +1432,16 @@ caching see <<modules-scripting-using-caching, Script Caching>>.
 .Script Options
 .Script Options
 [options="header"]
 [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.
 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
 The following example sets a new field called `field_a_plus_b_times_c` to be the sum of two existing

+ 5 - 1
modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ScriptProcessor.java

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

+ 31 - 1
modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorFactoryTests.java

@@ -20,7 +20,7 @@
 package org.elasticsearch.ingest.common;
 package org.elasticsearch.ingest.common;
 
 
 import org.elasticsearch.ElasticsearchException;
 import org.elasticsearch.ElasticsearchException;
-import org.elasticsearch.cluster.service.ClusterService;
+import org.elasticsearch.script.Script;
 import org.elasticsearch.script.ScriptService;
 import org.elasticsearch.script.ScriptService;
 import org.elasticsearch.test.ESTestCase;
 import org.elasticsearch.test.ESTestCase;
 import org.junit.Before;
 import org.junit.Before;
@@ -29,18 +29,48 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map;
 
 
+import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.is;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.mock;
 
 
 public class ScriptProcessorFactoryTests extends ESTestCase {
 public class ScriptProcessorFactoryTests extends ESTestCase {
 
 
     private ScriptProcessor.Factory factory;
     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
     @Before
     public void init() {
     public void init() {
         factory = new ScriptProcessor.Factory(mock(ScriptService.class));
         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 {
     public void testFactoryValidationForMultipleScriptingTypes() throws Exception {
         Map<String, Object> configMap = new HashMap<>();
         Map<String, Object> configMap = new HashMap<>();

+ 0 - 3
qa/smoke-test-ingest-with-all-dependencies/src/test/resources/rest-api-spec/test/ingest/50_script_processor_using_painless.yaml

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