Search in sources :

Example 1 with MockScriptEngine

use of org.elasticsearch.script.MockScriptEngine in project elasticsearch by elastic.

the class InternalScriptedMetricTests method mockScriptService.

/**
     * Mock of the script service. The script that is run looks at the
     * "_aggs" parameter visible when executing the script and simply returns the count.
     * This should be equal to the number of input InternalScriptedMetrics that are reduced
     * in total.
     */
@Override
protected ScriptService mockScriptService() {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), "false").build();
    // mock script always retuns the size of the input aggs list as result
    @SuppressWarnings("unchecked") MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, Collections.singletonMap(REDUCE_SCRIPT_NAME, script -> {
        return ((List<Object>) script.get("_aggs")).size();
    }));
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(scriptEngine));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    try {
        return new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    } catch (IOException e) {
        throw new ElasticsearchException(e);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) Script(org.elasticsearch.script.Script) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) InternalAggregationTestCase(org.elasticsearch.search.aggregations.InternalAggregationTestCase) ScriptType(org.elasticsearch.script.ScriptType) Reader(org.elasticsearch.common.io.stream.Writeable.Reader) IOException(java.io.IOException) HashMap(java.util.HashMap) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) MockScriptEngine(org.elasticsearch.script.MockScriptEngine) Map(java.util.Map) ScriptService(org.elasticsearch.script.ScriptService) Collections(java.util.Collections) ScriptService(org.elasticsearch.script.ScriptService) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) MockScriptEngine(org.elasticsearch.script.MockScriptEngine) Environment(org.elasticsearch.env.Environment) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ScriptSettings(org.elasticsearch.script.ScriptSettings) Settings(org.elasticsearch.common.settings.Settings) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry)

Example 2 with MockScriptEngine

use of org.elasticsearch.script.MockScriptEngine in project elasticsearch by elastic.

the class UpdateRequestTests method testNowInScript.

public void testNowInScript() throws IOException {
    Path genericConfigFolder = createTempDir();
    Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(Environment.PATH_CONF_SETTING.getKey(), genericConfigFolder).build();
    Environment environment = new Environment(baseSettings);
    Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
    scripts.put("ctx._source.update_timestamp = ctx._now", (vars) -> {
        Map<String, Object> vars2 = vars;
        @SuppressWarnings("unchecked") Map<String, Object> ctx = (Map<String, Object>) vars2.get("ctx");
        @SuppressWarnings("unchecked") Map<String, Object> source = (Map<String, Object>) ctx.get("_source");
        source.put("update_timestamp", ctx.get("_now"));
        return null;
    });
    scripts.put("ctx._timestamp = ctx._now", (vars) -> {
        @SuppressWarnings("unchecked") Map<String, Object> ctx = (Map<String, Object>) vars.get("ctx");
        ctx.put("_timestamp", ctx.get("_now"));
        return null;
    });
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new MockScriptEngine("mock", scripts)));
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    ScriptService scriptService = new ScriptService(baseSettings, environment, new ResourceWatcherService(baseSettings, null), scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    Settings settings = settings(Version.CURRENT).build();
    UpdateHelper updateHelper = new UpdateHelper(settings, scriptService);
    // We just upsert one document with now() using a script
    IndexRequest indexRequest = new IndexRequest("test", "type1", "2").source(jsonBuilder().startObject().field("foo", "bar").endObject());
    {
        UpdateRequest updateRequest = new UpdateRequest("test", "type1", "2").upsert(indexRequest).script(new Script(ScriptType.INLINE, "mock", "ctx._source.update_timestamp = ctx._now", Collections.emptyMap())).scriptedUpsert(true);
        long nowInMillis = randomNonNegativeLong();
        // We simulate that the document is not existing yet
        GetResult getResult = new GetResult("test", "type1", "2", 0, false, null, null);
        UpdateHelper.Result result = updateHelper.prepare(new ShardId("test", "_na_", 0), updateRequest, getResult, () -> nowInMillis);
        Streamable action = result.action();
        assertThat(action, instanceOf(IndexRequest.class));
        IndexRequest indexAction = (IndexRequest) action;
        assertEquals(indexAction.sourceAsMap().get("update_timestamp"), nowInMillis);
    }
    {
        UpdateRequest updateRequest = new UpdateRequest("test", "type1", "2").upsert(indexRequest).script(new Script(ScriptType.INLINE, "mock", "ctx._timestamp = ctx._now", Collections.emptyMap())).scriptedUpsert(true);
        // We simulate that the document is not existing yet
        GetResult getResult = new GetResult("test", "type1", "2", 0, true, new BytesArray("{}"), null);
        UpdateHelper.Result result = updateHelper.prepare(new ShardId("test", "_na_", 0), updateRequest, getResult, () -> 42L);
        Streamable action = result.action();
        assertThat(action, instanceOf(IndexRequest.class));
    }
}
Also used : HashMap(java.util.HashMap) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetResult(org.elasticsearch.index.get.GetResult) ScriptService(org.elasticsearch.script.ScriptService) ShardId(org.elasticsearch.index.shard.ShardId) Function(java.util.function.Function) ScriptSettings(org.elasticsearch.script.ScriptSettings) MockScriptEngine(org.elasticsearch.script.MockScriptEngine) Streamable(org.elasticsearch.common.io.stream.Streamable) ScriptSettings(org.elasticsearch.script.ScriptSettings) Settings(org.elasticsearch.common.settings.Settings) Path(java.nio.file.Path) Script(org.elasticsearch.script.Script) BytesArray(org.elasticsearch.common.bytes.BytesArray) GetResult(org.elasticsearch.index.get.GetResult) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) HashMap(java.util.HashMap) Map(java.util.Map) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService)

Example 3 with MockScriptEngine

use of org.elasticsearch.script.MockScriptEngine in project elasticsearch by elastic.

the class ESTestCase method newTestScriptModule.

public static ScriptModule newTestScriptModule() {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), false).build();
    Environment environment = new Environment(settings);
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, Collections.singletonMap("1", script -> "1"));
    return new ScriptModule(settings, environment, null, singletonList(scriptEngine), emptyList());
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) Arrays(java.util.Arrays) LoggerContext(org.apache.logging.log4j.core.LoggerContext) TestRuleAdapter(com.carrotsearch.randomizedtesting.rules.TestRuleAdapter) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) TokenizerFactory(org.elasticsearch.index.analysis.TokenizerFactory) Level(org.apache.logging.log4j.Level) IndexAnalyzers(org.elasticsearch.index.analysis.IndexAnalyzers) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) JsonXContent(org.elasticsearch.common.xcontent.json.JsonXContent) BooleanSupplier(java.util.function.BooleanSupplier) Collections.singletonList(java.util.Collections.singletonList) DirectoryStream(java.nio.file.DirectoryStream) RandomNumbers(com.carrotsearch.randomizedtesting.generators.RandomNumbers) ThreadLeakLingering(com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering) Map(java.util.Map) TimeUnits(org.apache.lucene.util.TimeUnits) AnalysisModule(org.elasticsearch.indices.analysis.AnalysisModule) Path(java.nio.file.Path) RandomStrings(com.carrotsearch.randomizedtesting.generators.RandomStrings) AfterClass(org.junit.AfterClass) CharFilterFactory(org.elasticsearch.index.analysis.CharFilterFactory) LoggingListener(org.elasticsearch.test.junit.listeners.LoggingListener) Set(java.util.Set) CollectionUtils.arrayAsArrayList(org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList) MockBigArrays(org.elasticsearch.common.util.MockBigArrays) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest) MockPageCacheRecycler(org.elasticsearch.common.util.MockPageCacheRecycler) UncheckedIOException(java.io.UncheckedIOException) StatusConsoleListener(org.apache.logging.log4j.status.StatusConsoleListener) ReproduceInfoPrinter(org.elasticsearch.test.junit.listeners.ReproduceInfoPrinter) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) RandomPicks(com.carrotsearch.randomizedtesting.generators.RandomPicks) SuppressCodecs(org.apache.lucene.util.LuceneTestCase.SuppressCodecs) TimeoutSuite(com.carrotsearch.randomizedtesting.annotations.TimeoutSuite) NamedWriteable(org.elasticsearch.common.io.stream.NamedWriteable) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) ScriptModule(org.elasticsearch.script.ScriptModule) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) TestRuleMarkFailure(org.apache.lucene.util.TestRuleMarkFailure) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) BootstrapForTesting(org.elasticsearch.bootstrap.BootstrapForTesting) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) ThreadLeakScope(com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope) IndicesModule(org.elasticsearch.indices.IndicesModule) IndicesService(org.elasticsearch.indices.IndicesService) PathUtils(org.elasticsearch.common.io.PathUtils) Before(org.junit.Before) Loggers(org.elasticsearch.common.logging.Loggers) Files(java.nio.file.Files) PathUtilsForTesting(org.elasticsearch.common.io.PathUtilsForTesting) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) TreeMap(java.util.TreeMap) ScriptService(org.elasticsearch.script.ScriptService) UninvertingReader(org.apache.lucene.uninverting.UninvertingReader) AnalysisPlugin(org.elasticsearch.plugins.AnalysisPlugin) Environment(org.elasticsearch.env.Environment) XContent(org.elasticsearch.common.xcontent.XContent) Random(java.util.Random) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ThreadPool(org.elasticsearch.threadpool.ThreadPool) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Setting(org.elasticsearch.common.settings.Setting) MockSearchService(org.elasticsearch.search.MockSearchService) Predicate(java.util.function.Predicate) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) ToXContent(org.elasticsearch.common.xcontent.ToXContent) AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) ClusterModule(org.elasticsearch.cluster.ClusterModule) BytesReference(org.elasticsearch.common.bytes.BytesReference) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Version(org.elasticsearch.Version) TransportAddress(org.elasticsearch.common.transport.TransportAddress) MockScriptEngine(org.elasticsearch.script.MockScriptEngine) Matchers.equalTo(org.hamcrest.Matchers.equalTo) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) StatusData(org.apache.logging.log4j.status.StatusData) DeprecationLogger(org.elasticsearch.common.logging.DeprecationLogger) BeforeClass(org.junit.BeforeClass) XContentType(org.elasticsearch.common.xcontent.XContentType) TestUtil(org.apache.lucene.util.TestUtil) Index(org.elasticsearch.index.Index) HashSet(java.util.HashSet) Configurator(org.apache.logging.log4j.core.config.Configurator) IndexSettings(org.elasticsearch.index.IndexSettings) Listeners(com.carrotsearch.randomizedtesting.annotations.Listeners) Requests(org.elasticsearch.client.Requests) MapperPlugin(org.elasticsearch.plugins.MapperPlugin) ExecutorService(java.util.concurrent.ExecutorService) Scope(com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope.Scope) Matchers.empty(org.hamcrest.Matchers.empty) Mapper(org.elasticsearch.index.mapper.Mapper) RuleChain(org.junit.rules.RuleChain) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) Rule(org.junit.Rule) StreamInput(org.elasticsearch.common.io.stream.StreamInput) StatusLogger(org.apache.logging.log4j.status.StatusLogger) TokenFilterFactory(org.elasticsearch.index.analysis.TokenFilterFactory) CodepointSetGenerator(com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator) Writeable(org.elasticsearch.common.io.stream.Writeable) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) MetadataFieldMapper(org.elasticsearch.index.mapper.MetadataFieldMapper) InputStream(java.io.InputStream) ScriptModule(org.elasticsearch.script.ScriptModule) Environment(org.elasticsearch.env.Environment) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) MockScriptEngine(org.elasticsearch.script.MockScriptEngine) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 4 with MockScriptEngine

use of org.elasticsearch.script.MockScriptEngine in project elasticsearch by elastic.

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(final MappedFieldType[] fieldTypes, IndexSettings idxSettings, CircuitBreakerService circuitBreakerService) {
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), "false").build();
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, SCRIPTS);
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(scriptEngine));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    ScriptService scriptService;
    try {
        scriptService = new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    } catch (IOException e) {
        throw new ElasticsearchException(e);
    }
    return new QueryShardContext(0, idxSettings, null, null, null, null, scriptService, xContentRegistry(), null, null, System::currentTimeMillis);
}
Also used : ScriptService(org.elasticsearch.script.ScriptService) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) MockScriptEngine(org.elasticsearch.script.MockScriptEngine) Environment(org.elasticsearch.env.Environment) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ScriptSettings(org.elasticsearch.script.ScriptSettings) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry)

Aggregations

Settings (org.elasticsearch.common.settings.Settings)4 Environment (org.elasticsearch.env.Environment)4 MockScriptEngine (org.elasticsearch.script.MockScriptEngine)4 ScriptService (org.elasticsearch.script.ScriptService)4 IOException (java.io.IOException)3 Map (java.util.Map)3 ScriptContextRegistry (org.elasticsearch.script.ScriptContextRegistry)3 ScriptEngineRegistry (org.elasticsearch.script.ScriptEngineRegistry)3 ScriptSettings (org.elasticsearch.script.ScriptSettings)3 Path (java.nio.file.Path)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 IndexSettings (org.elasticsearch.index.IndexSettings)2 Script (org.elasticsearch.script.Script)2 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)1 Listeners (com.carrotsearch.randomizedtesting.annotations.Listeners)1 ThreadLeakLingering (com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering)1 ThreadLeakScope (com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope)1