Search in sources :

Example 11 with ScriptService

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

the class InternalVariableWidthHistogramTests method testSingleShardReduceLong.

public void testSingleShardReduceLong() {
    InternalVariableWidthHistogram dummy_histogram = createEmptyTestInstance();
    List<InternalVariableWidthHistogram.Bucket> buckets = new ArrayList<>();
    for (long value : new long[] { 1, 2, 5, 10, 12, 200 }) {
        InternalVariableWidthHistogram.Bucket.BucketBounds bounds = new InternalVariableWidthHistogram.Bucket.BucketBounds(value, value + 1);
        InternalVariableWidthHistogram.Bucket bucket = new InternalVariableWidthHistogram.Bucket(value, bounds, 3, format, InternalAggregations.EMPTY);
        buckets.add(bucket);
    }
    InternalVariableWidthHistogram histogram = dummy_histogram.create(buckets);
    MockBigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
    ScriptService mockScriptService = mockScriptService();
    MultiBucketConsumerService.MultiBucketConsumer bucketConsumer = new MultiBucketConsumerService.MultiBucketConsumer(DEFAULT_MAX_BUCKETS, new NoneCircuitBreakerService().getBreaker(CircuitBreaker.REQUEST));
    InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forFinalReduction(bigArrays, mockScriptService, bucketConsumer, PipelineAggregator.PipelineTree.EMPTY);
    ArrayList<InternalAggregation> aggs = new ArrayList<>();
    aggs.add(histogram);
    List<InternalVariableWidthHistogram.Bucket> reduced_buckets = ((InternalVariableWidthHistogram) histogram.reduce(aggs, context)).getBuckets();
    // Final clusters should be [ (1,2,5), (10,12), 200) ]
    // Final centroids should be [ 3, 11, 200 ]
    // Final keys should be [ 1, 5, 200 ]
    double double_error = 1d / 10000d;
    assertEquals(1d, reduced_buckets.get(0).min(), double_error);
    assertEquals((8d / 3d), (double) reduced_buckets.get(0).getKey(), double_error);
    assertEquals(9, reduced_buckets.get(0).getDocCount());
    assertEquals(10d, reduced_buckets.get(1).min(), double_error);
    assertEquals(11d, (double) reduced_buckets.get(1).getKey(), double_error);
    assertEquals(6, reduced_buckets.get(1).getDocCount());
    assertEquals(200d, reduced_buckets.get(2).min(), double_error);
    assertEquals(200d, (double) reduced_buckets.get(2).getKey(), double_error);
    assertEquals(3, reduced_buckets.get(2).getDocCount());
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ArrayList(java.util.ArrayList) MockBigArrays(org.opensearch.common.util.MockBigArrays) ScriptService(org.opensearch.script.ScriptService) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) MultiBucketConsumerService(org.opensearch.search.aggregations.MultiBucketConsumerService) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 12 with ScriptService

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

the class InternalVariableWidthHistogramTests method testMultipleShardsReduce.

public void testMultipleShardsReduce() {
    InternalVariableWidthHistogram dummy_histogram = createEmptyTestInstance();
    List<InternalVariableWidthHistogram.Bucket> buckets1 = new ArrayList<>();
    for (long value : new long[] { 1, 5, 6, 10 }) {
        InternalVariableWidthHistogram.Bucket.BucketBounds bounds = new InternalVariableWidthHistogram.Bucket.BucketBounds(value, value + 1);
        InternalVariableWidthHistogram.Bucket bucket = new InternalVariableWidthHistogram.Bucket(value, bounds, 1, format, InternalAggregations.EMPTY);
        buckets1.add(bucket);
    }
    List<InternalVariableWidthHistogram.Bucket> buckets2 = new ArrayList<>();
    for (long value : new long[] { 2, 3, 6, 7 }) {
        InternalVariableWidthHistogram.Bucket.BucketBounds bounds = new InternalVariableWidthHistogram.Bucket.BucketBounds(value, value + 1);
        InternalVariableWidthHistogram.Bucket bucket = new InternalVariableWidthHistogram.Bucket(value, bounds, 1, format, InternalAggregations.EMPTY);
        buckets2.add(bucket);
    }
    List<InternalVariableWidthHistogram.Bucket> buckets3 = new ArrayList<>();
    for (long value : new long[] { 0, 2, 12 }) {
        InternalVariableWidthHistogram.Bucket.BucketBounds bounds = new InternalVariableWidthHistogram.Bucket.BucketBounds(value, value + 1);
        InternalVariableWidthHistogram.Bucket bucket = new InternalVariableWidthHistogram.Bucket(value, bounds, 1, format, InternalAggregations.EMPTY);
        buckets3.add(bucket);
    }
    InternalVariableWidthHistogram histogram1 = dummy_histogram.create(buckets1);
    InternalVariableWidthHistogram histogram2 = dummy_histogram.create(buckets2);
    InternalVariableWidthHistogram histogram3 = dummy_histogram.create(buckets3);
    MockBigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
    ScriptService mockScriptService = mockScriptService();
    MultiBucketConsumerService.MultiBucketConsumer bucketConsumer = new MultiBucketConsumerService.MultiBucketConsumer(DEFAULT_MAX_BUCKETS, new NoneCircuitBreakerService().getBreaker(CircuitBreaker.REQUEST));
    InternalAggregation.ReduceContext context = InternalAggregation.ReduceContext.forFinalReduction(bigArrays, mockScriptService, bucketConsumer, PipelineAggregator.PipelineTree.EMPTY);
    ArrayList<InternalAggregation> aggs = new ArrayList<>();
    aggs.add(histogram1);
    aggs.add(histogram2);
    aggs.add(histogram3);
    List<InternalVariableWidthHistogram.Bucket> reduced_buckets = ((InternalVariableWidthHistogram) histogram1.reduce(aggs, context)).getBuckets();
    // Final clusters should be [ (0, 1, 2, 2, 3), (5, 6, 6, 7), (10, 12) ]
    // Final centroids should be [ 2, 6, 11 ]
    // Final keys should be [ 1, 5, 10 ]
    double double_error = 1d / 10000d;
    assertEquals(0d, reduced_buckets.get(0).min(), double_error);
    assertEquals(1.6d, (double) reduced_buckets.get(0).getKey(), double_error);
    assertEquals(5, reduced_buckets.get(0).getDocCount());
    assertEquals(5d, reduced_buckets.get(1).min(), double_error);
    assertEquals(6d, (double) reduced_buckets.get(1).getKey(), double_error);
    assertEquals(4, reduced_buckets.get(1).getDocCount());
    assertEquals(10d, reduced_buckets.get(2).min(), double_error);
    assertEquals(11d, (double) reduced_buckets.get(2).getKey(), double_error);
    assertEquals(2, reduced_buckets.get(2).getDocCount());
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) ArrayList(java.util.ArrayList) MockBigArrays(org.opensearch.common.util.MockBigArrays) ScriptService(org.opensearch.script.ScriptService) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) MultiBucketConsumerService(org.opensearch.search.aggregations.MultiBucketConsumerService) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 13 with ScriptService

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

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

the class AbstractSuggestionBuilderTestCase method testBuild.

public void testBuild() throws IOException {
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        SB suggestionBuilder = randomTestBuilder();
        Settings indexSettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
        IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(new Index(randomAlphaOfLengthBetween(1, 10), "_na_"), indexSettings);
        MapperService mapperService = mock(MapperService.class);
        ScriptService scriptService = mock(ScriptService.class);
        boolean fieldTypeSearchAnalyzerSet = randomBoolean();
        MappedFieldType fieldType = mockFieldType(suggestionBuilder.field(), fieldTypeSearchAnalyzerSet);
        when(mapperService.searchAnalyzer()).thenReturn(new NamedAnalyzer("mapperServiceSearchAnalyzer", AnalyzerScope.INDEX, new SimpleAnalyzer()));
        when(mapperService.fieldType(any(String.class))).thenReturn(fieldType);
        when(mapperService.getNamedAnalyzer(any(String.class))).then(invocation -> new NamedAnalyzer((String) invocation.getArguments()[0], AnalyzerScope.INDEX, new SimpleAnalyzer()));
        when(scriptService.compile(any(Script.class), any())).then(invocation -> new TestTemplateService.MockTemplateScript.Factory(((Script) invocation.getArguments()[0]).getIdOrCode()));
        QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, mapperService, null, scriptService, xContentRegistry(), namedWriteableRegistry, null, null, System::currentTimeMillis, null, null, () -> true, null);
        SuggestionContext suggestionContext = suggestionBuilder.build(mockShardContext);
        assertEquals(toBytesRef(suggestionBuilder.text()), suggestionContext.getText());
        if (suggestionBuilder.text() != null && suggestionBuilder.prefix() == null) {
            assertEquals(toBytesRef(suggestionBuilder.text()), suggestionContext.getPrefix());
        } else {
            assertEquals(toBytesRef(suggestionBuilder.prefix()), suggestionContext.getPrefix());
        }
        assertEquals(toBytesRef(suggestionBuilder.regex()), suggestionContext.getRegex());
        assertEquals(suggestionBuilder.field(), suggestionContext.getField());
        int expectedSize = suggestionBuilder.size() != null ? suggestionBuilder.size : 5;
        assertEquals(expectedSize, suggestionContext.getSize());
        Integer expectedShardSize = suggestionBuilder.shardSize != null ? suggestionBuilder.shardSize : Math.max(expectedSize, 5);
        assertEquals(expectedShardSize, suggestionContext.getShardSize());
        assertSame(mockShardContext, suggestionContext.getShardContext());
        if (suggestionBuilder.analyzer() != null) {
            assertEquals(suggestionBuilder.analyzer(), ((NamedAnalyzer) suggestionContext.getAnalyzer()).name());
        } else if (fieldTypeSearchAnalyzerSet) {
            assertEquals("fieldSearchAnalyzer", ((NamedAnalyzer) suggestionContext.getAnalyzer()).name());
        } else {
            assertEquals("mapperServiceSearchAnalyzer", ((NamedAnalyzer) suggestionContext.getAnalyzer()).name());
        }
        assertSuggestionContext(suggestionBuilder, suggestionContext);
    }
}
Also used : Script(org.opensearch.script.Script) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) SimpleAnalyzer(org.apache.lucene.analysis.core.SimpleAnalyzer) IndexSettings(org.opensearch.index.IndexSettings) Index(org.opensearch.index.Index) ScriptService(org.opensearch.script.ScriptService) SuggestionContext(org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardContext(org.opensearch.index.query.QueryShardContext) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) MapperService(org.opensearch.index.mapper.MapperService)

Example 15 with ScriptService

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

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