use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class TrackingResultProcessorTests method testActualPipelineProcessorWithTrueConditional.
public void testActualPipelineProcessorWithTrueConditional() throws Exception {
String pipelineId1 = "pipeline1";
String pipelineId2 = "pipeline2";
IngestService ingestService = createIngestService();
Map<String, Object> pipelineConfig0 = new HashMap<>();
pipelineConfig0.put("name", pipelineId1);
Map<String, Object> pipelineConfig1 = new HashMap<>();
pipelineConfig1.put("name", pipelineId1);
Map<String, Object> pipelineConfig2 = new HashMap<>();
pipelineConfig2.put("name", pipelineId2);
PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService);
String key1 = randomAlphaOfLength(10);
String key2 = randomAlphaOfLength(10);
String key3 = randomAlphaOfLength(10);
String scriptName = "conditionalScript";
ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> true), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
Pipeline pipeline1 = new Pipeline(pipelineId1, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
ingestDocument.setFieldValue(key1, randomInt());
}), new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, factory.create(Collections.emptyMap(), "pipeline1", null, pipelineConfig2)), new TestProcessor(ingestDocument -> {
ingestDocument.setFieldValue(key3, randomInt());
})));
Pipeline pipeline2 = new Pipeline(pipelineId2, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
ingestDocument.setFieldValue(key2, randomInt());
})));
when(ingestService.getPipeline(pipelineId1)).thenReturn(pipeline1);
when(ingestService.getPipeline(pipelineId2)).thenReturn(pipeline2);
PipelineProcessor pipelineProcessor = factory.create(Collections.emptyMap(), "pipeline0", null, pipelineConfig0);
CompoundProcessor actualProcessor = new CompoundProcessor(pipelineProcessor);
CompoundProcessor trackingProcessor = decorate(actualProcessor, null, resultList);
trackingProcessor.execute(ingestDocument, (result, e) -> {
});
SimulateProcessorResult expectedResult = new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), ingestDocument, null);
expectedResult.getIngestDocument().getIngestMetadata().put("pipeline", pipelineId1);
verify(ingestService, Mockito.atLeast(1)).getPipeline(pipelineId1);
verify(ingestService, Mockito.atLeast(1)).getPipeline(pipelineId2);
assertThat(resultList.size(), equalTo(5));
assertNull(resultList.get(0).getConditionalWithResult());
assertThat(resultList.get(0).getType(), equalTo("pipeline"));
assertThat(resultList.get(0).getProcessorTag(), equalTo("pipeline0"));
assertTrue(resultList.get(1).getIngestDocument().hasField(key1));
assertFalse(resultList.get(1).getIngestDocument().hasField(key2));
assertFalse(resultList.get(1).getIngestDocument().hasField(key3));
assertThat(resultList.get(2).getConditionalWithResult().v1(), equalTo(scriptName));
assertThat(resultList.get(2).getConditionalWithResult().v2(), is(Boolean.TRUE));
assertThat(resultList.get(2).getType(), equalTo("pipeline"));
assertThat(resultList.get(2).getProcessorTag(), equalTo("pipeline1"));
assertTrue(resultList.get(3).getIngestDocument().hasField(key1));
assertTrue(resultList.get(3).getIngestDocument().hasField(key2));
assertFalse(resultList.get(3).getIngestDocument().hasField(key3));
assertThat(resultList.get(4).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
assertThat(resultList.get(4).getFailure(), nullValue());
assertThat(resultList.get(4).getProcessorTag(), nullValue());
}
use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class NestedAggregatorTests method getMockScriptService.
@Override
protected ScriptService getMockScriptService() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
scripts.put(INVERSE_SCRIPT, vars -> -((Number) vars.get("_value")).doubleValue());
MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, scripts, Collections.emptyMap());
Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class TermsAggregatorTests method getMockScriptService.
@Override
protected ScriptService getMockScriptService() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
Map<String, Function<Map<String, Object>, Object>> nonDeterministicScripts = new HashMap<>();
scripts.put(STRING_SCRIPT_NAME, value -> STRING_SCRIPT_OUTPUT);
MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, scripts, nonDeterministicScripts, Collections.emptyMap());
Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class MultiTermsAggregatorTests method getMockScriptService.
@Override
protected ScriptService getMockScriptService() {
final Map<String, Function<Map<String, Object>, Object>> scripts = org.opensearch.common.collect.Map.of(VALUE_SCRIPT_NAME, vars -> ((Number) vars.get("_value")).doubleValue() + 1, FIELD_SCRIPT_NAME, vars -> {
final String fieldName = (String) vars.get(FIELD_NAME);
final LeafDocLookup lookup = (LeafDocLookup) vars.get("doc");
return lookup.get(fieldName).stream().map(value -> ((Number) value).longValue() + 1).collect(toList());
});
final MockScriptEngine engine = new MockScriptEngine(MockScriptEngine.NAME, scripts, emptyMap());
final Map<String, ScriptEngine> engines = singletonMap(engine.getType(), engine);
return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class MaxAggregatorTests method getMockScriptService.
@Override
protected ScriptService getMockScriptService() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
Function<Map<String, Object>, Integer> getInc = vars -> {
if (vars == null || vars.containsKey("inc") == false) {
return 0;
} else {
return ((Number) vars.get("inc")).intValue();
}
};
BiFunction<Map<String, Object>, String, Object> sum = (vars, fieldname) -> {
int inc = getInc.apply(vars);
LeafDocLookup docLookup = (LeafDocLookup) vars.get("doc");
List<Long> values = new ArrayList<>();
for (Object v : docLookup.get(fieldname)) {
values.add(((Number) v).longValue() + inc);
}
return values;
};
scripts.put(SCRIPT_NAME, script -> SCRIPT_VALUE);
scripts.put(SUM_FIELD_PARAMS_SCRIPT, vars -> {
String fieldname = (String) vars.get("field");
return sum.apply(vars, fieldname);
});
scripts.put(SUM_VALUES_FIELD_SCRIPT, vars -> sum.apply(vars, "values"));
scripts.put(VALUE_FIELD_SCRIPT, vars -> sum.apply(vars, "value"));
scripts.put(VALUE_SCRIPT, vars -> {
int inc = getInc.apply(vars);
return ((Number) vars.get("_value")).doubleValue() + inc;
});
Map<String, Function<Map<String, Object>, Object>> nonDeterministicScripts = new HashMap<>();
nonDeterministicScripts.put(RANDOM_SCRIPT, vars -> MaxAggregatorTests.randomDouble());
MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, scripts, nonDeterministicScripts, Collections.emptyMap());
Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Aggregations