use of org.opensearch.script.ScriptEngine in project OpenSearch by opensearch-project.
the class CustomMustacheFactoryTests method testUrlEncoder.
public void testUrlEncoder() {
final ScriptEngine engine = new MustacheScriptEngine();
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, X_WWW_FORM_URLENCODED_MIME_TYPE);
TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);
TemplateScript executable = compiled.newInstance(singletonMap("value", "tilde~ AND date:[2016 FROM*]"));
assertThat(executable.execute(), equalTo("{\"field\": \"tilde%7E+AND+date%3A%5B2016+FROM*%5D\"}"));
}
use of org.opensearch.script.ScriptEngine in project OpenSearch by opensearch-project.
the class MissingAggregatorTests method getMockScriptService.
@Override
protected ScriptService getMockScriptService() {
final Map<String, Function<Map<String, Object>, Object>> deterministicScripts = new HashMap<>();
deterministicScripts.put(VALUE_SCRIPT_PARAMS, vars -> {
final double value = ((Number) vars.get("_value")).doubleValue();
final long inc = ((Number) vars.get("inc")).longValue();
return value + inc;
});
deterministicScripts.put(VALUE_SCRIPT, vars -> {
final double value = ((Number) vars.get("_value")).doubleValue();
return value + DEFAULT_INC_PARAM;
});
deterministicScripts.put(FIELD_SCRIPT_PARAMS, vars -> {
final String fieldName = (String) vars.get("field");
final long threshold = ((Number) vars.get("threshold")).longValue();
return threshold(fieldName, threshold, vars);
});
deterministicScripts.put(FIELD_SCRIPT, vars -> {
final String fieldName = (String) vars.get("field");
return threshold(fieldName, DEFAULT_THRESHOLD_PARAM, vars);
});
final MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, deterministicScripts, emptyMap(), emptyMap());
final Map<String, ScriptEngine> engines = singletonMap(scriptEngine.getType(), scriptEngine);
return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
use of org.opensearch.script.ScriptEngine in project OpenSearch by opensearch-project.
the class AvgAggregatorTests 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(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 -> AvgAggregatorTests.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);
}
use of org.opensearch.script.ScriptEngine in project OpenSearch by opensearch-project.
the class MedianAbsoluteDeviationAggregatorTests method getMockScriptService.
@Override
protected ScriptService getMockScriptService() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
scripts.put(VALUE_SCRIPT, vars -> ((Number) vars.get("_value")).doubleValue() + 1);
scripts.put(SINGLE_SCRIPT, vars -> 1);
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.ScriptEngine in project OpenSearch by opensearch-project.
the class ScriptedMetricAggregatorTests method queryShardContextMock.
/**
* We cannot use Mockito for mocking QueryShardContext in this case because
* script-related methods (e.g. QueryShardContext#getLazyExecutableScript)
* is final and cannot be mocked
*/
@Override
protected QueryShardContext queryShardContextMock(IndexSearcher searcher, MapperService mapperService, IndexSettings indexSettings, CircuitBreakerService circuitBreakerService, BigArrays bigArrays) {
MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, SCRIPTS, Collections.emptyMap());
Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
ScriptService scriptService = new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
return new QueryShardContext(0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, getIndexFieldDataLookup(mapperService, circuitBreakerService), mapperService, null, scriptService, xContentRegistry(), writableRegistry(), null, null, System::currentTimeMillis, null, null, () -> true, valuesSourceRegistry);
}
Aggregations