|
@@ -24,15 +24,17 @@ import org.elasticsearch.script.ExecutableScript;
|
|
|
import org.elasticsearch.script.ScriptService;
|
|
|
import org.elasticsearch.script.ScriptService.ScriptType;
|
|
|
import org.elasticsearch.script.SearchScript;
|
|
|
+import org.elasticsearch.search.SearchParseException;
|
|
|
import org.elasticsearch.search.aggregations.Aggregator;
|
|
|
import org.elasticsearch.search.aggregations.AggregatorFactory;
|
|
|
import org.elasticsearch.search.aggregations.InternalAggregation;
|
|
|
import org.elasticsearch.search.aggregations.metrics.MetricsAggregator;
|
|
|
import org.elasticsearch.search.aggregations.support.AggregationContext;
|
|
|
+import org.elasticsearch.search.internal.SearchContext;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
+import java.util.Map.Entry;
|
|
|
|
|
|
public class ScriptedMetricAggregator extends MetricsAggregator {
|
|
|
|
|
@@ -51,7 +53,7 @@ public class ScriptedMetricAggregator extends MetricsAggregator {
|
|
|
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);
|
|
|
+ super(name, 1, BucketAggregationMode.PER_BUCKET, context, parent);
|
|
|
this.scriptService = context.searchContext().scriptService();
|
|
|
this.scriptLang = scriptLang;
|
|
|
this.reduceScriptType = reduceScriptType;
|
|
@@ -59,7 +61,7 @@ public class ScriptedMetricAggregator extends MetricsAggregator {
|
|
|
this.params = new HashMap<>();
|
|
|
this.params.put("_agg", new HashMap<>());
|
|
|
} else {
|
|
|
- this.params = params;
|
|
|
+ this.params = new HashMap<>(params);
|
|
|
}
|
|
|
if (reduceParams == null) {
|
|
|
this.reduceParams = new HashMap<>();
|
|
@@ -142,9 +144,45 @@ public class ScriptedMetricAggregator extends MetricsAggregator {
|
|
|
|
|
|
@Override
|
|
|
public Aggregator create(AggregationContext context, Aggregator parent, long expectedBucketsCount) {
|
|
|
+ Map<String, Object> params = null;
|
|
|
+ if (this.params != null) {
|
|
|
+ params = deepCopyParams(this.params, context.searchContext());
|
|
|
+ }
|
|
|
+ Map<String, Object> reduceParams = null;
|
|
|
+ if (this.reduceParams != null) {
|
|
|
+ reduceParams = deepCopyParams(this.reduceParams, context.searchContext());
|
|
|
+ }
|
|
|
return new ScriptedMetricAggregator(name, scriptLang, initScriptType, initScript, mapScriptType, mapScript, combineScriptType,
|
|
|
combineScript, reduceScriptType, reduceScript, params, reduceParams, context, parent);
|
|
|
}
|
|
|
+
|
|
|
+ @SuppressWarnings({ "unchecked" })
|
|
|
+ private static <T> T deepCopyParams(T original, SearchContext context) {
|
|
|
+ T clone;
|
|
|
+ if (original instanceof Map) {
|
|
|
+ Map<?, ?> originalMap = (Map<?, ?>) original;
|
|
|
+ Map<Object, Object> clonedMap = new HashMap<>();
|
|
|
+ for (Entry<?, ?> e : originalMap.entrySet()) {
|
|
|
+ clonedMap.put(deepCopyParams(e.getKey(), context), deepCopyParams(e.getValue(), context));
|
|
|
+ }
|
|
|
+ clone = (T) clonedMap;
|
|
|
+ } else if (original instanceof List) {
|
|
|
+ List<?> originalList = (List<?>) original;
|
|
|
+ List<Object> clonedList = new ArrayList<Object>();
|
|
|
+ for (Object o : originalList) {
|
|
|
+ clonedList.add(deepCopyParams(o, context));
|
|
|
+ }
|
|
|
+ clone = (T) clonedList;
|
|
|
+ } else if (original instanceof String || original instanceof Integer || original instanceof Long || original instanceof Short
|
|
|
+ || original instanceof Byte || original instanceof Float || original instanceof Double || original instanceof Character
|
|
|
+ || original instanceof Boolean) {
|
|
|
+ clone = original;
|
|
|
+ } else {
|
|
|
+ throw new SearchParseException(context, "Can only clone primitives, String, ArrayList, and HashMap. Found: "
|
|
|
+ + original.getClass().getCanonicalName());
|
|
|
+ }
|
|
|
+ return clone;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|