@Override public InternalAggregation buildEmptyAggregation() { return new InternalAvg(name, 0.0, 0L, format, pipelineAggregators(), metaData()); }
@Override protected AvgAggregatorFactory innerBuild(SearchContext context, ValuesSourceConfig<Numeric> config, AggregatorFactory<?> parent, Builder subFactoriesBuilder) throws IOException { return new AvgAggregatorFactory(name, config, context, parent, subFactoriesBuilder, metaData); }
/** * Create a new {@link Avg} aggregation with the given name. */ public static AvgAggregationBuilder avg(String name) { return new AvgAggregationBuilder(name); }
@Override public InternalAggregation buildAggregation(long bucket) { if (valuesSource == null || bucket >= sums.size()) { return buildEmptyAggregation(); } return new InternalAvg(name, sums.get(bucket), counts.get(bucket), format, pipelineAggregators(), metaData()); }
@Override public InternalAvg doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) { long count = 0; double sum = 0; double compensation = 0; // Compute the sum of double values with Kahan summation algorithm which is more // accurate than naive summation. for (InternalAggregation aggregation : aggregations) { InternalAvg avg = (InternalAvg) aggregation; count += avg.count; if (Double.isFinite(avg.sum) == false) { sum += avg.sum; } else if (Double.isFinite(sum)) { double corrected = avg.sum - compensation; double newSum = sum + corrected; compensation = (newSum - sum) - corrected; sum = newSum; } } return new InternalAvg(getName(), sum, count, format, pipelineAggregators(), getMetaData()); }
@Override public double value() { return getValue(); }
@Override protected Aggregator createUnmapped(Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { return new AvgAggregator(name, null, config.format(), context, parent, pipelineAggregators, metaData); }
/** * Get aggregation results for each class as sum, avg and doc count. * @param aggr Terms aggregation object * @param classes Classes to be taken into account * @return HashMap with required aggregations */ private static HashMap<String, HashMap<String, Double>> getAggregationByClass(Terms aggr, List<String> classes) { HashMap<String, HashMap<String, Double>> retMap = new HashMap<>(); for (Terms.Bucket bucket : aggr.getBuckets()) { String key = bucket.getKeyAsString(); if (!classes.contains(key)) { continue; } long docCount = bucket.getDocCount(); Sum sum = bucket.getAggregations().get("sum_probability"); Avg avg = bucket.getAggregations().get("avg_probability"); HashMap<String, Double> map = new HashMap<>(); map.put("count", (double) docCount); map.put("sum", sum.getValue()); map.put("avg", avg.getValue()); retMap.put(key, map); } return retMap; }
@Override public double getValue() { return value(); }
public static ParsedAvg fromXContent(XContentParser parser, final String name) { ParsedAvg avg = PARSER.apply(parser, null); avg.setName(name); return avg; } }
/** * Get AggregationBuilder to calculate sum, avg and doc count for given classifier. * @param classifierName Name of classifier * @return AggregationBuilder with required configuration */ private static TermsBuilder getClassifierAggregationBuilder(String classifierName) { String probabilityField = classifierName + "_probability"; return AggregationBuilders.terms("by_class").field(classifierName) .subAggregation( AggregationBuilders.avg("avg_probability").field(probabilityField) ) .subAggregation( AggregationBuilders.sum("sum_probability").field(probabilityField) ); }
@Override public InternalAggregation buildEmptyAggregation() { return new InternalAvg(name, 0.0, 0L, format, pipelineAggregators(), metaData()); }
@Override public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { builder.field(CommonFields.VALUE.getPreferredName(), count != 0 ? getValue() : null); if (count != 0 && format != DocValueFormat.RAW) { builder.field(CommonFields.VALUE_AS_STRING.getPreferredName(), format.format(getValue()).toString()); } return builder; }
@Override protected Aggregator doCreateInternal(ValuesSource.Numeric valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { return new AvgAggregator(name, valuesSource, config.format(), context, parent, pipelineAggregators, metaData); } }
@Override protected AggregationBuilder shallowCopy(Builder factoriesBuilder, Map<String, Object> metaData) { return new AvgAggregationBuilder(this, factoriesBuilder, metaData); }
break; case Expression.AVG: aggregationBuilder = AggregationBuilders.avg(function.toParsedText()).field(jPAColumnName); break; case Expression.COUNT:
@Override public InternalAggregation buildEmptyAggregation() { return new InternalAvg(name, 0.0, 0L, format, pipelineAggregators(), metaData()); }
/** * Gets the aggregated result. * * @param internalAggs * the internal aggs * @param identifier * the identifier * @param exp * the exp * @return the aggregated result */ private Object getAggregatedResult(InternalAggregations internalAggs, String identifier, Expression exp) { switch (identifier) { case Expression.MIN: return (((InternalMin) internalAggs.get(exp.toParsedText())).getValue()); case Expression.MAX: return (((InternalMax) internalAggs.get(exp.toParsedText())).getValue()); case Expression.AVG: return (((InternalAvg) internalAggs.get(exp.toParsedText())).getValue()); case Expression.SUM: return (((InternalSum) internalAggs.get(exp.toParsedText())).getValue()); case Expression.COUNT: return (((InternalValueCount) internalAggs.get(exp.toParsedText())).getValue()); } throw new KunderaException("No support for " + identifier + " aggregation."); }
public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException { return PARSER.parse(parser, new AvgAggregationBuilder(aggregationName), null); }
@Override public InternalAggregation buildEmptyAggregation() { return new InternalAvg(name, 0.0, 0L, format, pipelineAggregators(), metaData()); }