Search in sources :

Example 1 with SingleBucketAggregation

use of org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation in project elasticsearch by elastic.

the class AggregationPath method resolveValue.

/**
     * Resolves the value pointed by this path given an aggregations root
     *
     * @param root  The root that serves as a point of reference for this path
     * @return      The resolved value
     */
public double resolveValue(HasAggregations root) {
    HasAggregations parent = root;
    double value = Double.NaN;
    for (int i = 0; i < pathElements.size(); i++) {
        AggregationPath.PathElement token = pathElements.get(i);
        Aggregation agg = parent.getAggregations().get(token.name);
        if (agg == null) {
            throw new IllegalArgumentException("Invalid order path [" + this + "]. Cannot find aggregation named [" + token.name + "]");
        }
        if (agg instanceof SingleBucketAggregation) {
            if (token.key != null && !token.key.equals("doc_count")) {
                throw new IllegalArgumentException("Invalid order path [" + this + "]. Unknown value key [" + token.key + "] for single-bucket aggregation [" + token.name + "]. Either use [doc_count] as key or drop the key all together");
            }
            parent = (SingleBucketAggregation) agg;
            value = ((SingleBucketAggregation) agg).getDocCount();
            continue;
        }
        // the agg can only be a metrics agg, and a metrics agg must be at the end of the path
        if (i != pathElements.size() - 1) {
            throw new IllegalArgumentException("Invalid order path [" + this + "]. Metrics aggregations cannot have sub-aggregations (at [" + token + ">" + pathElements.get(i + 1) + "]");
        }
        if (agg instanceof InternalNumericMetricsAggregation.SingleValue) {
            if (token.key != null && !token.key.equals("value")) {
                throw new IllegalArgumentException("Invalid order path [" + this + "]. Unknown value key [" + token.key + "] for single-value metric aggregation [" + token.name + "]. Either use [value] as key or drop the key all together");
            }
            parent = null;
            value = ((InternalNumericMetricsAggregation.SingleValue) agg).value();
            continue;
        }
        // we're left with a multi-value metric agg
        if (token.key == null) {
            throw new IllegalArgumentException("Invalid order path [" + this + "]. Missing value key in [" + token + "] which refers to a multi-value metric aggregation");
        }
        parent = null;
        value = ((InternalNumericMetricsAggregation.MultiValue) agg).value(token.key);
    }
    return value;
}
Also used : SingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) InternalNumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation) InternalNumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation) HasAggregations(org.elasticsearch.search.aggregations.HasAggregations) SingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation)

Aggregations

Aggregation (org.elasticsearch.search.aggregations.Aggregation)1 HasAggregations (org.elasticsearch.search.aggregations.HasAggregations)1 SingleBucketAggregation (org.elasticsearch.search.aggregations.bucket.SingleBucketAggregation)1 InternalNumericMetricsAggregation (org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation)1