org.elasticsearch.search.aggregations.metrics.Sum Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.Sum. 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: MetricsQueryEs7DAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public int readMetricsValue(final MetricsCondition condition,
                            final String valueColumnName,
                            final Duration duration) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
    buildQuery(sourceBuilder, condition, duration);

    TermsAggregationBuilder entityIdAggregation = AggregationBuilders.terms(Metrics.ENTITY_ID)
                                                                     .field(Metrics.ENTITY_ID)
                                                                     .size(1);
    final Function function = ValueColumnMetadata.INSTANCE.getValueFunction(condition.getName());
    functionAggregation(function, entityIdAggregation, valueColumnName);

    sourceBuilder.aggregation(entityIdAggregation);

    SearchResponse response = getClient().search(condition.getName(), sourceBuilder);

    Terms idTerms = response.getAggregations().get(Metrics.ENTITY_ID);
    for (Terms.Bucket idBucket : idTerms.getBuckets()) {
        switch (function) {
            case Sum:
                Sum sum = idBucket.getAggregations().get(valueColumnName);
                return (int) sum.getValue();
            case Avg:
                Avg avg = idBucket.getAggregations().get(valueColumnName);
                return (int) avg.getValue();
            default:
                avg = idBucket.getAggregations().get(valueColumnName);
                return (int) avg.getValue();
        }
    }
    return ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName());
}
 
Example #2
Source File: MetricsQueryEs7DAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
protected void functionAggregation(Function function, TermsAggregationBuilder parentAggBuilder, String valueCName) {
    switch (function) {
        case Avg:
            parentAggBuilder.subAggregation(AggregationBuilders.avg(valueCName).field(valueCName));
            break;
        case Sum:
            parentAggBuilder.subAggregation(AggregationBuilders.sum(valueCName).field(valueCName));
            break;
        default:
            parentAggBuilder.subAggregation(AggregationBuilders.avg(valueCName).field(valueCName));
            break;
    }
}
 
Example #3
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void sumOnNestedField() throws Exception {
    Aggregations result = query(String.format("SELECT sum(nested(message.dayOfWeek)) as sumDays FROM %s/nestedType", TEST_INDEX_NESTED_TYPE));
    InternalNested nested = result.get("message.dayOfWeek@NESTED");
    Sum sum = nested.getAggregations().get("sumDays");
    Assert.assertEquals(13.0,sum.getValue(),0.0001);

}
 
Example #4
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
@Test
public void sumTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	Aggregations result = query(String.format("SELECT SUM(balance) FROM %s/account", TEST_INDEX_ACCOUNT));
	Sum sum = result.get("SUM(balance)");
	assertThat(sum.getValue(), equalTo(25714837.0));
}