|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.aggregations.metrics.scripted; |
| 21 | + |
| 22 | +import org.elasticsearch.ElasticsearchException; |
| 23 | +import org.elasticsearch.common.io.stream.Writeable.Reader; |
| 24 | +import org.elasticsearch.common.settings.Settings; |
| 25 | +import org.elasticsearch.env.Environment; |
| 26 | +import org.elasticsearch.script.MockScriptEngine; |
| 27 | +import org.elasticsearch.script.Script; |
| 28 | +import org.elasticsearch.script.ScriptContextRegistry; |
| 29 | +import org.elasticsearch.script.ScriptEngineRegistry; |
| 30 | +import org.elasticsearch.script.ScriptService; |
| 31 | +import org.elasticsearch.script.ScriptSettings; |
| 32 | +import org.elasticsearch.script.ScriptType; |
| 33 | +import org.elasticsearch.search.aggregations.InternalAggregationTestCase; |
| 34 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +public class InternalScriptedMetricTests extends InternalAggregationTestCase<InternalScriptedMetric> { |
| 43 | + |
| 44 | + private static final String REDUCE_SCRIPT_NAME = "reduceScript"; |
| 45 | + // randomized only once so that any random test instance has the same value |
| 46 | + private boolean hasReduceScript = randomBoolean(); |
| 47 | + |
| 48 | + @Override |
| 49 | + protected InternalScriptedMetric createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, |
| 50 | + Map<String, Object> metaData) { |
| 51 | + Map<String, Object> params = new HashMap<>(); |
| 52 | + if (randomBoolean()) { |
| 53 | + params.put(randomAsciiOfLength(5), randomAsciiOfLength(5)); |
| 54 | + } |
| 55 | + Script reduceScript = null; |
| 56 | + if (hasReduceScript) { |
| 57 | + reduceScript = new Script(ScriptType.INLINE, MockScriptEngine.NAME, REDUCE_SCRIPT_NAME, params); |
| 58 | + } |
| 59 | + return new InternalScriptedMetric(name, randomAsciiOfLength(5), reduceScript, pipelineAggregators, metaData); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Mock of the script service. The script that is run looks at the |
| 64 | + * "_aggs" parameter visible when executing the script and simply returns the count. |
| 65 | + * This should be equal to the number of input InternalScriptedMetrics that are reduced |
| 66 | + * in total. |
| 67 | + */ |
| 68 | + @Override |
| 69 | + protected ScriptService mockScriptService() { |
| 70 | + Settings settings = Settings.builder() |
| 71 | + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) |
| 72 | + // no file watching, so we don't need a ResourceWatcherService |
| 73 | + .put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), "false") |
| 74 | + .build(); |
| 75 | + // mock script always retuns the size of the input aggs list as result |
| 76 | + @SuppressWarnings("unchecked") |
| 77 | + MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, |
| 78 | + Collections.singletonMap(REDUCE_SCRIPT_NAME, script -> { |
| 79 | + return ((List<Object>) script.get("_aggs")).size(); |
| 80 | + })); |
| 81 | + ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(scriptEngine)); |
| 82 | + ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList()); |
| 83 | + ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); |
| 84 | + try { |
| 85 | + return new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, |
| 86 | + scriptSettings); |
| 87 | + } catch (IOException e) { |
| 88 | + throw new ElasticsearchException(e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected void assertReduced(InternalScriptedMetric reduced, List<InternalScriptedMetric> inputs) { |
| 94 | + InternalScriptedMetric firstAgg = inputs.get(0); |
| 95 | + assertEquals(firstAgg.getName(), reduced.getName()); |
| 96 | + assertEquals(firstAgg.pipelineAggregators(), reduced.pipelineAggregators()); |
| 97 | + assertEquals(firstAgg.getMetaData(), reduced.getMetaData()); |
| 98 | + if (hasReduceScript) { |
| 99 | + assertEquals(inputs.size(), reduced.aggregation()); |
| 100 | + } else { |
| 101 | + assertEquals(inputs.size(), ((List<Object>) reduced.aggregation()).size()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected Reader<InternalScriptedMetric> instanceReader() { |
| 107 | + return InternalScriptedMetric::new; |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments