Search in sources :

Example 1 with MultiBucketsAggregation

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

the class CumulativeSumPipelineAggregator method reduce.

@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    MultiBucketsAggregation histo = (MultiBucketsAggregation) aggregation;
    List<? extends Bucket> buckets = histo.getBuckets();
    HistogramFactory factory = (HistogramFactory) histo;
    List<Bucket> newBuckets = new ArrayList<>();
    double sum = 0;
    for (Bucket bucket : buckets) {
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], GapPolicy.INSERT_ZEROS);
        sum += thisBucketValue;
        List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
            return (InternalAggregation) p;
        }).collect(Collectors.toList());
        aggs.add(new InternalSimpleValue(name(), sum, formatter, new ArrayList<PipelineAggregator>(), metaData()));
        Bucket newBucket = factory.createBucket(factory.getKey(bucket), bucket.getDocCount(), new InternalAggregations(aggs));
        newBuckets.add(newBucket);
    }
    return factory.createAggregation(newBuckets);
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) IOException(java.io.IOException) DocValueFormat(org.elasticsearch.search.DocValueFormat) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) ReduceContext(org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext) List(java.util.List) BucketHelpers.resolveBucketValue(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) InternalSimpleValue(org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) StreamSupport(java.util.stream.StreamSupport) HistogramFactory(org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory) ArrayList(java.util.ArrayList) HistogramFactory(org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory) 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) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)

Example 2 with MultiBucketsAggregation

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

the class DerivativePipelineAggregator method reduce.

@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    MultiBucketsAggregation histo = (MultiBucketsAggregation) aggregation;
    List<? extends Bucket> buckets = histo.getBuckets();
    HistogramFactory factory = (HistogramFactory) histo;
    List<Bucket> newBuckets = new ArrayList<>();
    Number lastBucketKey = null;
    Double lastBucketValue = null;
    for (Bucket bucket : buckets) {
        Number thisBucketKey = factory.getKey(bucket);
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], gapPolicy);
        if (lastBucketValue != null && thisBucketValue != null) {
            double gradient = thisBucketValue - lastBucketValue;
            double xDiff = -1;
            if (xAxisUnits != null) {
                xDiff = (thisBucketKey.doubleValue() - lastBucketKey.doubleValue()) / xAxisUnits;
            }
            final List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                return (InternalAggregation) p;
            }).collect(Collectors.toList());
            aggs.add(new InternalDerivative(name(), gradient, xDiff, formatter, new ArrayList<PipelineAggregator>(), metaData()));
            Bucket newBucket = factory.createBucket(factory.getKey(bucket), bucket.getDocCount(), new InternalAggregations(aggs));
            newBuckets.add(newBucket);
        } else {
            newBuckets.add(bucket);
        }
        lastBucketKey = thisBucketKey;
        lastBucketValue = thisBucketValue;
    }
    return factory.createAggregation(newBuckets);
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) IOException(java.io.IOException) DocValueFormat(org.elasticsearch.search.DocValueFormat) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) ReduceContext(org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext) List(java.util.List) BucketHelpers.resolveBucketValue(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) StreamSupport(java.util.stream.StreamSupport) HistogramFactory(org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory) ArrayList(java.util.ArrayList) HistogramFactory(org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)

Example 3 with MultiBucketsAggregation

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

the class MovAvgPipelineAggregator method reduce.

@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    MultiBucketsAggregation histo = (MultiBucketsAggregation) aggregation;
    List<? extends Bucket> buckets = histo.getBuckets();
    HistogramFactory factory = (HistogramFactory) histo;
    List<Bucket> newBuckets = new ArrayList<>();
    EvictingQueue<Double> values = new EvictingQueue<>(this.window);
    Number lastValidKey = 0;
    int lastValidPosition = 0;
    int counter = 0;
    // Do we need to fit the model parameters to the data?
    if (minimize) {
        assert (model.canBeMinimized());
        model = minimize(buckets, histo, model);
    }
    for (Bucket bucket : buckets) {
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], gapPolicy);
        // Default is to reuse existing bucket.  Simplifies the rest of the logic,
        // since we only change newBucket if we can add to it
        Bucket newBucket = bucket;
        if (!(thisBucketValue == null || thisBucketValue.equals(Double.NaN))) {
            // Some models (e.g. HoltWinters) have certain preconditions that must be met
            if (model.hasValue(values.size())) {
                double movavg = model.next(values);
                List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                    return (InternalAggregation) p;
                }).collect(Collectors.toList());
                aggs.add(new InternalSimpleValue(name(), movavg, formatter, new ArrayList<PipelineAggregator>(), metaData()));
                newBucket = factory.createBucket(factory.getKey(bucket), bucket.getDocCount(), new InternalAggregations(aggs));
            }
            if (predict > 0) {
                lastValidKey = factory.getKey(bucket);
                lastValidPosition = counter;
            }
            values.offer(thisBucketValue);
        }
        counter += 1;
        newBuckets.add(newBucket);
    }
    if (buckets.size() > 0 && predict > 0) {
        double[] predictions = model.predict(values, predict);
        for (int i = 0; i < predictions.length; i++) {
            List<InternalAggregation> aggs;
            Number newKey = factory.nextKey(lastValidKey);
            if (lastValidPosition + i + 1 < newBuckets.size()) {
                Bucket bucket = newBuckets.get(lastValidPosition + i + 1);
                // Get the existing aggs in the bucket so we don't clobber data
                aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                    return (InternalAggregation) p;
                }).collect(Collectors.toList());
                aggs.add(new InternalSimpleValue(name(), predictions[i], formatter, new ArrayList<PipelineAggregator>(), metaData()));
                Bucket newBucket = factory.createBucket(newKey, 0, new InternalAggregations(aggs));
                // Overwrite the existing bucket with the new version
                newBuckets.set(lastValidPosition + i + 1, newBucket);
            } else {
                // Not seen before, create fresh
                aggs = new ArrayList<>();
                aggs.add(new InternalSimpleValue(name(), predictions[i], formatter, new ArrayList<PipelineAggregator>(), metaData()));
                Bucket newBucket = factory.createBucket(newKey, 0, new InternalAggregations(aggs));
                // Since this is a new bucket, simply append it
                newBuckets.add(newBucket);
            }
            lastValidKey = newKey;
        }
    }
    return factory.createAggregation(newBuckets);
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) ListIterator(java.util.ListIterator) IOException(java.io.IOException) DocValueFormat(org.elasticsearch.search.DocValueFormat) MovAvgModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.MovAvgModel) Collectors(java.util.stream.Collectors) EvictingQueue(org.elasticsearch.common.collect.EvictingQueue) ArrayList(java.util.ArrayList) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) ReduceContext(org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext) List(java.util.List) BucketHelpers.resolveBucketValue(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) InternalSimpleValue(org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) StreamSupport(java.util.stream.StreamSupport) HistogramFactory(org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory) ArrayList(java.util.ArrayList) HistogramFactory(org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory) 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) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) EvictingQueue(org.elasticsearch.common.collect.EvictingQueue)

Example 4 with MultiBucketsAggregation

use of org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation in project metasfresh-webui-api by metasfresh.

the class KPIDataLoader method loadData.

private void loadData(final KPIDataResult.Builder data, final TimeRange timeRange) {
    logger.trace("Loading data for {}", timeRange);
    // 
    // Create query evaluation context
    final Evaluatee evalCtx = Evaluatees.mapBuilder().put("MainFromMillis", data.getRange().getFromMillis()).put("MainToMillis", data.getRange().getToMillis()).put("FromMillis", timeRange.getFromMillis()).put("ToMillis", timeRange.getToMillis()).build().andComposeWith(Evaluatees.ofCtx(Env.getCtx()));
    // 
    // Resolve esQuery's variables
    final IStringExpression esQuery = kpi.getESQuery();
    final String esQueryParsed = esQuery.evaluate(evalCtx, OnVariableNotFound.Preserve);
    // 
    // Execute the query
    final SearchResponse response;
    try {
        logger.trace("Executing: \n{}", esQueryParsed);
        response = elasticsearchClient.prepareSearch(kpi.getESSearchIndex()).setTypes(kpi.getESSearchTypes()).setSource(esQueryParsed).get();
        logger.trace("Got response: \n{}", response);
    } catch (final NoNodeAvailableException e) {
        // elastic search transport error => nothing to do about it
        throw e;
    } catch (final Exception e) {
        throw new AdempiereException("Failed executing query for " + this + ": " + e.getLocalizedMessage() + "\nQuery: " + esQueryParsed, e);
    }
    // Fetch data
    try {
        final List<Aggregation> aggregations = response.getAggregations().asList();
        for (final Aggregation agg : aggregations) {
            if (agg instanceof MultiBucketsAggregation) {
                final String aggName = agg.getName();
                final MultiBucketsAggregation multiBucketsAggregation = (MultiBucketsAggregation) agg;
                for (final Bucket bucket : multiBucketsAggregation.getBuckets()) {
                    final Object key = dataSetValueKeyExtractor.apply(bucket, timeRange);
                    for (final KPIField field : kpi.getFields()) {
                        final Object value = field.getBucketValueExtractor().extractValue(aggName, bucket);
                        final Object jsonValue = formatValue(field, value);
                        if (jsonValue == null) {
                            continue;
                        }
                        final String fieldName = fieldNameExtractor.apply(field, timeRange);
                        data.putValue(aggName, key, fieldName, jsonValue);
                    }
                    // 
                    // Make sure the groupByField's value is present in our dataSet value.
                    // If not exist, we can use the key as it's value.
                    final KPIField groupByField = kpi.getGroupByFieldOrNull();
                    if (groupByField != null) {
                        data.putValueIfAbsent(aggName, key, groupByField.getFieldName(), key);
                    }
                }
            } else if (agg instanceof NumericMetricsAggregation.SingleValue) {
                final NumericMetricsAggregation.SingleValue singleValueAggregation = (NumericMetricsAggregation.SingleValue) agg;
                // N/A
                final String key = "NO_KEY";
                for (final KPIField field : kpi.getFields()) {
                    final Object value;
                    if ("value".equals(field.getESPathAsString())) {
                        value = singleValueAggregation.value();
                    } else {
                        throw new IllegalStateException("Only ES path ending with 'value' allowed for field: " + field);
                    }
                    final Object jsonValue = field.convertValueToJson(value);
                    data.putValue(agg.getName(), key, field.getFieldName(), jsonValue);
                }
            } else {
                new AdempiereException("Aggregation type not supported: " + agg.getClass()).throwIfDeveloperModeOrLogWarningElse(logger);
            }
        }
    } catch (final Exception e) {
        throw new AdempiereException(e.getLocalizedMessage() + "\n KPI: " + this + "\n Query: " + esQueryParsed + "\n Response: " + response, e);
    }
}
Also used : NumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) AdempiereException(org.adempiere.exceptions.AdempiereException) Evaluatee(org.compiere.util.Evaluatee) SearchResponse(org.elasticsearch.action.search.SearchResponse) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) NumericMetricsAggregation(org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) AdempiereException(org.adempiere.exceptions.AdempiereException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) IStringExpression(org.adempiere.ad.expression.api.IStringExpression)

Example 5 with MultiBucketsAggregation

use of org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation in project vertigo by KleeGroup.

the class ESFacetedQueryResultBuilder method createCluster.

private Map<FacetValue, DtList<I>> createCluster(final Map<String, I> dtcIndex, final Map<I, Map<DtField, String>> resultHighlights) {
    final Map<FacetValue, DtList<I>> resultCluster = new LinkedHashMap<>();
    final FacetDefinition facetDefinition = searchQuery.getClusteringFacetDefinition();
    final Aggregation facetAggregation = queryResponse.getAggregations().get(facetDefinition.getName());
    if (facetDefinition.isRangeFacet()) {
        // Cas des facettes par 'range'
        final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
        for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
            final Bucket value = getBucketByKey(multiBuckets, facetRange.getListFilter().getFilterValue());
            populateCluster(value, facetRange, resultCluster, dtcIndex, resultHighlights);
        }
    } else {
        // Cas des facettes par 'term'
        final MultiBucketsAggregation multiBuckets = (MultiBucketsAggregation) facetAggregation;
        FacetValue facetValue;
        for (final Bucket bucket : multiBuckets.getBuckets()) {
            final String term = bucket.getKeyAsString();
            final String query = facetDefinition.getDtField().getName() + ":\"" + term + "\"";
            final MessageText label = MessageText.of(term);
            facetValue = new FacetValue(term, ListFilter.of(query), label);
            populateCluster(bucket, facetValue, resultCluster, dtcIndex, resultHighlights);
        }
    }
    return resultCluster;
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) FacetValue(io.vertigo.dynamo.collections.model.FacetValue) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) MessageText(io.vertigo.core.locale.MessageText) FacetDefinition(io.vertigo.dynamo.collections.metamodel.FacetDefinition) DtList(io.vertigo.dynamo.domain.model.DtList) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)9 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)7 ArrayList (java.util.ArrayList)6 Aggregation (org.elasticsearch.search.aggregations.Aggregation)5 FacetDefinition (io.vertigo.dynamo.collections.metamodel.FacetDefinition)4 IOException (java.io.IOException)4 List (java.util.List)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 StreamSupport (java.util.stream.StreamSupport)4 StreamInput (org.elasticsearch.common.io.stream.StreamInput)4 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)4 DocValueFormat (org.elasticsearch.search.DocValueFormat)4 InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)4 ReduceContext (org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext)4 InternalAggregations (org.elasticsearch.search.aggregations.InternalAggregations)4 HistogramFactory (org.elasticsearch.search.aggregations.bucket.histogram.HistogramFactory)4 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)4 BucketHelpers.resolveBucketValue (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue)4 PipelineAggregator (org.elasticsearch.search.aggregations.pipeline.PipelineAggregator)4