Search in sources :

Example 6 with ScriptEngine

use of org.opensearch.script.ScriptEngine in project OpenSearch by opensearch-project.

the class BucketScriptAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, Collections.singletonMap(SCRIPT_NAME, script -> script.get("the_avg")), Collections.emptyMap());
    Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
    return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Also used : Query(org.apache.lucene.search.Query) ScriptModule(org.opensearch.script.ScriptModule) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) ScriptEngine(org.opensearch.script.ScriptEngine) AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) CheckedConsumer(org.opensearch.common.CheckedConsumer) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ScriptType(org.opensearch.script.ScriptType) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) MockScriptEngine(org.opensearch.script.MockScriptEngine) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) ScriptService(org.opensearch.script.ScriptService) ValueType(org.opensearch.search.aggregations.support.ValueType) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) InternalFilters(org.opensearch.search.aggregations.bucket.filter.InternalFilters) Consumer(java.util.function.Consumer) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) Collections(java.util.Collections) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) ScriptService(org.opensearch.script.ScriptService) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 7 with ScriptEngine

use of org.opensearch.script.ScriptEngine in project OpenSearch by opensearch-project.

the class MovFnAggrgatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, Collections.singletonMap("test", script -> MovingFunctions.max((double[]) script.get("_values"))), Collections.emptyMap());
    Map<String, ScriptEngine> engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine);
    return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Also used : Query(org.apache.lucene.search.Query) InternalDateHistogram(org.opensearch.search.aggregations.bucket.histogram.InternalDateHistogram) Arrays(java.util.Arrays) LongPoint(org.apache.lucene.document.LongPoint) ScriptModule(org.opensearch.script.ScriptModule) ScriptEngine(org.opensearch.script.ScriptEngine) AvgAggregationBuilder(org.opensearch.search.aggregations.metrics.AvgAggregationBuilder) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) DateHistogramAggregationBuilder(org.opensearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder) DateHistogramInterval(org.opensearch.search.aggregations.bucket.histogram.DateHistogramInterval) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ScriptType(org.opensearch.script.ScriptType) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptService(org.opensearch.script.ScriptService) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) DateFormatters(org.opensearch.common.time.DateFormatters) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) Collections(java.util.Collections) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) ScriptService(org.opensearch.script.ScriptService) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 8 with ScriptEngine

use of org.opensearch.script.ScriptEngine 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 9 with ScriptEngine

use of org.opensearch.script.ScriptEngine in project OpenSearch by opensearch-project.

the class AbstractSortTestCase method init.

@BeforeClass
public static void init() {
    Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    Map<String, Function<Map<String, Object>, Object>> scripts = Collections.singletonMap(MOCK_SCRIPT_NAME, p -> null);
    ScriptEngine engine = new MockScriptEngine(MockScriptEngine.NAME, scripts, Collections.emptyMap());
    scriptService = new ScriptService(baseSettings, Collections.singletonMap(engine.getType(), engine), ScriptModule.CORE_CONTEXTS);
    SearchModule searchModule = new SearchModule(Settings.EMPTY, emptyList());
    namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
    xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
Also used : ScriptService(org.opensearch.script.ScriptService) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) TriFunction(org.opensearch.common.TriFunction) Function(java.util.function.Function) MockScriptEngine(org.opensearch.script.MockScriptEngine) SearchModule(org.opensearch.search.SearchModule) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine) BeforeClass(org.junit.BeforeClass)

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

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