Search in sources :

Example 1 with ScriptService

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());
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ArrayList(java.util.ArrayList) MockBigArrays(org.opensearch.common.util.MockBigArrays) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) ScriptService(org.opensearch.script.ScriptService) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 2 with ScriptService

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");
}
Also used : ScriptService(org.opensearch.script.ScriptService) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with ScriptService

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));
}
Also used : ScriptService(org.opensearch.script.ScriptService) Response(org.opensearch.painless.action.PainlessExecuteAction.Response) Script(org.opensearch.script.Script) BytesArray(org.opensearch.common.bytes.BytesArray) IndexService(org.opensearch.index.IndexService) Request(org.opensearch.painless.action.PainlessExecuteAction.Request)

Example 4 with ScriptService

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]"));
}
Also used : ScriptService(org.opensearch.script.ScriptService) Response(org.opensearch.painless.action.PainlessExecuteAction.Response) Script(org.opensearch.script.Script) HashMap(java.util.HashMap) Request(org.opensearch.painless.action.PainlessExecuteAction.Request) IOException(java.io.IOException) ScriptException(org.opensearch.script.ScriptException)

Example 5 with ScriptService

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);
}
Also used : Script(org.opensearch.script.Script) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) IntervalQuery(org.apache.lucene.queries.intervals.IntervalQuery) ScriptContext(org.opensearch.script.ScriptContext) ScriptService(org.opensearch.script.ScriptService)

Aggregations

ScriptService (org.opensearch.script.ScriptService)63 MockScriptEngine (org.opensearch.script.MockScriptEngine)31 Script (org.opensearch.script.Script)29 HashMap (java.util.HashMap)28 Settings (org.opensearch.common.settings.Settings)27 Map (java.util.Map)23 ArrayList (java.util.ArrayList)22 Collections (java.util.Collections)21 ScriptModule (org.opensearch.script.ScriptModule)21 ScriptEngine (org.opensearch.script.ScriptEngine)20 ScriptType (org.opensearch.script.ScriptType)19 List (java.util.List)17 Consumer (java.util.function.Consumer)13 Function (java.util.function.Function)13 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)13 Mockito.when (org.mockito.Mockito.when)12 IOException (java.io.IOException)11 Arrays (java.util.Arrays)11 Before (org.junit.Before)11 Matchers.containsString (org.hamcrest.Matchers.containsString)10