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

The following examples show how to use org.elasticsearch.search.aggregations.metrics.Avg. 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: AggregationQueryEs7DAO.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Override
public List<SelectedRecord> sortMetrics(final TopNCondition condition,
                                        final String valueColumnName,
                                        final Duration duration,
                                        final List<KeyValue> additionalConditions) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

    final RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery(Metrics.TIME_BUCKET)
                                                        .lte(duration.getEndTimeBucket())
                                                        .gte(duration.getStartTimeBucket());

    boolean asc = false;
    if (condition.getOrder().equals(Order.ASC)) {
        asc = true;
    }

    if (additionalConditions != null && additionalConditions.size() > 0) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        additionalConditions.forEach(additionalCondition -> {
            boolQuery.must()
                     .add(QueryBuilders.termsQuery(additionalCondition.getKey(), additionalCondition.getValue()));
        });
        boolQuery.must().add(queryBuilder);
        sourceBuilder.query(boolQuery);
    } else {
        sourceBuilder.query(queryBuilder);
    }

    sourceBuilder.aggregation(
        AggregationBuilders.terms(Metrics.ENTITY_ID)
                           .field(Metrics.ENTITY_ID)
                           .order(BucketOrder.aggregation(valueColumnName, asc))
                           .size(condition.getTopN())
                           .subAggregation(AggregationBuilders.avg(valueColumnName).field(valueColumnName))
    );

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

    List<SelectedRecord> topNList = new ArrayList<>();
    Terms idTerms = response.getAggregations().get(Metrics.ENTITY_ID);
    for (Terms.Bucket termsBucket : idTerms.getBuckets()) {
        SelectedRecord record = new SelectedRecord();
        record.setId(termsBucket.getKeyAsString());
        Avg value = termsBucket.getAggregations().get(valueColumnName);
        record.setValue(String.valueOf((long) value.getValue()));
        topNList.add(record);
    }

    return topNList;
}
 
Example #4
Source File: AggregationTest.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
@Test
public void avgTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	Aggregations result = query(String.format("SELECT AVG(age) FROM %s/account", TEST_INDEX_ACCOUNT));
	Avg avg = result.get("AVG(age)");
	assertThat(avg.getValue(), equalTo(30.171));
}