org.elasticsearch.search.aggregations.InternalMultiBucketAggregation Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.InternalMultiBucketAggregation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BucketMetricsPipelineAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public final InternalAggregation doReduce(Aggregations aggregations, ReduceContext context) {
    preCollection();
    List<String> bucketsPath = AggregationPath.parse(bucketsPaths()[0]).getPathElementsAsStringList();
    for (Aggregation aggregation : aggregations) {
        if (aggregation.getName().equals(bucketsPath.get(0))) {
            bucketsPath = bucketsPath.subList(1, bucketsPath.size());
            InternalMultiBucketAggregation multiBucketsAgg = (InternalMultiBucketAggregation) aggregation;
            List<? extends Bucket> buckets = multiBucketsAgg.getBuckets();
            for (int i = 0; i < buckets.size(); i++) {
                Bucket bucket = buckets.get(i);
                Double bucketValue = BucketHelpers.resolveBucketValue(multiBucketsAgg, bucket, bucketsPath, gapPolicy);
                if (bucketValue != null && !Double.isNaN(bucketValue)) {
                    collectBucketValue(bucket.getKeyAsString(), bucketValue);
                }
            }
        }
    }
    return buildAggregation(Collections.EMPTY_LIST, metaData());
}
 
Example #2
Source File: BucketHelpers.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Double resolveBucketValue(InternalMultiBucketAggregation<?, ? extends InternalMultiBucketAggregation.Bucket> agg,
        InternalMultiBucketAggregation.Bucket bucket, List<String> aggPathAsList, GapPolicy gapPolicy) {
    try {
        Object propertyValue = bucket.getProperty(agg.getName(), aggPathAsList);
        if (propertyValue == null) {
            throw new AggregationExecutionException(DerivativeParser.BUCKETS_PATH.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(DerivativeParser.BUCKETS_PATH.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;
    }
}
 
Example #3
Source File: BucketSelectorPipelineAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@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, reduceContext, Collections.<String, String>emptyMap());
    List newBuckets = new ArrayList<>();
    for (Bucket 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);
        }
        ExecutableScript executableScript = reduceContext.scriptService().executable(compiledScript, vars);
        Object scriptReturnValue = executableScript.run();
        final boolean keepBucket;
        // TODO: WTF!!!!!
        if ("expression".equals(script.getLang())) {
            double scriptDoubleValue = (double) scriptReturnValue;
            keepBucket = scriptDoubleValue == 1.0;
        } else {
            keepBucket = (boolean) scriptReturnValue;
        }
        if (keepBucket) {
            newBuckets.add(bucket);
        }
    }
    return originalAgg.create(newBuckets);
}
 
Example #4
Source File: TransportBasedClient.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private void setAggregations(Aggregations aggregations, ActionResponse actionResp) {
  // Only the result of the first aggregation is returned
  //
  final Aggregation agg = aggregations.asList().get(0);

  if (agg instanceof InternalMetricsAggregation) {
    actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE,
        XContentHelper.toString((InternalMetricsAggregation) agg).toString()));
  } else if (agg instanceof InternalSingleBucketAggregation) {
    actionResp.addAggregation(new AggWrapper(AggWrapper.AggregationType.SIMPLE,
        XContentHelper.toString((InternalSingleBucketAggregation) agg).toString()));
  } else if (agg instanceof InternalMultiBucketAggregation) {
    final Set<String> headerKeys = new HashSet<>();
    final List<Map<String, Object>> buckets = new LinkedList<>();
    final InternalMultiBucketAggregation multiBucketAgg = (InternalMultiBucketAggregation) agg;

    for (final MultiBucketsAggregation.Bucket bucket : multiBucketAgg.getBuckets()) {
      try {
        final XContentBuilder builder = XContentFactory.jsonBuilder();
        bucket.toXContent(builder, null);
        actionResp.addAggregation(
            new AggWrapper(AggWrapper.AggregationType.MULTI_BUCKETS, builder.string()));
      } catch (final IOException e) {
        // Ignored
      }
    }
  }
}
 
Example #5
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void geoHashGrid() throws SQLFeatureNotSupportedException, SqlParseException {
    Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/location GROUP BY geohash_grid(field='center',precision=5) ", TEST_INDEX_LOCATION));
    InternalGeoHashGrid grid = result.get("geohash_grid(field=center,precision=5)");
    Collection<? extends InternalMultiBucketAggregation.InternalBucket> buckets = grid.getBuckets();
    for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) {
        Assert.assertTrue(bucket.getKeyAsString().equals("w2fsm") || bucket.getKeyAsString().equals("w0p6y") );
        Assert.assertEquals(1,bucket.getDocCount());
    }
}
 
Example #6
Source File: BucketScriptPipelineAggregator.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@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, reduceContext, Collections.<String, String>emptyMap());
    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");
                }
                List<InternalAggregation> aggs = new ArrayList<>(eagerTransform(bucket.getAggregations().asList(), FUNCTION));
                aggs.add(new InternalSimpleValue(name(), ((Number) returned).doubleValue(), formatter,
                        new ArrayList<PipelineAggregator>(), metaData()));
                InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs),
                        (InternalMultiBucketAggregation.InternalBucket) bucket);
                newBuckets.add(newBucket);
            }
        }
    }
    return originalAgg.create(newBuckets);
}
 
Example #7
Source File: BucketHelpers.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Given a path and a set of buckets, this method will return the value
 * inside the agg at that path. This is used to extract values for use by
 * pipeline aggregations (e.g. a derivative might need the price for each
 * bucket). If the bucket is empty, the configured GapPolicy is invoked to
 * resolve the missing bucket
 *
 * @param agg
 *            A series of agg buckets in the form of a histogram
 * @param bucket
 *            A specific bucket that a value needs to be extracted from.
 *            This bucket should be present in the <code>histo</code>
 *            parameter
 * @param aggPath
 *            The path to a particular value that needs to be extracted.
 *            This path should point to a metric inside the
 *            <code>bucket</code>
 * @param gapPolicy
 *            The gap policy to apply if empty buckets are found
 * @return The value extracted from <code>bucket</code> found at
 *         <code>aggPath</code>
 */
public static Double resolveBucketValue(InternalMultiBucketAggregation<?, ? extends InternalMultiBucketAggregation.Bucket> agg,
        InternalMultiBucketAggregation.Bucket bucket, String aggPath, GapPolicy gapPolicy) {
    List<String> aggPathsList = AggregationPath.parse(aggPath).getPathElementsAsStringList();
    return resolveBucketValue(agg, bucket, aggPathsList, gapPolicy);
}