Search in sources :

Example 16 with MockScriptEngine

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

the class TrackingResultProcessorTests method testActualPipelineProcessorWithTrueConditional.

public void testActualPipelineProcessorWithTrueConditional() throws Exception {
    String pipelineId1 = "pipeline1";
    String pipelineId2 = "pipeline2";
    IngestService ingestService = createIngestService();
    Map<String, Object> pipelineConfig0 = new HashMap<>();
    pipelineConfig0.put("name", pipelineId1);
    Map<String, Object> pipelineConfig1 = new HashMap<>();
    pipelineConfig1.put("name", pipelineId1);
    Map<String, Object> pipelineConfig2 = new HashMap<>();
    pipelineConfig2.put("name", pipelineId2);
    PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService);
    String key1 = randomAlphaOfLength(10);
    String key2 = randomAlphaOfLength(10);
    String key3 = randomAlphaOfLength(10);
    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));
    Pipeline pipeline1 = new Pipeline(pipelineId1, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key1, randomInt());
    }), new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, factory.create(Collections.emptyMap(), "pipeline1", null, pipelineConfig2)), new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key3, randomInt());
    })));
    Pipeline pipeline2 = new Pipeline(pipelineId2, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key2, randomInt());
    })));
    when(ingestService.getPipeline(pipelineId1)).thenReturn(pipeline1);
    when(ingestService.getPipeline(pipelineId2)).thenReturn(pipeline2);
    PipelineProcessor pipelineProcessor = factory.create(Collections.emptyMap(), "pipeline0", null, pipelineConfig0);
    CompoundProcessor actualProcessor = new CompoundProcessor(pipelineProcessor);
    CompoundProcessor trackingProcessor = decorate(actualProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), ingestDocument, null);
    expectedResult.getIngestDocument().getIngestMetadata().put("pipeline", pipelineId1);
    verify(ingestService, Mockito.atLeast(1)).getPipeline(pipelineId1);
    verify(ingestService, Mockito.atLeast(1)).getPipeline(pipelineId2);
    assertThat(resultList.size(), equalTo(5));
    assertNull(resultList.get(0).getConditionalWithResult());
    assertThat(resultList.get(0).getType(), equalTo("pipeline"));
    assertThat(resultList.get(0).getProcessorTag(), equalTo("pipeline0"));
    assertTrue(resultList.get(1).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(1).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(1).getIngestDocument().hasField(key3));
    assertThat(resultList.get(2).getConditionalWithResult().v1(), equalTo(scriptName));
    assertThat(resultList.get(2).getConditionalWithResult().v2(), is(Boolean.TRUE));
    assertThat(resultList.get(2).getType(), equalTo("pipeline"));
    assertThat(resultList.get(2).getProcessorTag(), equalTo("pipeline1"));
    assertTrue(resultList.get(3).getIngestDocument().hasField(key1));
    assertTrue(resultList.get(3).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(3).getIngestDocument().hasField(key3));
    assertThat(resultList.get(4).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(4).getFailure(), nullValue());
    assertThat(resultList.get(4).getProcessorTag(), nullValue());
}
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) PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ScriptService(org.opensearch.script.ScriptService) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 17 with MockScriptEngine

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

the class NestedAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
    scripts.put(INVERSE_SCRIPT, vars -> -((Number) vars.get("_value")).doubleValue());
    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 18 with MockScriptEngine

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

the class TermsAggregatorTests method getMockScriptService.

@Override
protected ScriptService getMockScriptService() {
    Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
    Map<String, Function<Map<String, Object>, Object>> nonDeterministicScripts = new HashMap<>();
    scripts.put(STRING_SCRIPT_NAME, value -> STRING_SCRIPT_OUTPUT);
    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 : ScriptService(org.opensearch.script.ScriptService) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) HashMap(java.util.HashMap) MockScriptEngine(org.opensearch.script.MockScriptEngine) MockScriptEngine(org.opensearch.script.MockScriptEngine) ScriptEngine(org.opensearch.script.ScriptEngine)

Example 19 with MockScriptEngine

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

the class MultiTermsAggregatorTests 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_NAME);
        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 : Query(org.apache.lucene.search.Query) ScriptModule(org.opensearch.script.ScriptModule) ScriptEngine(org.opensearch.script.ScriptEngine) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) Collections.singletonList(java.util.Collections.singletonList) ScriptType(org.opensearch.script.ScriptType) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) GeoPointFieldMapper(org.opensearch.index.mapper.GeoPointFieldMapper) Matchers.closeTo(org.hamcrest.Matchers.closeTo) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) MockScriptEngine(org.opensearch.script.MockScriptEngine) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) DateFormatter(org.opensearch.common.time.DateFormatter) ScriptService(org.opensearch.script.ScriptService) ValueType(org.opensearch.search.aggregations.support.ValueType) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IpFieldMapper(org.opensearch.index.mapper.IpFieldMapper) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) InetAddressPoint(org.apache.lucene.document.InetAddressPoint) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) Script(org.opensearch.script.Script) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Matchers.contains(org.hamcrest.Matchers.contains) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BucketOrder(org.opensearch.search.aggregations.BucketOrder) InetAddresses(org.opensearch.common.network.InetAddresses) StringField(org.apache.lucene.document.StringField) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) ArrayList(java.util.ArrayList) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections.singletonMap(java.util.Collections.singletonMap) CoreValuesSourceType(org.opensearch.search.aggregations.support.CoreValuesSourceType) ValuesSourceType(org.opensearch.search.aggregations.support.ValuesSourceType) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) Collections.emptyMap(java.util.Collections.emptyMap) AggregatorTestCase(org.opensearch.search.aggregations.AggregatorTestCase) FloatDocValuesField(org.apache.lucene.document.FloatDocValuesField) IOException(java.io.IOException) MultiTermsValuesSourceConfig(org.opensearch.search.aggregations.support.MultiTermsValuesSourceConfig) MaxAggregationBuilder(org.opensearch.search.aggregations.metrics.MaxAggregationBuilder) Consumer(java.util.function.Consumer) TermQuery(org.apache.lucene.search.TermQuery) Collectors.toList(java.util.stream.Collectors.toList) BooleanFieldMapper(org.opensearch.index.mapper.BooleanFieldMapper) Field(org.apache.lucene.document.Field) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) Collections(java.util.Collections) 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 20 with MockScriptEngine

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

the class MaxAggregatorTests 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(SCRIPT_NAME, script -> SCRIPT_VALUE);
    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 -> MaxAggregatorTests.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) NoMergePolicy(org.apache.lucene.index.NoMergePolicy) Arrays(java.util.Arrays) ScriptModule(org.opensearch.script.ScriptModule) MultiBucketCollector(org.opensearch.search.aggregations.MultiBucketCollector) ScriptEngine(org.opensearch.script.ScriptEngine) BiFunction(java.util.function.BiFunction) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) ScriptType(org.opensearch.script.ScriptType) DoublePoint(org.apache.lucene.document.DoublePoint) Document(org.apache.lucene.document.Document) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) BucketCollector(org.opensearch.search.aggregations.BucketCollector) Collections.singleton(java.util.Collections.singleton) Directory(org.apache.lucene.store.Directory) Map(java.util.Map) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MockScriptEngine(org.opensearch.script.MockScriptEngine) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) 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) Collections.emptyList(java.util.Collections.emptyList) DirectoryReader(org.apache.lucene.index.DirectoryReader) Script(org.opensearch.script.Script) Global(org.opensearch.search.aggregations.bucket.global.Global) Settings(org.opensearch.common.settings.Settings) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) Tuple(org.opensearch.common.collect.Tuple) PointValues(org.apache.lucene.index.PointValues) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) BucketOrder(org.opensearch.search.aggregations.BucketOrder) LongPoint(org.apache.lucene.document.LongPoint) StringField(org.apache.lucene.document.StringField) CheckedConsumer(org.opensearch.common.CheckedConsumer) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) 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) Bits(org.apache.lucene.util.Bits) FloatPoint(org.apache.lucene.document.FloatPoint) 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) Field(org.apache.lucene.document.Field) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) LeafDocLookup(org.opensearch.search.lookup.LeafDocLookup) Aggregator(org.opensearch.search.aggregations.Aggregator) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) 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) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

MockScriptEngine (org.opensearch.script.MockScriptEngine)30 ScriptService (org.opensearch.script.ScriptService)30 HashMap (java.util.HashMap)22 Settings (org.opensearch.common.settings.Settings)21 Map (java.util.Map)20 Script (org.opensearch.script.Script)20 ScriptEngine (org.opensearch.script.ScriptEngine)19 ScriptModule (org.opensearch.script.ScriptModule)19 ScriptType (org.opensearch.script.ScriptType)19 Collections (java.util.Collections)17 List (java.util.List)16 ArrayList (java.util.ArrayList)14 Function (java.util.function.Function)13 Consumer (java.util.function.Consumer)12 Arrays (java.util.Arrays)11 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)10 IOException (java.io.IOException)9 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)9 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)9 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)9