Search in sources :

Example 1 with AggregationExecutionException

use of org.elasticsearch.search.aggregations.AggregationExecutionException in project elasticsearch by elastic.

the class AggregationPath method validate.

/**
     * Validates this path over the given aggregator as a point of reference.
     *
     * @param root  The point of reference of this path
     */
public void validate(Aggregator root) {
    Aggregator aggregator = root;
    for (int i = 0; i < pathElements.size(); i++) {
        aggregator = aggregator.subAggregator(pathElements.get(i).name);
        if (aggregator == null) {
            throw new AggregationExecutionException("Invalid term-aggregator order path [" + this + "]. Unknown aggregation [" + pathElements.get(i).name + "]");
        }
        if (i < pathElements.size() - 1) {
            if (!(aggregator instanceof SingleBucketAggregator)) {
                throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Terms buckets can only be sorted on a sub-aggregator path " + "that is built out of zero or more single-bucket aggregations within the path and a final " + "single-bucket or a metrics aggregation at the path end. Sub-path [" + subPath(0, i + 1) + "] points to non single-bucket aggregation");
            }
            if (pathElements.get(i).key != null) {
                throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Terms buckets can only be sorted on a sub-aggregator path " + "that is built out of zero or more single-bucket aggregations within the path and a " + "final single-bucket or a metrics aggregation at the path end. Sub-path [" + subPath(0, i + 1) + "] points to non single-bucket aggregation");
            }
        }
    }
    boolean singleBucket = aggregator instanceof SingleBucketAggregator;
    if (!singleBucket && !(aggregator instanceof NumericMetricsAggregator)) {
        throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Terms buckets can only be sorted on a sub-aggregator path " + "that is built out of zero or more single-bucket aggregations within the path and a final " + "single-bucket or a metrics aggregation at the path end.");
    }
    AggregationPath.PathElement lastToken = lastPathElement();
    if (singleBucket) {
        if (lastToken.key != null && !"doc_count".equals(lastToken.key)) {
            throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Ordering on a single-bucket aggregation can only be done on its doc_count. " + "Either drop the key (a la \"" + lastToken.name + "\") or change it to \"doc_count\" (a la \"" + lastToken.name + ".doc_count\")");
        }
        // perfectly valid to sort on single-bucket aggregation (will be sored on its doc_count)
        return;
    }
    if (aggregator instanceof NumericMetricsAggregator.SingleValue) {
        if (lastToken.key != null && !"value".equals(lastToken.key)) {
            throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Ordering on a single-value metrics aggregation can only be done on its value. " + "Either drop the key (a la \"" + lastToken.name + "\") or change it to \"value\" (a la \"" + lastToken.name + ".value\")");
        }
        // perfectly valid to sort on single metric aggregation (will be sorted on its associated value)
        return;
    }
    // the aggregator must be of a multi-value metrics type
    if (lastToken.key == null) {
        throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. When ordering on a multi-value metrics aggregation a metric name must be specified");
    }
    if (!((NumericMetricsAggregator.MultiValue) aggregator).hasMetric(lastToken.key)) {
        throw new AggregationExecutionException("Invalid terms aggregation order path [" + this + "]. Unknown metric name [" + lastToken.key + "] on multi-value metrics aggregation [" + lastToken.name + "]");
    }
}
Also used : NumericMetricsAggregator(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator) Aggregator(org.elasticsearch.search.aggregations.Aggregator) SingleBucketAggregator(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator) NumericMetricsAggregator(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator) SingleBucketAggregator(org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException)

Example 2 with AggregationExecutionException

use of org.elasticsearch.search.aggregations.AggregationExecutionException in project elasticsearch by elastic.

the class BucketHelpers method resolveBucketValue.

public static Double resolveBucketValue(MultiBucketsAggregation agg, InternalMultiBucketAggregation.Bucket bucket, List<String> aggPathAsList, GapPolicy gapPolicy) {
    try {
        Object propertyValue = bucket.getProperty(agg.getName(), aggPathAsList);
        if (propertyValue == null) {
            throw new AggregationExecutionException(AbstractPipelineAggregationBuilder.BUCKETS_PATH_FIELD.getPreferredName() + " must reference either a number value or a single value numeric metric aggregation");
        } else {
            double value;
            if (propertyValue instanceof Number) {
                value = ((Number) propertyValue).doubleValue();
            } else if (propertyValue instanceof InternalNumericMetricsAggregation.SingleValue) {
                value = ((InternalNumericMetricsAggregation.SingleValue) propertyValue).value();
            } else {
                throw new AggregationExecutionException(AbstractPipelineAggregationBuilder.BUCKETS_PATH_FIELD.getPreferredName() + " must reference either a number value or a single value numeric metric aggregation, got: " + propertyValue.getClass().getCanonicalName());
            }
            // doc count never has missing values so gap policy doesn't apply here
            boolean isDocCountProperty = aggPathAsList.size() == 1 && "_count".equals(aggPathAsList.get(0));
            if (Double.isInfinite(value) || Double.isNaN(value) || (bucket.getDocCount() == 0 && !isDocCountProperty)) {
                switch(gapPolicy) {
                    case INSERT_ZEROS:
                        return 0.0;
                    case SKIP:
                    default:
                        return Double.NaN;
                }
            } else {
                return value;
            }
        }
    } catch (InvalidAggregationPathException e) {
        return null;
    }
}
Also used : InternalNumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation) InvalidAggregationPathException(org.elasticsearch.search.aggregations.InvalidAggregationPathException) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException)

Example 3 with AggregationExecutionException

use of org.elasticsearch.search.aggregations.AggregationExecutionException in project elasticsearch by elastic.

the class BucketScriptPipelineAggregator method reduce.

@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket> originalAgg = (InternalMultiBucketAggregation<InternalMultiBucketAggregation, InternalMultiBucketAggregation.InternalBucket>) aggregation;
    List<? extends Bucket> buckets = originalAgg.getBuckets();
    CompiledScript compiledScript = reduceContext.scriptService().compile(script, ScriptContext.Standard.AGGS);
    List newBuckets = new ArrayList<>();
    for (Bucket bucket : buckets) {
        Map<String, Object> vars = new HashMap<>();
        if (script.getParams() != null) {
            vars.putAll(script.getParams());
        }
        boolean skipBucket = false;
        for (Map.Entry<String, String> entry : bucketsPathsMap.entrySet()) {
            String varName = entry.getKey();
            String bucketsPath = entry.getValue();
            Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy);
            if (GapPolicy.SKIP == gapPolicy && (value == null || Double.isNaN(value))) {
                skipBucket = true;
                break;
            }
            vars.put(varName, value);
        }
        if (skipBucket) {
            newBuckets.add(bucket);
        } else {
            ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars);
            Object returned = executableScript.run();
            if (returned == null) {
                newBuckets.add(bucket);
            } else {
                if (!(returned instanceof Number)) {
                    throw new AggregationExecutionException("series_arithmetic script for reducer [" + name() + "] must return a Number");
                }
                final List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                    return (InternalAggregation) p;
                }).collect(Collectors.toList());
                aggs.add(new InternalSimpleValue(name(), ((Number) returned).doubleValue(), formatter, new ArrayList<>(), metaData()));
                InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs), (InternalMultiBucketAggregation.InternalBucket) bucket);
                newBuckets.add(newBucket);
            }
        }
    }
    return originalAgg.create(newBuckets);
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) HashMap(java.util.HashMap) DocValueFormat(org.elasticsearch.search.DocValueFormat) ScriptContext(org.elasticsearch.script.ScriptContext) ArrayList(java.util.ArrayList) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) BucketHelpers.resolveBucketValue(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) Map(java.util.Map) GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) StreamSupport(java.util.stream.StreamSupport) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException) Script(org.elasticsearch.script.Script) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) ReduceContext(org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext) List(java.util.List) CompiledScript(org.elasticsearch.script.CompiledScript) InternalSimpleValue(org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue) StreamInput(org.elasticsearch.common.io.stream.StreamInput) ExecutableScript(org.elasticsearch.script.ExecutableScript) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutableScript(org.elasticsearch.script.ExecutableScript) ArrayList(java.util.ArrayList) List(java.util.List) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalSimpleValue(org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with AggregationExecutionException

use of org.elasticsearch.search.aggregations.AggregationExecutionException in project elasticsearch by elastic.

the class SignificantTermsAggregatorFactory method doCreateInternal.

@Override
protected Aggregator doCreateInternal(ValuesSource valuesSource, Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException {
    if (collectsFromSingleBucket == false) {
        return asMultiBucketAggregator(this, context, parent);
    }
    numberOfAggregatorsCreated++;
    BucketCountThresholds bucketCountThresholds = new BucketCountThresholds(this.bucketCountThresholds);
    if (bucketCountThresholds.getShardSize() == SignificantTermsAggregationBuilder.DEFAULT_BUCKET_COUNT_THRESHOLDS.getShardSize()) {
        // The user has not made a shardSize selection .
        // Use default heuristic to avoid any wrong-ranking caused by
        // distributed counting
        // but request double the usual amount.
        // We typically need more than the number of "top" terms requested
        // by other aggregations
        // as the significance algorithm is in less of a position to
        // down-select at shard-level -
        // some of the things we want to find have only one occurrence on
        // each shard and as
        // such are impossible to differentiate from non-significant terms
        // at that early stage.
        bucketCountThresholds.setShardSize(2 * BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize(), context.numberOfShards()));
    }
    if (valuesSource instanceof ValuesSource.Bytes) {
        ExecutionMode execution = null;
        if (executionHint != null) {
            execution = ExecutionMode.fromString(executionHint);
        }
        if (!(valuesSource instanceof ValuesSource.Bytes.WithOrdinals)) {
            execution = ExecutionMode.MAP;
        }
        if (execution == null) {
            if (Aggregator.descendsFromBucketAggregator(parent)) {
                execution = ExecutionMode.GLOBAL_ORDINALS_HASH;
            } else {
                execution = ExecutionMode.GLOBAL_ORDINALS;
            }
        }
        assert execution != null;
        DocValueFormat format = config.format();
        if ((includeExclude != null) && (includeExclude.isRegexBased()) && format != DocValueFormat.RAW) {
            throw new AggregationExecutionException("Aggregation [" + name + "] cannot support regular expression style include/exclude " + "settings as they can only be applied to string fields. Use an array of values for include/exclude clauses");
        }
        return execution.create(name, factories, valuesSource, format, bucketCountThresholds, includeExclude, context, parent, significanceHeuristic, this, pipelineAggregators, metaData);
    }
    if ((includeExclude != null) && (includeExclude.isRegexBased())) {
        throw new AggregationExecutionException("Aggregation [" + name + "] cannot support regular expression style include/exclude " + "settings as they can only be applied to string fields. Use an array of numeric values for include/exclude clauses used to filter numeric fields");
    }
    if (valuesSource instanceof ValuesSource.Numeric) {
        if (((ValuesSource.Numeric) valuesSource).isFloatingPoint()) {
            throw new UnsupportedOperationException("No support for examining floating point numerics");
        }
        IncludeExclude.LongFilter longFilter = null;
        if (includeExclude != null) {
            longFilter = includeExclude.convertToLongFilter(config.format());
        }
        return new SignificantLongTermsAggregator(name, factories, (ValuesSource.Numeric) valuesSource, config.format(), bucketCountThresholds, context, parent, significanceHeuristic, this, longFilter, pipelineAggregators, metaData);
    }
    throw new AggregationExecutionException("significant_terms aggregation cannot be applied to field [" + config.fieldContext().field() + "]. It can only be applied to numeric or string fields.");
}
Also used : BucketCountThresholds(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator.BucketCountThresholds) DocValueFormat(org.elasticsearch.search.DocValueFormat) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) ValuesSource(org.elasticsearch.search.aggregations.support.ValuesSource) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException)

Example 5 with AggregationExecutionException

use of org.elasticsearch.search.aggregations.AggregationExecutionException in project elasticsearch by elastic.

the class InternalTerms method doReduce.

@Override
public InternalAggregation doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    Map<Object, List<B>> buckets = new HashMap<>();
    long sumDocCountError = 0;
    long otherDocCount = 0;
    InternalTerms<A, B> referenceTerms = null;
    for (InternalAggregation aggregation : aggregations) {
        @SuppressWarnings("unchecked") InternalTerms<A, B> terms = (InternalTerms<A, B>) aggregation;
        if (referenceTerms == null && !aggregation.getClass().equals(UnmappedTerms.class)) {
            referenceTerms = terms;
        }
        if (referenceTerms != null && !referenceTerms.getClass().equals(terms.getClass()) && !terms.getClass().equals(UnmappedTerms.class)) {
            // is of different types in different indices.
            throw new AggregationExecutionException("Merging/Reducing the aggregations failed when computing the aggregation [" + referenceTerms.getName() + "] because the field you gave in the aggregation query existed as two different " + "types in two different indices");
        }
        otherDocCount += terms.getSumOfOtherDocCounts();
        final long thisAggDocCountError;
        if (terms.getBucketsInternal().size() < getShardSize() || InternalOrder.isTermOrder(order)) {
            thisAggDocCountError = 0;
        } else if (InternalOrder.isCountDesc(this.order)) {
            if (terms.getDocCountError() > 0) {
                // If there is an existing docCountError for this agg then
                // use this as the error for this aggregation
                thisAggDocCountError = terms.getDocCountError();
            } else {
                // otherwise use the doc count of the last term in the
                // aggregation
                thisAggDocCountError = terms.getBucketsInternal().get(terms.getBucketsInternal().size() - 1).docCount;
            }
        } else {
            thisAggDocCountError = -1;
        }
        if (sumDocCountError != -1) {
            if (thisAggDocCountError == -1) {
                sumDocCountError = -1;
            } else {
                sumDocCountError += thisAggDocCountError;
            }
        }
        setDocCountError(thisAggDocCountError);
        for (B bucket : terms.getBucketsInternal()) {
            // If there is already a doc count error for this bucket
            // subtract this aggs doc count error from it to make the
            // new value for the bucket. This then means that when the
            // final error for the bucket is calculated below we account
            // for the existing error calculated in a previous reduce.
            // Note that if the error is unbounded (-1) this will be fixed
            // later in this method.
            bucket.docCountError -= thisAggDocCountError;
            List<B> bucketList = buckets.get(bucket.getKey());
            if (bucketList == null) {
                bucketList = new ArrayList<>();
                buckets.put(bucket.getKey(), bucketList);
            }
            bucketList.add(bucket);
        }
    }
    final int size = reduceContext.isFinalReduce() == false ? buckets.size() : Math.min(requiredSize, buckets.size());
    final BucketPriorityQueue<B> ordered = new BucketPriorityQueue<>(size, order.comparator(null));
    for (List<B> sameTermBuckets : buckets.values()) {
        final B b = sameTermBuckets.get(0).reduce(sameTermBuckets, reduceContext);
        if (sumDocCountError == -1) {
            b.docCountError = -1;
        } else {
            b.docCountError += sumDocCountError;
        }
        if (b.docCount >= minDocCount || reduceContext.isFinalReduce() == false) {
            B removed = ordered.insertWithOverflow(b);
            if (removed != null) {
                otherDocCount += removed.getDocCount();
            }
        }
    }
    B[] list = createBucketsArray(ordered.size());
    for (int i = ordered.size() - 1; i >= 0; i--) {
        list[i] = ordered.pop();
    }
    long docCountError;
    if (sumDocCountError == -1) {
        docCountError = -1;
    } else {
        docCountError = aggregations.size() == 1 ? 0 : sumDocCountError;
    }
    return create(name, Arrays.asList(list), docCountError, otherDocCount);
}
Also used : HashMap(java.util.HashMap) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) BucketPriorityQueue(org.elasticsearch.search.aggregations.bucket.terms.support.BucketPriorityQueue) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) List(java.util.List) AggregationExecutionException(org.elasticsearch.search.aggregations.AggregationExecutionException)

Aggregations

AggregationExecutionException (org.elasticsearch.search.aggregations.AggregationExecutionException)7 DocValueFormat (org.elasticsearch.search.DocValueFormat)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)2 BucketCountThresholds (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator.BucketCountThresholds)2 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)2 ValuesSource (org.elasticsearch.search.aggregations.support.ValuesSource)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 StreamInput (org.elasticsearch.common.io.stream.StreamInput)1 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)1 ObjectMapper (org.elasticsearch.index.mapper.ObjectMapper)1 NestedScope (org.elasticsearch.index.query.support.NestedScope)1