use of org.opensearch.script.ScriptService in project OpenSearch by opensearch-project.
the class InternalMatrixStatsTests method testReduceRandom.
@Override
public void testReduceRandom() {
int numValues = 10000;
int numShards = randomIntBetween(1, 20);
int valuesPerShard = (int) Math.floor(numValues / numShards);
List<Double> aValues = new ArrayList<>();
List<Double> bValues = new ArrayList<>();
RunningStats runningStats = new RunningStats();
List<InternalAggregation> shardResults = new ArrayList<>();
int valuePerShardCounter = 0;
for (int i = 0; i < numValues; i++) {
double valueA = randomDouble();
aValues.add(valueA);
double valueB = randomDouble();
bValues.add(valueB);
runningStats.add(new String[] { "a", "b" }, new double[] { valueA, valueB });
if (++valuePerShardCounter == valuesPerShard) {
shardResults.add(new InternalMatrixStats("_name", 1L, runningStats, null, Collections.emptyMap()));
runningStats = new RunningStats();
valuePerShardCounter = 0;
}
}
if (valuePerShardCounter != 0) {
shardResults.add(new InternalMatrixStats("_name", 1L, runningStats, null, Collections.emptyMap()));
}
MultiPassStats multiPassStats = new MultiPassStats("a", "b");
multiPassStats.computeStats(aValues, bValues);
ScriptService mockScriptService = mockScriptService();
MockBigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forFinalReduction(bigArrays, mockScriptService, b -> {
}, PipelineTree.EMPTY);
InternalMatrixStats reduced = (InternalMatrixStats) shardResults.get(0).reduce(shardResults, context);
multiPassStats.assertNearlyEqual(reduced.getResults());
}
use of org.opensearch.script.ScriptService in project OpenSearch by opensearch-project.
the class ScriptProcessorFactoryTests method testInlineBackcompat.
public void testInlineBackcompat() throws Exception {
ScriptService mockedScriptService = mock(ScriptService.class);
when(mockedScriptService.compile(any(), any())).thenReturn(mock(IngestScript.Factory.class));
factory = new ScriptProcessor.Factory(mockedScriptService);
Map<String, Object> configMap = new HashMap<>();
configMap.put("inline", "code");
factory.create(null, randomAlphaOfLength(10), null, configMap);
assertWarnings("Deprecated field [inline] used, expected [source] instead");
}
use of org.opensearch.script.ScriptService in project OpenSearch by opensearch-project.
the class PainlessExecuteApiTests method testFilterExecutionContext.
public void testFilterExecutionContext() throws IOException {
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "field", "type=long");
Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 3}"), null);
contextSetup.setXContentType(XContentType.JSON);
Request request = new Request(new Script("doc['field'].value >= 3"), "filter", contextSetup);
Response response = innerShardOperation(request, scriptService, indexService);
assertThat(response.getResult(), equalTo(true));
contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 3}"), null);
contextSetup.setXContentType(XContentType.JSON);
request = new Request(new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", singletonMap("max", 3)), "filter", contextSetup);
response = innerShardOperation(request, scriptService, indexService);
assertThat(response.getResult(), equalTo(true));
contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 2}"), null);
contextSetup.setXContentType(XContentType.JSON);
request = new Request(new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", singletonMap("max", 3)), "filter", contextSetup);
response = innerShardOperation(request, scriptService, indexService);
assertThat(response.getResult(), equalTo(false));
}
use of org.opensearch.script.ScriptService in project OpenSearch by opensearch-project.
the class PainlessExecuteApiTests method testDefaults.
public void testDefaults() throws IOException {
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
Request request = new Request(new Script("100.0 / 1000.0"), null, null);
Response response = innerShardOperation(request, scriptService, null);
assertThat(response.getResult(), equalTo("0.1"));
Map<String, Object> params = new HashMap<>();
params.put("count", 100.0D);
params.put("total", 1000.0D);
request = new Request(new Script(ScriptType.INLINE, "painless", "params.count / params.total", params), null, null);
response = innerShardOperation(request, scriptService, null);
assertThat(response.getResult(), equalTo("0.1"));
Exception e = expectThrows(ScriptException.class, () -> {
Request r = new Request(new Script(ScriptType.INLINE, "painless", "params.count / params.total + doc['constant']", params), null, null);
innerShardOperation(r, scriptService, null);
});
assertThat(e.getCause().getMessage(), equalTo("cannot resolve symbol [doc]"));
}
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);
}
Aggregations