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

Add support for parameters to the script ingest processor #20136

Merged
merged 1 commit into from
Aug 25, 2016
Merged
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
11 changes: 7 additions & 4 deletions docs/reference/ingest/ingest-node.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1356,20 +1356,23 @@ caching see <<modules-scripting-using-caching, Script Caching>>.
| `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
|======

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` to be the sum of two existing
numeric fields `field_a` and `field_b`:
The following example sets a new field called `field_a_plus_b_times_c` to be the sum of two existing
numeric fields `field_a` and `field_b` multiplied by the parameter param_c:

[source,js]
--------------------------------------------------
{
"script": {
"field": "field_a_plus_b",
"lang": "painless",
"inline": "return ctx.field_a + ctx.field_b"
"inline": "ctx.field_a_plus_b_times_c = (ctx.field_a + ctx.field_b) * params.param_c",
"params": {
"param_c": 10
}
}
}
--------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@

package org.elasticsearch.ingest.common;

import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.common.Strings;
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.script.CompiledScript;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
Expand All @@ -35,6 +33,7 @@
import static java.util.Collections.emptyMap;
import static org.elasticsearch.common.Strings.hasLength;
import static org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException;
import static org.elasticsearch.ingest.ConfigurationUtils.readOptionalMap;
import static org.elasticsearch.ingest.ConfigurationUtils.readOptionalStringProperty;
import static org.elasticsearch.ingest.ConfigurationUtils.readStringProperty;
import static org.elasticsearch.script.ScriptService.ScriptType.FILE;
Expand All @@ -60,10 +59,8 @@ public final class ScriptProcessor extends AbstractProcessor {

@Override
public void execute(IngestDocument document) {
Map<String, Object> vars = new HashMap<>();
vars.put("ctx", document.getSourceAndMetadata());
CompiledScript compiledScript = scriptService.compile(script, ScriptContext.Standard.INGEST, emptyMap());
ExecutableScript executableScript = scriptService.executable(compiledScript, vars);
ExecutableScript executableScript = scriptService.executable(script, ScriptContext.Standard.INGEST, emptyMap());
executableScript.setNextVar("ctx", document.getSourceAndMetadata());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks new. I like this update!

executableScript.run();
}

Expand All @@ -87,6 +84,7 @@ public ScriptProcessor create(Map<String, Processor.Factory> registry, String pr
String inline = readOptionalStringProperty(TYPE, processorTag, config, "inline");
String file = readOptionalStringProperty(TYPE, processorTag, config, "file");
String id = readOptionalStringProperty(TYPE, processorTag, config, "id");
Map<String, ?> params = readOptionalMap(TYPE, processorTag, config, "params");

boolean containsNoScript = !hasLength(file) && !hasLength(id) && !hasLength(inline);
if (containsNoScript) {
Expand All @@ -99,13 +97,17 @@ public ScriptProcessor create(Map<String, Processor.Factory> registry, String pr
throw newConfigurationException(TYPE, processorTag, null, "Only one of [file], [id], or [inline] may be configured");
}

if(params == null) {
params = emptyMap();
}

final Script script;
if (Strings.hasLength(file)) {
script = new Script(file, FILE, lang, emptyMap());
script = new Script(file, FILE, lang, params);
} else if (Strings.hasLength(inline)) {
script = new Script(inline, INLINE, lang, emptyMap());
script = new Script(inline, INLINE, lang, params);
} else if (Strings.hasLength(id)) {
script = new Script(id, STORED, lang, emptyMap());
script = new Script(id, STORED, lang, params);
} else {
throw newConfigurationException(TYPE, processorTag, null, "Could not initialize script");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ public void testScripting() throws Exception {
int randomBytesTotal = randomBytesIn + randomBytesOut;

ScriptService scriptService = mock(ScriptService.class);
CompiledScript compiledScript = mock(CompiledScript.class);
Script script = new Script("_script");
when(scriptService.compile(any(), any(), any())).thenReturn(compiledScript);
ExecutableScript executableScript = mock(ExecutableScript.class);
when(scriptService.executable(any(), any())).thenReturn(executableScript);
when(scriptService.executable(any(), any(), any())).thenReturn(executableScript);

Map<String, Object> document = new HashMap<>();
document.put("bytes_in", randomInt());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"Test script processor with inline script":
"Test script processor with inline script and params":
- do:
ingest.put_pipeline:
id: "my_pipeline"
Expand All @@ -10,7 +10,10 @@
{
"script" : {
"lang" : "painless",
"inline": "ctx.bytes_total = ctx.bytes_in + ctx.bytes_out"
"inline": "ctx.bytes_total = (ctx.bytes_in + ctx.bytes_out) * params.factor",
"params": {
"factor": 10
}
}
}
]
Expand All @@ -32,7 +35,7 @@
id: 1
- match: { _source.bytes_in: 1234 }
- match: { _source.bytes_out: 4321 }
- match: { _source.bytes_total: 5555 }
- match: { _source.bytes_total: 55550 }

---
"Test script processor with file script":
Expand Down