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


Java ScriptContext类代码示例

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


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

示例1: doBuild

import org.elasticsearch.script.ScriptContext; //导入依赖的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

示例2: doBuild

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
@Override
protected TopHitsAggregatorFactory doBuild(SearchContext context, AggregatorFactory<?> parent, Builder subfactoriesBuilder)
        throws IOException {
    List<ScriptFieldsContext.ScriptField> fields = new ArrayList<>();
    if (scriptFields != null) {
        for (ScriptField field : scriptFields) {
            SearchScript searchScript = context.getQueryShardContext().getSearchScript(field.script(),
                ScriptContext.Standard.SEARCH);
            fields.add(new org.elasticsearch.search.fetch.subphase.ScriptFieldsContext.ScriptField(
                field.fieldName(), searchScript, field.ignoreFailure()));
        }
    }

    final Optional<SortAndFormats> optionalSort;
    if (sorts == null) {
        optionalSort = Optional.empty();
    } else {
        optionalSort = SortBuilder.buildSort(sorts, context.getQueryShardContext());
    }
    return new TopHitsAggregatorFactory(name, from, size, explain, version, trackScores, optionalSort, highlightBuilder,
            storedFieldsContext, fieldDataFields, fields, fetchSourceContext, context, parent, subfactoriesBuilder, metaData);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:23,代码来源:TopHitsAggregationBuilder.java

示例3: init

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

示例4: ScriptedMetricAggregator

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
protected ScriptedMetricAggregator(String name, Script initScript, Script mapScript, Script combineScript, Script reduceScript,
        Map<String, Object> params, AggregationContext context, Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData)
        throws IOException {
    super(name, context, parent, pipelineAggregators, metaData);
    this.params = params;
    ScriptService scriptService = context.searchContext().scriptService();
    if (initScript != null) {
        scriptService.executable(initScript, ScriptContext.Standard.AGGS, context.searchContext(), Collections.<String, String>emptyMap()).run();
    }
    this.mapScript = scriptService.search(context.searchContext().lookup(), mapScript, ScriptContext.Standard.AGGS, Collections.<String, String>emptyMap());
    if (combineScript != null) {
        this.combineScript = scriptService.executable(combineScript, ScriptContext.Standard.AGGS, context.searchContext(), Collections.<String, String>emptyMap());
    } else {
        this.combineScript = null;
    }
    this.reduceScript = reduceScript;
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:18,代码来源:ScriptedMetricAggregator.java

示例5: parse

import org.elasticsearch.script.ScriptContext; //导入依赖的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

示例6: transformSourceAsMap

import org.elasticsearch.script.ScriptContext; //导入依赖的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

示例7: doExecute

import org.elasticsearch.script.ScriptContext; //导入依赖的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

示例8: execute

import org.elasticsearch.script.ScriptContext; //导入依赖的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

示例9: reduce

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg =
            (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
    List<? extends Bucket> buckets = originalAgg.getBuckets();

    CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS);
    List newBuckets = new ArrayList<>();
    for (Bucket bucket : buckets) {
        Map<String, Object> vars = new HashMap<>();
        if (script.getParams() != null) {
            vars.putAll(script.getParams());
        }
        for (Map.Entry<String, String> entry : bucketsPathsMap.entrySet()) {
            String varName = entry.getKey();
            String bucketsPath = entry.getValue();
            Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy);
            vars.put(varName, value);
        }
        ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars);
        Object scriptReturnValue = executableScript.run();
        final boolean keepBucket;
        // TODO: WTF!!!!!
        if ("expression".equals(script.getLang())) {
            double scriptDoubleValue = (double) scriptReturnValue;
            keepBucket = scriptDoubleValue == 1.0;
        } else {
            keepBucket = (boolean) scriptReturnValue;
        }
        if (keepBucket) {
            newBuckets.add(bucket);
        }
    }
    return originalAgg.create(newBuckets);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:36,代码来源:BucketSelectorPipelineAggregator.java

示例10: createScript

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
private static SearchScript createScript(Script script, QueryShardContext context) {
    if (script == null) {
        return null;
    } else {
        return context.getSearchScript(script, ScriptContext.Standard.AGGS);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:8,代码来源:ValuesSourceConfig.java

示例11: compile

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
@Override
public Template compile(String template) {
    int mustacheStart = template.indexOf("{{");
    int mustacheEnd = template.indexOf("}}");
    if (mustacheStart != -1 && mustacheEnd != -1 && mustacheStart < mustacheEnd) {
        Script script = new Script(ScriptType.INLINE, "mustache", template, Collections.emptyMap());
        CompiledScript compiledScript = scriptService.compile(script, ScriptContext.Standard.INGEST);
        return new Template() {
            @Override
            public String execute(Map<String, Object> model) {
                ExecutableScript executableScript = scriptService.executable(compiledScript, model);
                Object result = executableScript.run();
                if (result instanceof BytesReference) {
                    return ((BytesReference) result).utf8ToString();
                }
                return String.valueOf(result);
            }

            @Override
            public String getKey() {
                return template;
            }
        };
    } else {
        return new StringTemplate(template);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:28,代码来源:InternalTemplateService.java

示例12: doToFunction

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
@Override
protected ScoreFunction doToFunction(QueryShardContext context) {
    try {
        SearchScript searchScript = context.getSearchScript(script, ScriptContext.Standard.SEARCH);
        return new ScriptScoreFunction(script, searchScript);
    } catch (Exception e) {
        throw new QueryShardException(context, "script_score: the script could not be loaded", e);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:10,代码来源:ScriptScoreFunctionBuilder.java

示例13: setupInnerHitsContext

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
private void setupInnerHitsContext(QueryShardContext context, InnerHitsContext.BaseInnerHits innerHitsContext) throws IOException {
    innerHitsContext.from(from);
    innerHitsContext.size(size);
    innerHitsContext.explain(explain);
    innerHitsContext.version(version);
    innerHitsContext.trackScores(trackScores);
    if (storedFieldsContext != null) {
        innerHitsContext.storedFieldsContext(storedFieldsContext);
    }
    if (docValueFields != null) {
        innerHitsContext.docValueFieldsContext(new DocValueFieldsContext(docValueFields));
    }
    if (scriptFields != null) {
        for (ScriptField field : scriptFields) {
            SearchScript searchScript = innerHitsContext.getQueryShardContext().getSearchScript(field.script(),
                ScriptContext.Standard.SEARCH);
            innerHitsContext.scriptFields().add(new org.elasticsearch.search.fetch.subphase.ScriptFieldsContext.ScriptField(
                    field.fieldName(), searchScript, field.ignoreFailure()));
        }
    }
    if (fetchSourceContext != null) {
        innerHitsContext.fetchSourceContext(fetchSourceContext);
    }
    if (sorts != null) {
        Optional<SortAndFormats> optionalSort = SortBuilder.buildSort(sorts, context);
        if (optionalSort.isPresent()) {
            innerHitsContext.sort(optionalSort.get());
        }
    }
    if (highlightBuilder != null) {
        innerHitsContext.highlight(highlightBuilder.build(context));
    }
    ParsedQuery parsedQuery = new ParsedQuery(query.toQuery(context), context.copyNamedQueries());
    innerHitsContext.parsedQuery(parsedQuery);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:36,代码来源:InnerHitBuilder.java

示例14: executeScript

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
private Map<String, Object> executeScript(Script script, Map<String, Object> ctx) {
    try {
        if (scriptService != null) {
            ExecutableScript executableScript = scriptService.executable(script, ScriptContext.Standard.UPDATE);
            executableScript.setNextVar("ctx", ctx);
            executableScript.run();
            // we need to unwrap the ctx...
            ctx = (Map<String, Object>) executableScript.unwrap(ctx);
        }
    } catch (Exception e) {
        throw new IllegalArgumentException("failed to execute script", e);
    }
    return ctx;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:15,代码来源:UpdateHelper.java

示例15: reduce

import org.elasticsearch.script.ScriptContext; //导入依赖的package包/类
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg = (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
    List<? extends Bucket> buckets = originalAgg.getBuckets();

    CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS, reduceContext, Collections.<String, String>emptyMap());
    List newBuckets = new ArrayList<>();
    for (Bucket bucket : buckets) {
        Map<String, Object> vars = new HashMap<>();
        if (script.getParams() != null) {
            vars.putAll(script.getParams());
        }
        for (Map.Entry<String, String> entry : bucketsPathsMap.entrySet()) {
            String varName = entry.getKey();
            String bucketsPath = entry.getValue();
            Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy);
            vars.put(varName, value);
        }
        ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars);
        Object scriptReturnValue = executableScript.run();
        final boolean keepBucket;
        // TODO: WTF!!!!!
        if ("expression".equals(script.getLang())) {
            double scriptDoubleValue = (double) scriptReturnValue;
            keepBucket = scriptDoubleValue == 1.0;
        } else {
            keepBucket = (boolean) scriptReturnValue;
        }
        if (keepBucket) {
            newBuckets.add(bucket);
        }
    }
    return originalAgg.create(newBuckets);
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:35,代码来源:BucketSelectorPipelineAggregator.java


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