Search in sources :

Example 6 with ScriptService

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

the class AbstractScriptTestCase method init.

@Before
public void init() throws Exception {
    Settings settings = Settings.builder().put("path.home", createTempDir()).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), false).build();
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Arrays.asList(new MustacheScriptEngineService()));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    ScriptService scriptService = new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    templateService = new InternalTemplateService(scriptService);
}
Also used : ScriptService(org.elasticsearch.script.ScriptService) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) Settings(org.elasticsearch.common.settings.Settings) ScriptSettings(org.elasticsearch.script.ScriptSettings) MustacheScriptEngineService(org.elasticsearch.script.mustache.MustacheScriptEngineService) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) Before(org.junit.Before)

Example 7 with ScriptService

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

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

the class InternalAggregationTestCase method testReduceRandom.

public final void testReduceRandom() {
    String name = randomAsciiOfLength(5);
    List<T> inputs = new ArrayList<>();
    List<InternalAggregation> toReduce = new ArrayList<>();
    int toReduceSize = between(1, 200);
    for (int i = 0; i < toReduceSize; i++) {
        T t = randomBoolean() ? createUnmappedInstance(name) : createTestInstance(name);
        inputs.add(t);
        toReduce.add(t);
    }
    ScriptService mockScriptService = mockScriptService();
    MockBigArrays bigArrays = new MockBigArrays(Settings.EMPTY, new NoneCircuitBreakerService());
    if (randomBoolean() && toReduce.size() > 1) {
        Collections.shuffle(toReduce, random());
        // we leave at least one element in the list
        int r = Math.max(1, randomIntBetween(0, toReduceSize - 2));
        List<InternalAggregation> internalAggregations = toReduce.subList(0, r);
        InternalAggregation.ReduceContext context = new InternalAggregation.ReduceContext(bigArrays, mockScriptService, false);
        @SuppressWarnings("unchecked") T reduced = (T) inputs.get(0).reduce(internalAggregations, context);
        toReduce = toReduce.subList(r, toReduceSize);
        toReduce.add(reduced);
    }
    InternalAggregation.ReduceContext context = new InternalAggregation.ReduceContext(bigArrays, mockScriptService, true);
    @SuppressWarnings("unchecked") T reduced = (T) inputs.get(0).reduce(toReduce, context);
    assertReduced(reduced, inputs);
}
Also used : ArrayList(java.util.ArrayList) MockBigArrays(org.elasticsearch.common.util.MockBigArrays) ScriptService(org.elasticsearch.script.ScriptService) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

Example 9 with ScriptService

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

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