Search in sources :

Example 1 with ScriptEngine

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\"}"));
}
Also used : ScriptEngine(org.opensearch.script.ScriptEngine) TemplateScript(org.opensearch.script.TemplateScript)

Example 2 with ScriptEngine

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);
}
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 3 with ScriptEngine

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);
}
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) 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) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) 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 4 with ScriptEngine

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);
}
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 5 with ScriptEngine

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);
}
Also used : ScriptService(org.opensearch.script.ScriptService) MockScriptEngine(org.opensearch.script.MockScriptEngine) QueryShardContext(org.opensearch.index.query.QueryShardContext) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Aggregations

ScriptEngine (org.opensearch.script.ScriptEngine)22 ScriptService (org.opensearch.script.ScriptService)19 MockScriptEngine (org.opensearch.script.MockScriptEngine)18 Function (java.util.function.Function)12 HashMap (java.util.HashMap)10 Settings (org.opensearch.common.settings.Settings)10 Map (java.util.Map)9 IOException (java.io.IOException)8 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)8 IndexReader (org.apache.lucene.index.IndexReader)8 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)8 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)8 Directory (org.apache.lucene.store.Directory)8 Script (org.opensearch.script.Script)8 ScriptModule (org.opensearch.script.ScriptModule)8 ScriptType (org.opensearch.script.ScriptType)8 List (java.util.List)7 Consumer (java.util.function.Consumer)7 IndexSearcher (org.apache.lucene.search.IndexSearcher)7 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)7