Search in sources :

Example 26 with ScriptService

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);
}
Also used : ScriptService(org.opensearch.script.ScriptService) Function(java.util.function.Function) HashMap(java.util.HashMap) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 27 with ScriptService

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);
}
Also used : ScriptService(org.opensearch.script.ScriptService) Function(java.util.function.Function) HashMap(java.util.HashMap) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 28 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)

Example 29 with ScriptService

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);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) ScriptModule(org.opensearch.script.ScriptModule) ScriptEngine(org.opensearch.script.ScriptEngine) BiFunction(java.util.function.BiFunction) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericUtils(org.apache.lucene.util.NumericUtils) ScriptType(org.opensearch.script.ScriptType) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) Collections.singleton(java.util.Collections.singleton) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) MockScriptEngine(org.opensearch.script.MockScriptEngine) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) QueryBuilders.termQuery(org.opensearch.index.query.QueryBuilders.termQuery) ScriptService(org.opensearch.script.ScriptService) ValueType(org.opensearch.search.aggregations.support.ValueType) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) List(java.util.List) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) BucketOrder(org.opensearch.search.aggregations.BucketOrder) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) IntPoint(org.apache.lucene.document.IntPoint) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) IOException(java.io.IOException) DocValuesFieldExistsQuery(org.apache.lucene.search.DocValuesFieldExistsQuery) Consumer(java.util.function.Consumer) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) TermsAggregator(org.opensearch.search.aggregations.bucket.terms.TermsAggregator) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) Collections(java.util.Collections) HashMap(java.util.HashMap) IntPoint(org.apache.lucene.document.IntPoint) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptService(org.opensearch.script.ScriptService) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) MockScriptEngine(org.opensearch.script.MockScriptEngine) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 30 with ScriptService

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));
}
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) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder)

Aggregations

ScriptService (org.opensearch.script.ScriptService)64 MockScriptEngine (org.opensearch.script.MockScriptEngine)32 Script (org.opensearch.script.Script)30 HashMap (java.util.HashMap)29 Settings (org.opensearch.common.settings.Settings)28 Map (java.util.Map)24 ArrayList (java.util.ArrayList)23 Collections (java.util.Collections)22 ScriptModule (org.opensearch.script.ScriptModule)22 ScriptEngine (org.opensearch.script.ScriptEngine)21 ScriptType (org.opensearch.script.ScriptType)20 List (java.util.List)18 Consumer (java.util.function.Consumer)14 Function (java.util.function.Function)14 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)13 IOException (java.io.IOException)12 Mockito.when (org.mockito.Mockito.when)12 Arrays (java.util.Arrays)11 Before (org.junit.Before)11 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)10