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

The following examples show how to use org.elasticsearch.search.aggregations.metrics.avg.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: ElasticsearchDataQuery.java    From frostmourne with MIT License 6 votes vote down vote up
private Double findAggregationValue(MetricContract metricContract, SearchResponse searchResponse) {
    String aggType = metricContract.getAggregation_type();
    if (aggType.equalsIgnoreCase("max")) {
        Max max = searchResponse.getAggregations().get("maxNumber");
        return max.getValue();
    }
    if (aggType.equalsIgnoreCase("min")) {
        Min min = searchResponse.getAggregations().get("minNumber");
        return min.getValue();
    }
    if (aggType.equalsIgnoreCase("avg")) {
        Avg avg = searchResponse.getAggregations().get("avgNumber");
        return avg.getValue();
    }
    if (aggType.equalsIgnoreCase("sum")) {
        Sum sum = searchResponse.getAggregations().get("sumNumber");
        return sum.getValue();
    }

    throw new IllegalArgumentException("unsupported aggregation type: " + aggType);
}
 
Example #2
Source File: AggregationResponseHandle.java    From jetlinks-community with Apache License 2.0 6 votes vote down vote up
private static <A extends Aggregation> void route(Bucket bucket, A a) {
    if (a instanceof Terms) {
        bucket.setBuckets(terms(a));
    } else if (a instanceof Range) {
        bucket.setBuckets(range(a));
    } else if (a instanceof Histogram) {
        bucket.setBuckets(range(a));
    } else if (a instanceof Avg) {
        bucket.setAvg(avg(a));
    } else if (a instanceof Min) {
        bucket.setMin(min(a));
    } else if (a instanceof Max) {
        bucket.setMax(max(a));
    } else if (a instanceof Sum) {
        bucket.setSum(sum(a));
    } else if (a instanceof Stats) {
        stats(bucket, a);
    }  else if (a instanceof ValueCount) {
        bucket.setValueCount(count(a));
    } else {
        throw new UnsupportedOperationException("不支持的聚合类型");
    }
}
 
Example #3
Source File: BaseDemo.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 聚合分析
 * 1. 先分组
 * 2. 子分组
 * 3. 最后算出子分组的平均值
 *
 * @param transportClient
 * @throws IOException
 */
private static void aggregate(TransportClient transportClient) throws IOException {

	SearchResponse searchResponse = transportClient.prepareSearch("product_index").setTypes("product")
			.addAggregation(AggregationBuilders.terms("product_group_by_price").field("price")
					.subAggregation(AggregationBuilders.dateHistogram("product_group_by_created_date_time").field("created_date_time")
							.dateHistogramInterval(DateHistogramInterval.YEAR)
							.subAggregation(AggregationBuilders.avg("product_avg_price").field("price")))
			).execute().actionGet();

	Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();

	StringTerms productGroupByPrice = (StringTerms) aggregationMap.get("product_group_by_price");
	Iterator<Terms.Bucket> productGroupByPriceIterator = productGroupByPrice.getBuckets().iterator();
	while (productGroupByPriceIterator.hasNext()) {
		Terms.Bucket productGroupByPriceBucket = productGroupByPriceIterator.next();
		logger.info("--------------------------------:" + productGroupByPriceBucket.getKey() + ":" + productGroupByPriceBucket.getDocCount());

		Histogram productGroupByPrice1 = (Histogram) productGroupByPriceBucket.getAggregations().asMap().get("product_group_by_price");
		Iterator<org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket> groupByCreateDateTimeIterator = productGroupByPrice1.getBuckets().iterator();
		while (groupByCreateDateTimeIterator.hasNext()) {
			Histogram.Bucket groupByCreateDateTimeBucket = groupByCreateDateTimeIterator.next();
			logger.info("--------------------------------:" + groupByCreateDateTimeBucket.getKey() + ":" + groupByCreateDateTimeBucket.getDocCount());

			Avg avgPrice = (Avg) groupByCreateDateTimeBucket.getAggregations().asMap().get("product_avg_price");
			logger.info("--------------------------------:" + avgPrice.getValue());
		}
	}


}
 
Example #4
Source File: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
private static NodeTimeseriesStatistics toNodeTimeseriesStatistics(Bucket bucket) {
    Terms term = bucket.getAggregations().get("components");
    NodeTimeseriesStatistics s = new NodeTimeseriesStatistics();
    s.setTimestamp(bucket.getKeyAsDate().getMillis());
    term.getBuckets().forEach(b -> {
        Avg avg = b.getAggregations().get("avg");
        NodeComponentTypeStatistics component = new NodeComponentTypeStatistics((long)avg.getValue(), b.getDocCount());
        s.getComponentTypes().put(b.getKey(), component);
    });
    return s;
}
 
Example #5
Source File: MetricsQueryEsDAO.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 #6
Source File: MetricsQueryEsDAO.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 #7
Source File: Search.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
public static String getValueFromAggregation(Aggregation a, Function f){
	
	String value = null;
	switch(f.type){
	case Function.SUM :
		value = String.valueOf(((Sum) a).getValue());
		break;
	case Function.COUNT :
		value = String.valueOf(((ValueCount) a).getValue());
		break;
	case Function.DC :
		value = String.valueOf(((Cardinality) a).getValue());
		break;
	case Function.AVG :
		value = String.valueOf(((Avg) a).getValue());
		break;
	case Function.MAX :
		value = String.valueOf(((Max) a).getValue());
		break;
	case Function.MIN :
		value = String.valueOf(((Min) a).getValue());
		break;
	}
	
	return value;
	
	
}
 
Example #8
Source File: AggregationQueryEsDAO.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 #9
Source File: SimpleTests.java    From elasticsearch-topk-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void assertTop10of50OneShardNestedAggregations() {
    client.admin().indices().prepareCreate("topk-4").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
    
    double sum = 0;
    for (int i = 0; i < 50; ++i) { // 50 values
        client.prepareIndex("topk-4", "type0", "doc" + i).setSource("{ \"field0\": \"foo" + i + "\", \"field1\":" + i +" }").setRefresh(true).execute().actionGet();
    }
    
    // foo0 x 50
    for (int i = 50; i < 100; ++i) {
        client.prepareIndex("topk-4", "type0", "doc" + i).setSource("{ \"field0\": \"foo0\", \"field1\":" + i +" }").setRefresh(true).execute().actionGet();
        sum += i;
    }
   
    SearchResponse searchResponse = client.prepareSearch("topk-4")
            .setQuery(matchAllQuery())
            .addAggregation(new TopKBuilder("topk").field("field0").size(10)
                    .subAggregation(new AvgBuilder("avg").field("field1"))
                    .subAggregation(new MaxBuilder("max").field("field1"))
            )
            .execute().actionGet();
    assertEquals(100, searchResponse.getHits().getTotalHits());
    TopK topk = searchResponse.getAggregations().get("topk");
    assertNotNull(topk);
    List<TopK.Bucket> buckets = new ArrayList<>(topk.getBuckets());
    assertEquals(10, buckets.size());
    assertEquals("foo0", buckets.get(0).getKey());
    assertEquals(51, buckets.get(0).getDocCount());
    assertEquals(2, buckets.get(0).getAggregations().asList().size());
    for (Aggregation agg : buckets.get(0).getAggregations()) {
        switch (agg.getName()) {
        case "avg":
            assertEquals(sum / 51, ((Avg) agg).getValue(), 0.01);
            break;
        case "max":
            assertEquals(99.0, ((Max) agg).getValue(), 0.001);
            break;
        default:
            assertTrue(false);
        } 
    }
}
 
Example #10
Source File: SimpleTests.java    From elasticsearch-topk-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void assertTop10of50TwoShardNestedAggregations() {
    client.admin().indices().prepareCreate("topk-5").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2)).execute().actionGet();
    
    double sum = 0;
    for (int i = 0; i < 50; ++i) { // 50 values
        client.prepareIndex("topk-5", "type0", "doc" + i).setSource("{ \"field0\": \"foo" + i + "\", \"field1\":" + i +" }").setRefresh(true).execute().actionGet();
    }
    
    // foo0 x 50
    for (int i = 50; i < 100; ++i) {
        client.prepareIndex("topk-5", "type0", "doc" + i).setSource("{ \"field0\": \"foo0\", \"field1\":" + i +" }").setRefresh(true).execute().actionGet();
        sum += i;
    }
   
    SearchResponse searchResponse = client.prepareSearch("topk-5")
            .setQuery(matchAllQuery())
            .addAggregation(new TopKBuilder("topk").field("field0").size(10)
                    .subAggregation(new AvgBuilder("avg").field("field1"))
                    .subAggregation(new MaxBuilder("max").field("field1"))
            )
            .execute().actionGet();
    assertEquals(100, searchResponse.getHits().getTotalHits());
    TopK topk = searchResponse.getAggregations().get("topk");
    assertNotNull(topk);
    List<TopK.Bucket> buckets = new ArrayList<>(topk.getBuckets());
    assertEquals(10, buckets.size());
    assertEquals("foo0", buckets.get(0).getKey());
    assertEquals(51, buckets.get(0).getDocCount());
    assertEquals(2, buckets.get(0).getAggregations().asList().size());
    for (Aggregation agg : buckets.get(0).getAggregations()) {
        switch (agg.getName()) {
        case "avg":
            assertEquals(sum / 51, ((Avg) agg).getValue(), 0.01);
            break;
        case "max":
            assertEquals(99.0, ((Max) agg).getValue(), 0.001);
            break;
        default:
            assertTrue(false);
        } 
    }
}
 
Example #11
Source File: SimpleTests.java    From elasticsearch-topk-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void assertTop10of50TwoShardNestedAggregationsStress() {
    client.admin().indices().prepareCreate("topk-6").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 4)).execute().actionGet();
    
    final int N = 30000;
    double sum = 0;
    int n = 0;
    int max = Integer.MIN_VALUE;
    Random r = new Random();
    for (int i = 0; i < N; ++i) {
        int v = r.nextInt();
        if (i % 7 == 0) {
            sum += v;
            ++n;
            if (v > max) {
                max = v;
            }
        }
        client.prepareIndex("topk-6", "type0", "doc" + i).setSource("{ \"field0\": \"foo" + (i % 7 == 0 ? "bar" : String.valueOf(i)) + "\", \"field1\":" + v +" }").setRefresh(i == N - 1).execute().actionGet();
    }
    try { Thread.sleep(2000); } catch (InterruptedException e) {} // FIXME: wait until all docs are searchable
    
    SearchResponse searchResponse = client.prepareSearch("topk-6")
            .setQuery(matchAllQuery())
            .addAggregation(new TopKBuilder("topk").field("field0").size(10)
                    .subAggregation(new AvgBuilder("avg").field("field1"))
                    .subAggregation(new MaxBuilder("max").field("field1"))
            )
            .execute().actionGet();
    assertEquals(N, searchResponse.getHits().getTotalHits());
    TopK topk = searchResponse.getAggregations().get("topk");
    assertNotNull(topk);
    List<TopK.Bucket> buckets = new ArrayList<>(topk.getBuckets());
    assertEquals(10, buckets.size());
    assertEquals("foobar", buckets.get(0).getKey());
    assertEquals(n, buckets.get(0).getDocCount());
    assertEquals(2, buckets.get(0).getAggregations().asList().size());
    for (Aggregation agg : buckets.get(0).getAggregations()) {
        switch (agg.getName()) {
        case "avg":
            assertEquals(sum / n, ((Avg) agg).getValue(), 0.01);
            break;
        case "max":
            assertEquals((double) max, ((Max) agg).getValue(), 0.001);
            break;
        default:
            assertTrue(false);
        } 
    }
}