当前位置: 首页>>代码示例>>Java>>正文


Java ExecutableScript类代码示例

本文整理汇总了Java中org.elasticsearch.script.ExecutableScript的典型用法代码示例。如果您正苦于以下问题:Java ExecutableScript类的具体用法?Java ExecutableScript怎么用?Java ExecutableScript使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ExecutableScript类属于org.elasticsearch.script包,在下文中一共展示了ExecutableScript类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testBasics

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
public void testBasics() {
    String template = "GET _search {\"query\": " + "{\"boosting\": {"
        + "\"positive\": {\"match\": {\"body\": \"gift\"}},"
        + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}"
        + "}}, \"negative_boost\": {{boost_val}} } }}";
    Map<String, Object> params = Collections.singletonMap("boost_val", "0.2");

    Mustache mustache = (Mustache) engine.compile(null, template, Collections.emptyMap());
    CompiledScript compiledScript = new CompiledScript(INLINE, "my-name", "mustache", mustache);
    ExecutableScript result = engine.executable(compiledScript, params);
    assertEquals(
            "Mustache templating broken",
            "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}},"
                    + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.2 } }}",
            ((BytesReference) result.run()).utf8ToString()
    );
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:18,代码来源:MustacheTests.java

示例2: exec

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
/** Compiles and returns the result of {@code script} with access to {@code vars} and compile-time parameters */
public Object exec(String script, Map<String, Object> vars, Map<String,String> compileParams, Scorer scorer, boolean picky) {
    // test for ambiguity errors before running the actual script if picky is true
    if (picky) {
        ScriptInterface scriptInterface = new ScriptInterface(GenericElasticsearchScript.class);
        CompilerSettings pickySettings = new CompilerSettings();
        pickySettings.setPicky(true);
        pickySettings.setRegexesEnabled(CompilerSettings.REGEX_ENABLED.get(scriptEngineSettings()));
        Walker.buildPainlessTree(scriptInterface, getTestName(), script, pickySettings, null);
    }
    // test actual script execution
    Object object = scriptEngine.compile(null, script, compileParams);
    CompiledScript compiled = new CompiledScript(ScriptType.INLINE, getTestName(), "painless", object);
    ExecutableScript executableScript = scriptEngine.executable(compiled, vars);
    if (scorer != null) {
        ((ScorerAware)executableScript).setScorer(scorer);
    }
    return executableScript.run();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:20,代码来源:ScriptTestCase.java

示例3: testChangingVarsCrossExecution1

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
public void testChangingVarsCrossExecution1() {
    Map<String, Object> vars = new HashMap<>();
    Map<String, Object> ctx = new HashMap<>();
    vars.put("ctx", ctx);

    Object compiledScript = scriptEngine.compile(null,
            "return ctx.value;", Collections.emptyMap());
    ExecutableScript script = scriptEngine.executable(new CompiledScript(ScriptType.INLINE,
            "testChangingVarsCrossExecution1", "painless", compiledScript), vars);

    ctx.put("value", 1);
    Object o = script.run();
    assertEquals(1, ((Number) o).intValue());

    ctx.put("value", 2);
    o = script.run();
    assertEquals(2, ((Number) o).intValue());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:19,代码来源:ScriptEngineTests.java

示例4: createInternal

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
@Override
public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators,
        Map<String, Object> metaData) throws IOException {
    if (collectsFromSingleBucket == false) {
        return asMultiBucketAggregator(this, context, parent);
    }
    Map<String, Object> params = this.params;
    if (params != null) {
        params = deepCopyParams(params, context);
    } else {
        params = new HashMap<>();
        params.put("_agg", new HashMap<String, Object>());
    }

    final ExecutableScript initScript = this.initScript.apply(params);
    final SearchScript mapScript = this.mapScript.apply(params);
    final ExecutableScript combineScript = this.combineScript.apply(params);

    final Script reduceScript = deepCopyScript(this.reduceScript, context);
    if (initScript != null) {
        initScript.run();
    }
    return new ScriptedMetricAggregator(name, mapScript,
            combineScript, reduceScript, params, context, parent,
            pipelineAggregators, metaData);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:27,代码来源:ScriptedMetricAggregatorFactory.java

示例5: doBuild

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
@Override
protected ScriptedMetricAggregatorFactory doBuild(SearchContext context, AggregatorFactory<?> parent,
        Builder subfactoriesBuilder) throws IOException {

    QueryShardContext queryShardContext = context.getQueryShardContext();
    Function<Map<String, Object>, ExecutableScript> executableInitScript;
    if (initScript != null) {
        executableInitScript = queryShardContext.getLazyExecutableScript(initScript, ScriptContext.Standard.AGGS);
    } else {
        executableInitScript = (p) -> null;
    }
    Function<Map<String, Object>, SearchScript> searchMapScript = queryShardContext.getLazySearchScript(mapScript,
        ScriptContext.Standard.AGGS);
    Function<Map<String, Object>, ExecutableScript> executableCombineScript;
    if (combineScript != null) {
        executableCombineScript = queryShardContext.getLazyExecutableScript(combineScript, ScriptContext.Standard.AGGS);
    } else {
        executableCombineScript = (p) -> null;
    }
    return new ScriptedMetricAggregatorFactory(name, searchMapScript, executableInitScript, executableCombineScript, reduceScript,
            params, context, parent, subfactoriesBuilder, metaData);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:23,代码来源:ScriptedMetricAggregationBuilder.java

示例6: executable

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
@Override
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> params) {
    final String field = (String) compiledScript.compiled();
    return new ExecutableScript() {

        Map<String, Object> vars = new HashMap<>();

        @Override
        public void setNextVar(String name, Object value) {
            vars.put(name, value);
        }

        @Override
        public Object run() {
            Map<String, Object> ctx = (Map<String, Object>) vars.get("ctx");
            assertNotNull(ctx);
            Map<String, Object> source = (Map<String, Object>) ctx.get("_source");
            Number currentValue = (Number) source.get(field);
            Number inc = params == null ? 1L : (Number) params.getOrDefault("inc", 1);
            source.put(field, currentValue.longValue() + inc.longValue());
            return ctx;
        }
    };
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:25,代码来源:UpdateIT.java

示例7: executable

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
@Override
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> params) {
    String script = (String) compiledScript.compiled();
    for (Entry<String, Object> entry : params.entrySet()) {
        script = script.replace("{{" + entry.getKey() + "}}", String.valueOf(entry.getValue()));
    }
    String result = script;
    return new ExecutableScript() {
        @Override
        public void setNextVar(String name, Object value) {
            throw new UnsupportedOperationException("setNextVar not supported");
        }

        @Override
        public Object run() {
            return new BytesArray(result);
        }
    };
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:20,代码来源:SuggestSearchIT.java

示例8: newScript

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
@Override
public ExecutableScript newScript(Map<String, Object> params) {
    return new ExecutableScript() {
        private Map<String, Object> ctx;

        @Override
        @SuppressWarnings("unchecked")  // Elasicsearch convention
        public void setNextVar(String name, Object value) {
            if (name.equals("ctx")) {
                ctx = (Map<String, Object>) value;
            }
        }

        @Override
        public Object run() {
            ctx.put("op", "delete");
            return null;
        }
    };
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:21,代码来源:WaitUntilRefreshIT.java

示例9: ScriptHeuristic

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
public ScriptHeuristic(ExecutableScript searchScript, Script script) {
    subsetSizeHolder = new LongAccessor();
    supersetSizeHolder = new LongAccessor();
    subsetDfHolder = new LongAccessor();
    supersetDfHolder = new LongAccessor();
    this.searchScript = searchScript;
    if (searchScript != null) {
        searchScript.setNextVar("_subset_freq", subsetDfHolder);
        searchScript.setNextVar("_subset_size", subsetSizeHolder);
        searchScript.setNextVar("_superset_freq", supersetDfHolder);
        searchScript.setNextVar("_superset_size", supersetSizeHolder);
    }
    this.script = script;


}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:17,代码来源:ScriptHeuristic.java

示例10: parse

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
/**
 * Parses the template query replacing template parameters with provided
 * values. Handles both submitting the template as part of the request as
 * well as referencing only the template name.
 *
 * @param parseContext
 *            parse context containing the templated query.
 */
@Override
@Nullable
public Query parse(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    Template template = parse(parser, parseContext.parseFieldMatcher());
    ExecutableScript executable = this.scriptService.executable(template, ScriptContext.Standard.SEARCH, SearchContext.current(), Collections.<String, String>emptyMap());

    BytesReference querySource = (BytesReference) executable.run();

    try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource)) {
        final QueryParseContext context = new QueryParseContext(parseContext.index(), parseContext.indexQueryParserService());
        context.reset(qSourceParser);
        return context.parseInnerQuery();
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:24,代码来源:TemplateQueryParser.java

示例11: transformSourceAsMap

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public Map<String, Object> transformSourceAsMap(Map<String, Object> sourceAsMap) {
    try {
        // We use the ctx variable and the _source name to be consistent with the update api.
        ExecutableScript executable = scriptService.executable(script, ScriptContext.Standard.MAPPING, null, Collections.<String, String>emptyMap());
        Map<String, Object> ctx = new HashMap<>(1);
        ctx.put("_source", sourceAsMap);
        executable.setNextVar("ctx", ctx);
        executable.run();
        ctx = (Map<String, Object>) executable.unwrap(ctx);
        return (Map<String, Object>) ctx.get("_source");
    } catch (Exception e) {
        throw new IllegalArgumentException("failed to execute script", e);
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:17,代码来源:DocumentMapper.java

示例12: doExecute

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
@Override
protected void doExecute(final RenderSearchTemplateRequest request, final ActionListener<RenderSearchTemplateResponse> listener) {
    threadPool.generic().execute(new AbstractRunnable() {

        @Override
        public void onFailure(Throwable t) {
            listener.onFailure(t);
        }

        @Override
        protected void doRun() throws Exception {
            ExecutableScript executable = scriptService.executable(request.template(), ScriptContext.Standard.SEARCH, request, Collections.<String, String>emptyMap());
            BytesReference processedTemplate = (BytesReference) executable.run();
            RenderSearchTemplateResponse response = new RenderSearchTemplateResponse();
            response.source(processedTemplate);
            listener.onResponse(response);
        }
    });
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:20,代码来源:TransportRenderSearchTemplateAction.java

示例13: testChangingVarsCrossExecution1

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
@Test
public void testChangingVarsCrossExecution1() {
    Map<String, Object> vars = new HashMap<String, Object>();
    Map<String, Object> ctx = new HashMap<String, Object>();
    vars.put("ctx", ctx);
    Object compiledScript = se.compile("ctx.value");

    ExecutableScript script = se.executable(compiledScript, vars);
    ctx.put("value", 1);
    Object o = script.run();
    assertThat(((Number) o).intValue(), equalTo(1));

    ctx.put("value", 2);
    o = script.run();
    assertThat(((Number) o).intValue(), equalTo(2));
}
 
开发者ID:jprante,项目名称:elasticsearch-lang-javascript-nashorn,代码行数:17,代码来源:NashornScriptEngineTests.java

示例14: execute

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
/**
 * Executes the script with the Ingest document in context.
 *
 * @param document The Ingest document passed into the script context under the "ctx" object.
 */
@Override
public void execute(IngestDocument document) {
    ExecutableScript executableScript = scriptService.executable(script, ScriptContext.Standard.INGEST);
    executableScript.setNextVar("ctx",  document.getSourceAndMetadata());
    executableScript.run();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:12,代码来源:ScriptProcessor.java

示例15: testScripting

import org.elasticsearch.script.ExecutableScript; //导入依赖的package包/类
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));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:30,代码来源:ScriptProcessorTests.java


注:本文中的org.elasticsearch.script.ExecutableScript类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。