use of org.opensearch.script.ScriptService 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.ScriptService in project OpenSearch by opensearch-project.
the class ValueCountAggregatorTests method getMockScriptService.
@Override
protected ScriptService getMockScriptService() {
Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
scripts.put(STRING_VALUE_SCRIPT, vars -> (Double.valueOf((String) vars.get("_value")) + 1));
scripts.put(NUMBER_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.ScriptService in project OpenSearch by opensearch-project.
the class IntervalQueryBuilderTests method testScriptFilter.
public void testScriptFilter() throws IOException {
IntervalFilterScript.Factory factory = () -> new IntervalFilterScript() {
@Override
public boolean execute(Interval interval) {
return interval.getStart() > 3;
}
};
ScriptService scriptService = new ScriptService(Settings.EMPTY, Collections.emptyMap(), Collections.emptyMap()) {
@Override
@SuppressWarnings("unchecked")
public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
assertEquals(IntervalFilterScript.CONTEXT, context);
assertEquals(new Script("interval.start > 3"), script);
return (FactoryType) factory;
}
};
QueryShardContext baseContext = createShardContext();
QueryShardContext context = new QueryShardContext(baseContext.getShardId(), baseContext.getIndexSettings(), BigArrays.NON_RECYCLING_INSTANCE, null, null, baseContext.getMapperService(), null, scriptService, null, null, null, null, null, null, null, () -> true, null);
String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"match\" : { " + " \"query\" : \"term1\"," + " \"filter\" : { " + " \"script\" : { " + " \"source\" : \"interval.start > 3\" } } } } } }";
IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json);
Query q = builder.toQuery(context);
IntervalQuery expected = new IntervalQuery(TEXT_FIELD_NAME, new IntervalsSourceProvider.ScriptFilterSource(Intervals.term("term1"), "interval.start > 3", null));
assertEquals(expected, q);
}
use of org.opensearch.script.ScriptService 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.ScriptService in project OpenSearch by opensearch-project.
the class PainlessExecuteApiTests method testScoreExecutionContext.
public void testScoreExecutionContext() throws IOException {
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=text");
Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), new MatchQueryBuilder("text", "fox"));
contextSetup.setXContentType(XContentType.JSON);
Request request = new Request(new Script(ScriptType.INLINE, "painless", "Math.round((_score + (doc['rank'].value / params.max_rank)) * 100.0) / 100.0", singletonMap("max_rank", 5.0)), "score", contextSetup);
Response response = innerShardOperation(request, scriptService, indexService);
assertThat(response.getResult(), equalTo(0.93D));
}
Aggregations