Search in sources :

Example 16 with ScriptService

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

the class InternalAggregationTestCase method testReduceRandom.

public void testReduceRandom() throws IOException {
    String name = randomAlphaOfLength(5);
    int size = between(1, 200);
    List<T> inputs = randomResultsToReduce(name, size);
    assertThat(inputs, hasSize(size));
    List<InternalAggregation> toReduce = new ArrayList<>();
    toReduce.addAll(inputs);
    // Sort aggs so that unmapped come last. This mimicks the behavior of InternalAggregations.reduce()
    inputs.sort(INTERNAL_AGG_COMPARATOR);
    ScriptService mockScriptService = mockScriptService();
    MockBigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
    if (randomBoolean() && toReduce.size() > 1) {
        // sometimes do a partial reduce
        Collections.shuffle(toReduce, random());
        int r = randomIntBetween(1, inputs.size());
        List<InternalAggregation> toPartialReduce = toReduce.subList(0, r);
        // Sort aggs so that unmapped come last. This mimicks the behavior of InternalAggregations.reduce()
        toPartialReduce.sort(INTERNAL_AGG_COMPARATOR);
        InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forPartialReduction(bigArrays, mockScriptService, () -> PipelineAggregator.PipelineTree.EMPTY);
        @SuppressWarnings("unchecked") T reduced = (T) toPartialReduce.get(0).reduce(toPartialReduce, context);
        int initialBucketCount = 0;
        for (InternalAggregation internalAggregation : toPartialReduce) {
            initialBucketCount += countInnerBucket(internalAggregation);
        }
        int reducedBucketCount = countInnerBucket(reduced);
        // check that non final reduction never adds buckets
        assertThat(reducedBucketCount, lessThanOrEqualTo(initialBucketCount));
        /*
             * Sometimes serializing and deserializing the partially reduced
             * result to simulate the compaction that we attempt after a
             * partial reduce. And to simulate cross cluster search.
             */
        if (randomBoolean()) {
            reduced = copyNamedWriteable(reduced, getNamedWriteableRegistry(), categoryClass());
        }
        toReduce = new ArrayList<>(toReduce.subList(r, inputs.size()));
        toReduce.add(reduced);
    }
    MultiBucketConsumer bucketConsumer = new MultiBucketConsumer(DEFAULT_MAX_BUCKETS, new NoneCircuitBreakerService().getBreaker(CircuitBreaker.REQUEST));
    InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forFinalReduction(bigArrays, mockScriptService, bucketConsumer, PipelineTree.EMPTY);
    @SuppressWarnings("unchecked") T reduced = (T) inputs.get(0).reduce(toReduce, context);
    doAssertReducedMultiBucketConsumer(reduced, bucketConsumer);
    assertReduced(reduced, inputs);
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ArrayList(java.util.ArrayList) ReduceContext(org.opensearch.search.aggregations.InternalAggregation.ReduceContext) MockBigArrays(org.opensearch.common.util.MockBigArrays) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) ScriptService(org.opensearch.script.ScriptService) MultiBucketConsumer(org.opensearch.search.aggregations.MultiBucketConsumerService.MultiBucketConsumer) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 17 with ScriptService

use of org.opensearch.script.ScriptService 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) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) Matchers.equalTo(org.hamcrest.Matchers.equalTo) 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 18 with ScriptService

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

the class PipelineProcessorTests method createIngestService.

static IngestService createIngestService() {
    IngestService ingestService = mock(IngestService.class);
    ScriptService scriptService = mock(ScriptService.class);
    when(ingestService.getScriptService()).thenReturn(scriptService);
    return ingestService;
}
Also used : ScriptService(org.opensearch.script.ScriptService)

Example 19 with ScriptService

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

the class TrackingResultProcessorTests method testActualCompoundProcessorWithFalseConditional.

public void testActualCompoundProcessorWithFalseConditional() throws Exception {
    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 -> false), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    CompoundProcessor compoundProcessor = 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, new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key2, randomInt());
    })), new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key3, randomInt());
    }));
    CompoundProcessor trackingProcessor = decorate(compoundProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(compoundProcessor.getType(), compoundProcessor.getTag(), compoundProcessor.getDescription(), ingestDocument, null);
    assertThat(resultList.size(), equalTo(3));
    assertTrue(resultList.get(0).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(0).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(0).getIngestDocument().hasField(key3));
    assertThat(resultList.get(1).getConditionalWithResult().v1(), equalTo(scriptName));
    assertThat(resultList.get(1).getConditionalWithResult().v2(), is(Boolean.FALSE));
    assertTrue(resultList.get(2).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(2).getIngestDocument().hasField(key2));
    assertTrue(resultList.get(2).getIngestDocument().hasField(key3));
    assertThat(resultList.get(2).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(2).getFailure(), nullValue());
    assertThat(resultList.get(2).getProcessorTag(), nullValue());
}
Also used : ScriptService(org.opensearch.script.ScriptService) 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) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) MockScriptEngine(org.opensearch.script.MockScriptEngine) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 20 with ScriptService

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

the class TrackingResultProcessorTests method testActualPipelineProcessorWithFalseConditional.

public void testActualPipelineProcessorWithFalseConditional() 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 -> false), 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(), null, 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(), null, 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.never()).getPipeline(pipelineId2);
    assertThat(resultList.size(), equalTo(4));
    assertNull(resultList.get(0).getConditionalWithResult());
    assertThat(resultList.get(0).getType(), equalTo("pipeline"));
    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.FALSE));
    assertThat(resultList.get(3).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(3).getFailure(), nullValue());
    assertThat(resultList.get(3).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)

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