Skip to content

Commit

Permalink
Aggregations: makes script params consistent with other APIs in scrip…
Browse files Browse the repository at this point in the history
…ted_metric

This change removes the script_type parameter form the Scripted Metric Aggregation and adds support for _file and _id suffixes to the init_script, map_script, combine_script and reduce_script parameters to make defining the source of the script consistent with the other APIs which use the ScriptService
  • Loading branch information
colings86 committed Oct 6, 2014
1 parent 2cbe1b9 commit 6cf3713
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 40 deletions.
Expand Up @@ -227,5 +227,12 @@ params:: Optional. An object whose contents will be passed as variable
reduce_params:: Optional. An object whose contents will be passed as variables to the `reduce_script`. This can be useful to allow the user to control
the behavior of the reduce phase. If this is not specified the variable will be undefined in the reduce_script execution.
lang:: Optional. The script language used for the scripts. If this is not specified the default scripting language is used.
script_type:: Optional. The type of script provided. This can be `inline`, `file` or `indexed`. The default is `inline`.
init_script_file:: Optional. Can be used in place of the `init_script` parameter to provide the script using in a file.
init_script_id:: Optional. Can be used in place of the `init_script` parameter to provide the script using an indexed script.
map_script_file:: Optional. Can be used in place of the `map_script` parameter to provide the script using in a file.
map_script_id:: Optional. Can be used in place of the `map_script` parameter to provide the script using an indexed script.
combine_script_file:: Optional. Can be used in place of the `combine_script` parameter to provide the script using in a file.
combine_script_id:: Optional. Can be used in place of the `combine_script` parameter to provide the script using an indexed script.
reduce_script_file:: Optional. Can be used in place of the `reduce_script` parameter to provide the script using in a file.
reduce_script_id:: Optional. Can be used in place of the `reduce_script` parameter to provide the script using an indexed script.

Expand Up @@ -46,15 +46,15 @@ public class ScriptedMetricAggregator extends MetricsAggregator {
// initial parameters for {reduce}
private final Map<String, Object> reduceParams;
private ScriptService scriptService;
private ScriptType scriptType;
private ScriptType reduceScriptType;

protected ScriptedMetricAggregator(String name, String scriptLang, ScriptType scriptType, String initScript, String mapScript,
String combineScript, String reduceScript, Map<String, Object> params, Map<String, Object> reduceParams,
AggregationContext context, Aggregator parent) {
protected ScriptedMetricAggregator(String name, String scriptLang, ScriptType initScriptType, String initScript,
ScriptType mapScriptType, String mapScript, ScriptType combineScriptType, String combineScript, ScriptType reduceScriptType,
String reduceScript, Map<String, Object> params, Map<String, Object> reduceParams, AggregationContext context, Aggregator parent) {
super(name, 1, context, parent);
this.scriptService = context.searchContext().scriptService();
this.scriptLang = scriptLang;
this.scriptType = scriptType;
this.reduceScriptType = reduceScriptType;
if (params == null) {
this.params = new HashMap<>();
this.params.put("_agg", new HashMap<>());
Expand All @@ -67,11 +67,11 @@ protected ScriptedMetricAggregator(String name, String scriptLang, ScriptType sc
this.reduceParams = reduceParams;
}
if (initScript != null) {
scriptService.executable(scriptLang, initScript, scriptType, this.params).run();
scriptService.executable(scriptLang, initScript, initScriptType, this.params).run();
}
this.mapScript = scriptService.search(context.searchContext().lookup(), scriptLang, mapScript, scriptType, this.params);
this.mapScript = scriptService.search(context.searchContext().lookup(), scriptLang, mapScript, mapScriptType, this.params);
if (combineScript != null) {
this.combineScript = scriptService.executable(scriptLang, combineScript, scriptType, this.params);
this.combineScript = scriptService.executable(scriptLang, combineScript, combineScriptType, this.params);
} else {
this.combineScript = null;
}
Expand Down Expand Up @@ -102,30 +102,36 @@ public InternalAggregation buildAggregation(long owningBucketOrdinal) {
} else {
aggregation = params.get("_agg");
}
return new InternalScriptedMetric(name, aggregation, scriptLang, scriptType, reduceScript, reduceParams);
return new InternalScriptedMetric(name, aggregation, scriptLang, reduceScriptType, reduceScript, reduceParams);
}

@Override
public InternalAggregation buildEmptyAggregation() {
return new InternalScriptedMetric(name, null, scriptLang, scriptType, reduceScript, reduceParams);
return new InternalScriptedMetric(name, null, scriptLang, reduceScriptType, reduceScript, reduceParams);
}

public static class Factory extends AggregatorFactory {

private String scriptLang;
private ScriptType scriptType;
private ScriptType initScriptType;
private ScriptType mapScriptType;
private ScriptType combineScriptType;
private ScriptType reduceScriptType;
private String initScript;
private String mapScript;
private String combineScript;
private String reduceScript;
private Map<String, Object> params;
private Map<String, Object> reduceParams;

public Factory(String name, String scriptLang, ScriptType scriptType, String initScript, String mapScript, String combineScript, String reduceScript,
public Factory(String name, String scriptLang, ScriptType initScriptType, String initScript, ScriptType mapScriptType, String mapScript, ScriptType combineScriptType, String combineScript, ScriptType reduceScriptType, String reduceScript,
Map<String, Object> params, Map<String, Object> reduceParams) {
super(name, InternalScriptedMetric.TYPE.name());
this.scriptLang = scriptLang;
this.scriptType = scriptType;
this.initScriptType = initScriptType;
this.mapScriptType = mapScriptType;
this.combineScriptType = combineScriptType;
this.reduceScriptType = reduceScriptType;
this.initScript = initScript;
this.mapScript = mapScript;
this.combineScript = combineScript;
Expand All @@ -136,8 +142,8 @@ public Factory(String name, String scriptLang, ScriptType scriptType, String ini

@Override
public Aggregator create(AggregationContext context, Aggregator parent, long expectedBucketsCount) {
return new ScriptedMetricAggregator(name, scriptLang, scriptType, initScript, mapScript, combineScript, reduceScript, params,
reduceParams, context, parent);
return new ScriptedMetricAggregator(name, scriptLang, initScriptType, initScript, mapScriptType, mapScript, combineScriptType,
combineScript, reduceScriptType, reduceScript, params, reduceParams, context, parent);
}

}
Expand Down
Expand Up @@ -20,7 +20,6 @@
package org.elasticsearch.search.aggregations.metrics.scripted;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.elasticsearch.search.aggregations.metrics.MetricsAggregationBuilder;

import java.io.IOException;
Expand All @@ -33,11 +32,18 @@ public class ScriptedMetricBuilder extends MetricsAggregationBuilder {

private Map<String, Object> params = null;
private Map<String, Object> reduceParams = null;
private ScriptType scriptType = null;
private String initScript = null;
private String mapScript = null;
private String combineScript = null;
private String reduceScript = null;
private String initScriptFile = null;
private String mapScriptFile = null;
private String combineScriptFile = null;
private String reduceScriptFile = null;
private String initScriptId = null;
private String mapScriptId = null;
private String combineScriptId = null;
private String reduceScriptId = null;
private String lang = null;

/**
Expand Down Expand Up @@ -97,18 +103,74 @@ public ScriptedMetricBuilder reduceScript(String reduceScript) {
}

/**
* Set the script language.
* Set the <tt>init</tt> script file.
*/
public ScriptedMetricBuilder lang(String lang) {
this.lang = lang;
public ScriptedMetricBuilder initScriptFile(String initScriptFile) {
this.initScriptFile = initScriptFile;
return this;
}

/**
* Set the <tt>map</tt> script file.
*/
public ScriptedMetricBuilder mapScriptFile(String mapScriptFile) {
this.mapScriptFile = mapScriptFile;
return this;
}

/**
* Set the <tt>combine</tt> script file.
*/
public ScriptedMetricBuilder combineScriptFile(String combineScriptFile) {
this.combineScriptFile = combineScriptFile;
return this;
}

/**
* Set the <tt>reduce</tt> script file.
*/
public ScriptedMetricBuilder reduceScriptFile(String reduceScriptFile) {
this.reduceScriptFile = reduceScriptFile;
return this;
}

/**
* Set the indexed <tt>init</tt> script id.
*/
public ScriptedMetricBuilder initScriptId(String initScriptId) {
this.initScriptId = initScriptId;
return this;
}

/**
* Set the script type.
* Set the indexed <tt>map</tt> script id.
*/
public ScriptedMetricBuilder scriptType(ScriptType scriptType) {
this.scriptType = scriptType;
public ScriptedMetricBuilder mapScriptId(String mapScriptId) {
this.mapScriptId = mapScriptId;
return this;
}

/**
* Set the indexed <tt>combine</tt> script id.
*/
public ScriptedMetricBuilder combineScriptId(String combineScriptId) {
this.combineScriptId = combineScriptId;
return this;
}

/**
* Set the indexed <tt>reduce</tt> script id.
*/
public ScriptedMetricBuilder reduceScriptId(String reduceScriptId) {
this.reduceScriptId = reduceScriptId;
return this;
}

/**
* Set the script language.
*/
public ScriptedMetricBuilder lang(String lang) {
this.lang = lang;
return this;
}

Expand Down Expand Up @@ -140,12 +202,40 @@ protected void internalXContent(XContentBuilder builder, Params builderParams) t
builder.field(ScriptedMetricParser.REDUCE_SCRIPT_FIELD.getPreferredName(), reduceScript);
}

if (lang != null) {
builder.field(ScriptedMetricParser.LANG_FIELD.getPreferredName(), lang);
if (initScriptFile != null) {
builder.field(ScriptedMetricParser.INIT_SCRIPT_FILE_FIELD.getPreferredName(), initScriptFile);
}

if (mapScriptFile != null) {
builder.field(ScriptedMetricParser.MAP_SCRIPT_FILE_FIELD.getPreferredName(), mapScriptFile);
}

if (scriptType != null) {
builder.field(ScriptedMetricParser.SCRIPT_TYPE_FIELD.getPreferredName(), scriptType.name());
if (combineScriptFile != null) {
builder.field(ScriptedMetricParser.COMBINE_SCRIPT_FILE_FIELD.getPreferredName(), combineScriptFile);
}

if (reduceScriptFile != null) {
builder.field(ScriptedMetricParser.REDUCE_SCRIPT_FILE_FIELD.getPreferredName(), reduceScriptFile);
}

if (initScriptId != null) {
builder.field(ScriptedMetricParser.INIT_SCRIPT_ID_FIELD.getPreferredName(), initScriptId);
}

if (mapScriptId != null) {
builder.field(ScriptedMetricParser.MAP_SCRIPT_ID_FIELD.getPreferredName(), mapScriptId);
}

if (combineScriptId != null) {
builder.field(ScriptedMetricParser.COMBINE_SCRIPT_ID_FIELD.getPreferredName(), combineScriptId);
}

if (reduceScriptId != null) {
builder.field(ScriptedMetricParser.REDUCE_SCRIPT_ID_FIELD.getPreferredName(), reduceScriptId);
}

if (lang != null) {
builder.field(ScriptedMetricParser.LANG_FIELD.getPreferredName(), lang);
}
}

Expand Down

0 comments on commit 6cf3713

Please sign in to comment.