Search in sources :

Example 1 with BucketAggregationSelectorScript

use of org.opensearch.script.BucketAggregationSelectorScript in project OpenSearch by opensearch-project.

the class BucketSelectorPipelineAggregator method reduce.

@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg = (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
    List<? extends InternalMultiBucketAggregation.InternalBucket> buckets = originalAgg.getBuckets();
    BucketAggregationSelectorScript.Factory factory = reduceContext.scriptService().compile(script, BucketAggregationSelectorScript.CONTEXT);
    List<InternalMultiBucketAggregation.InternalBucket> newBuckets = new ArrayList<>();
    for (InternalMultiBucketAggregation.InternalBucket 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);
        }
        // TODO: can we use one instance of the script for all buckets? it should be stateless?
        BucketAggregationSelectorScript executableScript = factory.newInstance(vars);
        if (executableScript.execute()) {
            newBuckets.add(bucket);
        }
    }
    return originalAgg.create(newBuckets);
}
Also used : BucketAggregationSelectorScript(org.opensearch.script.BucketAggregationSelectorScript) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalMultiBucketAggregation(org.opensearch.search.aggregations.InternalMultiBucketAggregation) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BucketAggregationSelectorScript (org.opensearch.script.BucketAggregationSelectorScript)1 InternalMultiBucketAggregation (org.opensearch.search.aggregations.InternalMultiBucketAggregation)1