Search in sources :

Example 1 with ScriptService

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

the class ScriptProcessorFactoryTests method testFactoryInvalidateWithInvalidCompiledScript.

public void testFactoryInvalidateWithInvalidCompiledScript() throws Exception {
    String randomType = randomFrom("inline", "file", "id");
    ScriptService mockedScriptService = mock(ScriptService.class);
    ScriptException thrownException = new ScriptException("compile-time exception", new RuntimeException(), Collections.emptyList(), "script", "mockscript");
    when(mockedScriptService.compile(any(), any())).thenThrow(thrownException);
    factory = new ScriptProcessor.Factory(mockedScriptService);
    Map<String, Object> configMap = new HashMap<>();
    configMap.put("lang", "mockscript");
    configMap.put(randomType, "my_script");
    ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> factory.create(null, randomAsciiOfLength(10), configMap));
    assertThat(exception.getMessage(), is("compile-time exception"));
}
Also used : ScriptService(org.elasticsearch.script.ScriptService) ScriptException(org.elasticsearch.script.ScriptException) HashMap(java.util.HashMap) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 2 with ScriptService

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

the class ScriptProcessorTests method testScripting.

public void testScripting() throws Exception {
    int randomBytesIn = randomInt();
    int randomBytesOut = randomInt();
    int randomBytesTotal = randomBytesIn + randomBytesOut;
    ScriptService scriptService = mock(ScriptService.class);
    Script script = new Script("_script");
    ExecutableScript executableScript = mock(ExecutableScript.class);
    when(scriptService.executable(any(Script.class), any())).thenReturn(executableScript);
    Map<String, Object> document = new HashMap<>();
    document.put("bytes_in", randomInt());
    document.put("bytes_out", randomInt());
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    doAnswer(invocationOnMock -> {
        ingestDocument.setFieldValue("bytes_total", randomBytesTotal);
        return null;
    }).when(executableScript).run();
    ScriptProcessor processor = new ScriptProcessor(randomAsciiOfLength(10), script, scriptService);
    processor.execute(ingestDocument);
    assertThat(ingestDocument.getSourceAndMetadata(), hasKey("bytes_in"));
    assertThat(ingestDocument.getSourceAndMetadata(), hasKey("bytes_out"));
    assertThat(ingestDocument.getSourceAndMetadata(), hasKey("bytes_total"));
    assertThat(ingestDocument.getSourceAndMetadata().get("bytes_total"), is(randomBytesTotal));
}
Also used : ScriptService(org.elasticsearch.script.ScriptService) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) HashMap(java.util.HashMap) ExecutableScript(org.elasticsearch.script.ExecutableScript) IngestDocument(org.elasticsearch.ingest.IngestDocument)

Example 3 with ScriptService

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

the class IndexModuleTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    indicesQueryCache = new IndicesQueryCache(settings);
    indexSettings = IndexSettingsModule.newIndexSettings("foo", settings);
    index = indexSettings.getIndex();
    environment = new Environment(settings);
    threadPool = new TestThreadPool("test");
    circuitBreakerService = new NoneCircuitBreakerService();
    bigArrays = new BigArrays(settings, circuitBreakerService);
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(emptyList());
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    scriptService = new ScriptService(settings, environment, new ResourceWatcherService(settings, threadPool), scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    clusterService = ClusterServiceUtils.createClusterService(threadPool);
    nodeEnvironment = new NodeEnvironment(settings, environment);
    mapperRegistry = new IndicesModule(Collections.emptyList()).getMapperRegistry();
}
Also used : IndicesQueryCache(org.elasticsearch.indices.IndicesQueryCache) ScriptService(org.elasticsearch.script.ScriptService) BigArrays(org.elasticsearch.common.util.BigArrays) IndicesModule(org.elasticsearch.indices.IndicesModule) ScriptSettings(org.elasticsearch.script.ScriptSettings) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

Example 4 with ScriptService

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

the class AbstractSortTestCase method init.

@BeforeClass
public static void init() 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);
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new TestEngineService()));
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    scriptService = new ScriptService(baseSettings, environment, new ResourceWatcherService(baseSettings, null), scriptEngineRegistry, scriptContextRegistry, scriptSettings) {

        @Override
        public CompiledScript compile(Script script, ScriptContext scriptContext) {
            return new CompiledScript(ScriptType.INLINE, "mockName", "test", script);
        }
    };
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
    namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
    xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
Also used : Path(java.nio.file.Path) ContentPath(org.elasticsearch.index.mapper.ContentPath) CompiledScript(org.elasticsearch.script.CompiledScript) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ScriptContext(org.elasticsearch.script.ScriptContext) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) ScriptService(org.elasticsearch.script.ScriptService) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) SearchModule(org.elasticsearch.search.SearchModule) TestEngineService(org.elasticsearch.script.ScriptServiceTests.TestEngineService) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Settings(org.elasticsearch.common.settings.Settings) ScriptSettings(org.elasticsearch.script.ScriptSettings) IndexSettings(org.elasticsearch.index.IndexSettings) BeforeClass(org.junit.BeforeClass)

Example 5 with ScriptService

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

Aggregations

ScriptService (org.elasticsearch.script.ScriptService)9 Environment (org.elasticsearch.env.Environment)6 ScriptContextRegistry (org.elasticsearch.script.ScriptContextRegistry)6 ScriptEngineRegistry (org.elasticsearch.script.ScriptEngineRegistry)6 ScriptSettings (org.elasticsearch.script.ScriptSettings)6 Settings (org.elasticsearch.common.settings.Settings)5 HashMap (java.util.HashMap)4 Script (org.elasticsearch.script.Script)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 MockScriptEngine (org.elasticsearch.script.MockScriptEngine)3 ResourceWatcherService (org.elasticsearch.watcher.ResourceWatcherService)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Map (java.util.Map)2 IndexSettings (org.elasticsearch.index.IndexSettings)2 NoneCircuitBreakerService (org.elasticsearch.indices.breaker.NoneCircuitBreakerService)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Function (java.util.function.Function)1