Search in sources :

Example 1 with IngestScript

use of org.opensearch.script.IngestScript in project OpenSearch by opensearch-project.

the class ScriptProcessorTests method setupScripting.

@Before
public void setupScripting() {
    String scriptName = "script";
    scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> {
        Integer bytesIn = (Integer) ctx.get("bytes_in");
        Integer bytesOut = (Integer) ctx.get("bytes_out");
        ctx.put("bytes_total", bytesIn + bytesOut);
        return null;
    }), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap());
    ingestScript = scriptService.compile(script, IngestScript.CONTEXT).newInstance(script.getParams());
}
Also used : ScriptService(org.opensearch.script.ScriptService) Script(org.opensearch.script.Script) IngestScript(org.opensearch.script.IngestScript) HashMap(java.util.HashMap) MockScriptEngine(org.opensearch.script.MockScriptEngine) Before(org.junit.Before)

Example 2 with IngestScript

use of org.opensearch.script.IngestScript in project OpenSearch by opensearch-project.

the class ScriptProcessor method execute.

/**
 * Executes the script with the Ingest document in context.
 *
 * @param document The Ingest document passed into the script context under the "ctx" object.
 */
@Override
public IngestDocument execute(IngestDocument document) {
    final IngestScript ingestScript;
    if (precompiledIngestScript == null) {
        IngestScript.Factory factory = scriptService.compile(script, IngestScript.CONTEXT);
        ingestScript = factory.newInstance(script.getParams());
    } else {
        ingestScript = precompiledIngestScript;
    }
    ingestScript.execute(new DynamicMap(document.getSourceAndMetadata(), PARAMS_FUNCTIONS));
    CollectionUtils.ensureNoSelfReferences(document.getSourceAndMetadata(), "ingest script");
    return document;
}
Also used : IngestScript(org.opensearch.script.IngestScript) DynamicMap(org.opensearch.script.DynamicMap)

Aggregations

IngestScript (org.opensearch.script.IngestScript)2 HashMap (java.util.HashMap)1 Before (org.junit.Before)1 DynamicMap (org.opensearch.script.DynamicMap)1 MockScriptEngine (org.opensearch.script.MockScriptEngine)1 Script (org.opensearch.script.Script)1 ScriptService (org.opensearch.script.ScriptService)1