Search in sources :

Example 21 with ScriptService

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

the class TrackingResultProcessorTests method testActualCompoundProcessorWithOnFailureAndTrueCondition.

public void testActualCompoundProcessorWithOnFailureAndTrueCondition() throws Exception {
    String scriptName = "conditionalScript";
    ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> true), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    RuntimeException exception = new RuntimeException("fail");
    TestProcessor failProcessor = new TestProcessor("fail", "test", null, exception);
    ConditionalProcessor conditionalProcessor = new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, failProcessor);
    TestProcessor onFailureProcessor = new TestProcessor("success", "test", null, ingestDocument -> {
    });
    CompoundProcessor actualProcessor = new CompoundProcessor(false, Arrays.asList(conditionalProcessor), Arrays.asList(onFailureProcessor));
    CompoundProcessor trackingProcessor = decorate(actualProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedFailResult = new SimulateProcessorResult(failProcessor.getType(), failProcessor.getTag(), failProcessor.getDescription(), ingestDocument, null);
    SimulateProcessorResult expectedSuccessResult = new SimulateProcessorResult(onFailureProcessor.getType(), onFailureProcessor.getTag(), onFailureProcessor.getDescription(), ingestDocument, null);
    assertThat(failProcessor.getInvokedCounter(), equalTo(1));
    assertThat(onFailureProcessor.getInvokedCounter(), equalTo(1));
    assertThat(resultList.size(), equalTo(2));
    assertThat(resultList.get(0).getIngestDocument(), nullValue());
    assertThat(resultList.get(0).getFailure(), equalTo(exception));
    assertThat(resultList.get(0).getProcessorTag(), equalTo(expectedFailResult.getProcessorTag()));
    assertThat(resultList.get(0).getConditionalWithResult().v1(), equalTo(scriptName));
    assertThat(resultList.get(0).getConditionalWithResult().v2(), is(Boolean.TRUE));
    Map<String, Object> metadata = resultList.get(1).getIngestDocument().getIngestMetadata();
    assertThat(metadata.get(ON_FAILURE_MESSAGE_FIELD), equalTo("fail"));
    assertThat(metadata.get(ON_FAILURE_PROCESSOR_TYPE_FIELD), equalTo("test"));
    assertThat(metadata.get(ON_FAILURE_PROCESSOR_TAG_FIELD), equalTo("fail"));
    assertThat(resultList.get(1).getFailure(), nullValue());
    assertThat(resultList.get(1).getProcessorTag(), equalTo(expectedSuccessResult.getProcessorTag()));
}
Also used : PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) Arrays(java.util.Arrays) ON_FAILURE_PROCESSOR_TAG_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TAG_FIELD) TrackingResultProcessor.decorate(org.opensearch.ingest.TrackingResultProcessor.decorate) ScriptModule(org.opensearch.script.ScriptModule) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) ArrayList(java.util.ArrayList) ScriptType(org.opensearch.script.ScriptType) ON_FAILURE_PROCESSOR_TYPE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TYPE_FIELD) ON_FAILURE_MESSAGE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_MESSAGE_FIELD) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MockScriptEngine(org.opensearch.script.MockScriptEngine) Before(org.junit.Before) ScriptService(org.opensearch.script.ScriptService) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Mockito(org.mockito.Mockito) List(java.util.List) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Script(org.opensearch.script.Script) Matchers.containsString(org.hamcrest.Matchers.containsString) ScriptService(org.opensearch.script.ScriptService) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 22 with ScriptService

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

the class ValueSourceTests method testScriptShouldCompile.

public void testScriptShouldCompile() {
    ScriptService scriptService = mock(ScriptService.class);
    when(scriptService.isLangSupported(anyString())).thenReturn(true);
    String propertyValue = "{{" + randomAlphaOfLength(10) + "}}";
    String compiledValue = randomAlphaOfLength(10);
    when(scriptService.compile(any(), any())).thenReturn(new TestTemplateService.MockTemplateScript.Factory(compiledValue));
    ValueSource result = ValueSource.wrap(propertyValue, scriptService);
    assertThat(result.copyAndResolve(Collections.emptyMap()), equalTo(compiledValue));
    verify(scriptService, times(1)).compile(any(), any());
}
Also used : ScriptService(org.opensearch.script.ScriptService) Mockito.anyString(org.mockito.Mockito.anyString)

Example 23 with ScriptService

use of org.opensearch.script.ScriptService 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) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) 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 24 with ScriptService

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

the class StatsAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    final Map<String, Function<Map<String, Object>, Object>> scripts = org.opensearch.common.collect.Map.of(VALUE_SCRIPT_NAME, vars -> ((Number) vars.get("_value")).doubleValue() + 1, FIELD_SCRIPT_NAME, vars -> {
        final String fieldName = (String) vars.get("field");
        final LeafDocLookup lookup = (LeafDocLookup) vars.get("doc");
        return lookup.get(fieldName).stream().map(value -> ((Number) value).longValue() + 1).collect(toList());
    });
    final MockScriptEngine engine = new MockScriptEngine(MockScriptEngine.NAME, scripts, emptyMap());
    final Map<String, ScriptEngine> engines = singletonMap(engine.getType(), engine);
    return new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS);
}
Also used : MultiReader(org.apache.lucene.index.MultiReader) Arrays(java.util.Arrays) ScriptModule(org.opensearch.script.ScriptModule) IndexableField(org.apache.lucene.index.IndexableField) ScriptEngine(org.opensearch.script.ScriptEngine) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) CheckedConsumer(org.opensearch.common.CheckedConsumer) NumericUtils(org.apache.lucene.util.NumericUtils) Function(java.util.function.Function) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) 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) BiConsumer(java.util.function.BiConsumer) Collections.singletonMap(java.util.Collections.singletonMap) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) MockScriptEngine(org.opensearch.script.MockScriptEngine) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) AggregationBuilders.stats(org.opensearch.search.aggregations.AggregationBuilders.stats) AggregationInspectionHelper(org.opensearch.search.aggregations.support.AggregationInspectionHelper) Collectors.toSet(java.util.stream.Collectors.toSet) ScriptService(org.opensearch.script.ScriptService) Collections.emptyMap(java.util.Collections.emptyMap) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) Collections.emptySet(java.util.Collections.emptySet) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Script(org.opensearch.script.Script) Collection(java.util.Collection) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) NumberType(org.opensearch.index.mapper.NumberFieldMapper.NumberType) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) ScriptService(org.opensearch.script.ScriptService) Function(java.util.function.Function) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptEngine(org.opensearch.script.ScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 25 with ScriptService

use of org.opensearch.script.ScriptService 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

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