Java Code Examples for org.elasticsearch.script.ExecutableScript#run()
The following examples show how to use
org.elasticsearch.script.ExecutableScript#run() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemplateQueryParser.java From Elasticsearch with Apache License 2.0 | 6 votes |
/** * 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(); } }
Example 2
Source File: DocumentMapper.java From Elasticsearch with Apache License 2.0 | 6 votes |
@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); } }
Example 3
Source File: BucketSelectorPipelineAggregator.java From Elasticsearch with Apache License 2.0 | 5 votes |
@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); }
Example 4
Source File: UpdateHelper.java From Elasticsearch with Apache License 2.0 | 5 votes |
private Map<String, Object> executeScript(UpdateRequest request, Map<String, Object> ctx) { try { if (scriptService != null) { ExecutableScript script = scriptService.executable(request.script, ScriptContext.Standard.UPDATE, request, Collections.<String, String>emptyMap()); script.setNextVar("ctx", ctx); script.run(); // we need to unwrap the ctx... ctx = (Map<String, Object>) script.unwrap(ctx); } } catch (Exception e) { throw new IllegalArgumentException("failed to execute script", e); } return ctx; }
Example 5
Source File: BucketScriptPipelineAggregator.java From Elasticsearch with Apache License 2.0 | 4 votes |
@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()); } boolean skipBucket = false; for (Map.Entry<String, String> entry : bucketsPathsMap.entrySet()) { String varName = entry.getKey(); String bucketsPath = entry.getValue(); Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy); if (GapPolicy.SKIP == gapPolicy && (value == null || Double.isNaN(value))) { skipBucket = true; break; } vars.put(varName, value); } if (skipBucket) { newBuckets.add(bucket); } else { ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars); Object returned = executableScript.run(); if (returned == null) { newBuckets.add(bucket); } else { if (!(returned instanceof Number)) { throw new AggregationExecutionException("series_arithmetic script for reducer [" + name() + "] must return a Number"); } List<InternalAggregation> aggs = new ArrayList<>(eagerTransform(bucket.getAggregations().asList(), FUNCTION)); aggs.add(new InternalSimpleValue(name(), ((Number) returned).doubleValue(), formatter, new ArrayList<PipelineAggregator>(), metaData())); InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs), (InternalMultiBucketAggregation.InternalBucket) bucket); newBuckets.add(newBucket); } } } return originalAgg.create(newBuckets); }